regenerate-thumbnails.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { map } from 'bluebird'
  2. import { program } from 'commander'
  3. import { pathExists, remove } from 'fs-extra'
  4. import { generateImageFilename, processImage } from '@server/helpers/image-utils'
  5. import { THUMBNAILS_SIZE } from '@server/initializers/constants'
  6. import { initDatabaseModels } from '@server/initializers/database'
  7. import { VideoModel } from '@server/models/video/video'
  8. program
  9. .description('Regenerate local thumbnails using preview files')
  10. .parse(process.argv)
  11. run()
  12. .then(() => process.exit(0))
  13. .catch(err => console.error(err))
  14. async function run () {
  15. await initDatabaseModels(true)
  16. const ids = await VideoModel.listLocalIds()
  17. await map(ids, id => {
  18. return processVideo(id)
  19. .catch(err => console.error('Cannot process video %d.', id, err))
  20. }, { concurrency: 20 })
  21. }
  22. async function processVideo (id: number) {
  23. const video = await VideoModel.loadWithFiles(id)
  24. console.log('Processing video %s.', video.name)
  25. const thumbnail = video.getMiniature()
  26. const preview = video.getPreview()
  27. const previewPath = preview.getPath()
  28. if (!await pathExists(previewPath)) {
  29. throw new Error(`Preview ${previewPath} does not exist on disk`)
  30. }
  31. const size = {
  32. width: THUMBNAILS_SIZE.width,
  33. height: THUMBNAILS_SIZE.height
  34. }
  35. const oldPath = thumbnail.getPath()
  36. // Update thumbnail
  37. thumbnail.filename = generateImageFilename()
  38. thumbnail.width = size.width
  39. thumbnail.height = size.height
  40. const thumbnailPath = thumbnail.getPath()
  41. await processImage({ path: previewPath, destination: thumbnailPath, newSize: size, keepOriginal: true })
  42. // Save new attributes
  43. await thumbnail.save()
  44. // Remove old thumbnail
  45. await remove(oldPath)
  46. // Don't federate, remote instances will refresh the thumbnails after a while
  47. }