1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
4 import * as Bluebird from 'bluebird'
5 import { program } from 'commander'
6 import { pathExists, remove } from 'fs-extra'
7 import { generateImageFilename, 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'
14 .description('Regenerate local thumbnails using preview files')
18 .then(() => process.exit(0))
19 .catch(err => console.error(err))
21 async function run () {
22 await initDatabaseModels(true)
24 const videos = await VideoModel.listLocal()
26 await Bluebird.map(videos, v => {
27 return processVideo(v)
28 .catch(err => console.error('Cannot process video %s.', v.url, err))
29 }, { concurrency: 20 })
32 async function processVideo (videoArg: MVideo) {
33 const video = await VideoModel.loadWithFiles(videoArg.id)
35 console.log('Processing video %s.', video.name)
37 const thumbnail = video.getMiniature()
38 const preview = video.getPreview()
40 const previewPath = preview.getPath()
42 if (!await pathExists(previewPath)) {
43 throw new Error(`Preview ${previewPath} does not exist on disk`)
47 width: THUMBNAILS_SIZE.width,
48 height: THUMBNAILS_SIZE.height
51 const oldPath = thumbnail.getPath()
54 thumbnail.filename = generateImageFilename()
55 thumbnail.width = size.width
56 thumbnail.height = size.height
58 const thumbnailPath = thumbnail.getPath()
59 await processImage(previewPath, thumbnailPath, size, true)
61 // Save new attributes
62 await thumbnail.save()
64 // Remove old thumbnail
67 // Don't federate, remote instances will refresh the thumbnails after a while