]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - scripts/regenerate-thumbnails.ts
Translated using Weblate (Czech)
[github/Chocobozzz/PeerTube.git] / scripts / regenerate-thumbnails.ts
... / ...
CommitLineData
1import { registerTSPaths } from '../server/helpers/register-ts-paths'
2registerTSPaths()
3
4import { map } from 'bluebird'
5import { program } from 'commander'
6import { pathExists, remove } from 'fs-extra'
7import { generateImageFilename, processImage } from '@server/helpers/image-utils'
8import { THUMBNAILS_SIZE } from '@server/initializers/constants'
9import { VideoModel } from '@server/models/video/video'
10import { initDatabaseModels } from '@server/initializers/database'
11
12program
13 .description('Regenerate local thumbnails using preview files')
14 .parse(process.argv)
15
16run()
17 .then(() => process.exit(0))
18 .catch(err => console.error(err))
19
20async 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
31async 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}