regenerate-thumbnails.ts 1.9 KB

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