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