]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/create-transcoding-job.ts
5f56d6d3684039e7be7e65c6691ac3670918e423
[github/Chocobozzz/PeerTube.git] / scripts / create-transcoding-job.ts
1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
2 registerTSPaths()
3
4 import * as program from 'commander'
5 import { VideoModel } from '../server/models/video/video'
6 import { initDatabaseModels } from '../server/initializers/database'
7 import { JobQueue } from '../server/lib/job-queue'
8 import { computeResolutionsToTranscode } from '@server/helpers/ffprobe-utils'
9 import { VideoTranscodingPayload } from '@shared/models'
10
11 program
12 .option('-v, --video [videoUUID]', 'Video UUID')
13 .option('-r, --resolution [resolution]', 'Video resolution (integer)')
14 .option('--generate-hls', 'Generate HLS playlist')
15 .parse(process.argv)
16
17 const options = program.opts()
18
19 if (options.video === undefined) {
20 console.error('All parameters are mandatory.')
21 process.exit(-1)
22 }
23
24 if (options.resolution !== undefined && Number.isNaN(+options.resolution)) {
25 console.error('The resolution must be an integer (example: 1080).')
26 process.exit(-1)
27 }
28
29 run()
30 .then(() => process.exit(0))
31 .catch(err => {
32 console.error(err)
33 process.exit(-1)
34 })
35
36 async function run () {
37 await initDatabaseModels(true)
38
39 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.video)
40 if (!video) throw new Error('Video not found.')
41
42 const dataInput: VideoTranscodingPayload[] = []
43 const { videoFileResolution } = await video.getMaxQualityResolution()
44
45 if (options.generateHls) {
46 const resolutionsEnabled = options.resolution
47 ? [ options.resolution ]
48 : computeResolutionsToTranscode(videoFileResolution, 'vod').concat([ videoFileResolution ])
49
50 for (const resolution of resolutionsEnabled) {
51 dataInput.push({
52 type: 'new-resolution-to-hls',
53 videoUUID: video.uuid,
54 resolution,
55 isPortraitMode: false,
56 copyCodecs: false,
57 isMaxQuality: false
58 })
59 }
60 } else if (options.resolution !== undefined) {
61 dataInput.push({
62 type: 'new-resolution-to-webtorrent',
63 videoUUID: video.uuid,
64 isNewVideo: false,
65 resolution: options.resolution
66 })
67 } else {
68 dataInput.push({
69 type: 'optimize-to-webtorrent',
70 videoUUID: video.uuid,
71 isNewVideo: false
72 })
73 }
74
75 await JobQueue.Instance.init()
76
77 for (const d of dataInput) {
78 await JobQueue.Instance.createJobWithPromise({ type: 'video-transcoding', payload: d })
79 console.log('Transcoding job for video %s created.', video.uuid)
80 }
81 }