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