]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/regenerate-thumbnails.ts
Translated using Weblate (Czech)
[github/Chocobozzz/PeerTube.git] / scripts / regenerate-thumbnails.ts
CommitLineData
c2bd7a6f
C
1import { registerTSPaths } from '../server/helpers/register-ts-paths'
2registerTSPaths()
3
41fb13c3 4import { map } from 'bluebird'
8cc61201 5import { program } from 'commander'
a0eeb45f 6import { pathExists, remove } from 'fs-extra'
84531547 7import { generateImageFilename, processImage } from '@server/helpers/image-utils'
c2bd7a6f
C
8import { THUMBNAILS_SIZE } from '@server/initializers/constants'
9import { VideoModel } from '@server/models/video/video'
c2bd7a6f
C
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
e1ab52d7 23 const ids = await VideoModel.listLocalIds()
c2bd7a6f 24
e1ab52d7 25 await map(ids, id => {
26 return processVideo(id)
27 .catch(err => console.error('Cannot process video %d.', id, err))
c2bd7a6f
C
28 }, { concurrency: 20 })
29}
30
e1ab52d7 31async function processVideo (id: number) {
32 const video = await VideoModel.loadWithFiles(id)
c2bd7a6f 33
6794d100
C
34 console.log('Processing video %s.', video.name)
35
c2bd7a6f
C
36 const thumbnail = video.getMiniature()
37 const preview = video.getPreview()
38
c2bd7a6f
C
39 const previewPath = preview.getPath()
40
c2bd7a6f
C
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 }
a0eeb45f
C
49
50 const oldPath = thumbnail.getPath()
51
52 // Update thumbnail
84531547 53 thumbnail.filename = generateImageFilename()
a0eeb45f
C
54 thumbnail.width = size.width
55 thumbnail.height = size.height
56
57 const thumbnailPath = thumbnail.getPath()
c2bd7a6f 58 await processImage(previewPath, thumbnailPath, size, true)
a0eeb45f
C
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
c2bd7a6f 67}