]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/regenerate-thumbnails.ts
Fix transcoding job count inconsistency
[github/Chocobozzz/PeerTube.git] / scripts / regenerate-thumbnails.ts
1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
2 registerTSPaths()
3
4 import { map } 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 { initDatabaseModels } from '@server/initializers/database'
11
12 program
13 .description('Regenerate local thumbnails using preview files')
14 .parse(process.argv)
15
16 run()
17 .then(() => process.exit(0))
18 .catch(err => console.error(err))
19
20 async function run () {
21 await initDatabaseModels(true)
22
23 const ids = await VideoModel.listLocalIds()
24
25 await map(ids, id => {
26 return processVideo(id)
27 .catch(err => console.error('Cannot process video %d.', id, err))
28 }, { concurrency: 20 })
29 }
30
31 async function processVideo (id: number) {
32 const video = await VideoModel.loadWithFiles(id)
33
34 console.log('Processing video %s.', video.name)
35
36 const thumbnail = video.getMiniature()
37 const preview = video.getPreview()
38
39 const previewPath = preview.getPath()
40
41 if (!await pathExists(previewPath)) {
42 throw new Error(`Preview ${previewPath} does not exist on disk`)
43 }
44
45 const size = {
46 width: THUMBNAILS_SIZE.width,
47 height: THUMBNAILS_SIZE.height
48 }
49
50 const oldPath = thumbnail.getPath()
51
52 // Update thumbnail
53 thumbnail.filename = generateImageFilename()
54 thumbnail.width = size.width
55 thumbnail.height = size.height
56
57 const thumbnailPath = thumbnail.getPath()
58 await processImage(previewPath, thumbnailPath, size, true)
59
60 // Save new attributes
61 await thumbnail.save()
62
63 // Remove old thumbnail
64 await remove(oldPath)
65
66 // Don't federate, remote instances will refresh the thumbnails after a while
67 }