1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import * as Bluebird from 'bluebird'
5 import * as program from 'commander'
6 import { pathExists, remove } from 'fs-extra'
7 import { processImage } from '@server/helpers/image-utils'
8 import { THUMBNAILS_SIZE } from '@server/initializers/constants'
9 import { VideoModel } from '@server/models/video/video'
10 import { MVideo } from '@server/types/models'
11 import { initDatabaseModels } from '@server/initializers/database'
12 import { ActorImageModel } from '@server/models/account/actor-image'
15 .description('Regenerate local thumbnails using preview files')
19 .then(() => process.exit(0))
20 .catch(err => console.error(err))
22 async function run () {
23 await initDatabaseModels(true)
25 const videos = await VideoModel.listLocal()
27 await Bluebird.map(videos, v => {
28 return processVideo(v)
29 .catch(err => console.error('Cannot process video %s.', v.url, err))
30 }, { concurrency: 20 })
33 async function processVideo (videoArg: MVideo) {
34 const video = await VideoModel.loadWithFiles(videoArg.id)
36 console.log('Processing video %s.', video.name)
38 const thumbnail = video.getMiniature()
39 const preview = video.getPreview()
41 const previewPath = preview.getPath()
43 if (!await pathExists(previewPath)) {
44 throw new Error(`Preview ${previewPath} does not exist on disk`)
48 width: THUMBNAILS_SIZE.width,
49 height: THUMBNAILS_SIZE.height
52 const oldPath = thumbnail.getPath()
55 thumbnail.filename = ActorImageModel.generateFilename()
56 thumbnail.width = size.width
57 thumbnail.height = size.height
59 const thumbnailPath = thumbnail.getPath()
60 await processImage(previewPath, thumbnailPath, size, true)
62 // Save new attributes
63 await thumbnail.save()
65 // Remove old thumbnail
68 // Don't federate, remote instances will refresh the thumbnails after a while