Merge branch 'feature/audio-upload' into develop
[oweals/peertube.git] / scripts / create-transcoding-job.ts
1 import * as program from 'commander'
2 import { VideoModel } from '../server/models/video/video'
3 import { initDatabaseModels } from '../server/initializers'
4 import { JobQueue } from '../server/lib/job-queue'
5 import { VideoTranscodingPayload } from '../server/lib/job-queue/handlers/video-transcoding'
6
7 program
8   .option('-v, --video [videoUUID]', 'Video UUID')
9   .option('-r, --resolution [resolution]', 'Video resolution (integer)')
10   .parse(process.argv)
11
12 if (program['video'] === undefined) {
13   console.error('All parameters are mandatory.')
14   process.exit(-1)
15 }
16
17 if (program.resolution !== undefined && Number.isNaN(+program.resolution)) {
18   console.error('The resolution must be an integer (example: 1080).')
19   process.exit(-1)
20 }
21
22 run()
23   .then(() => process.exit(0))
24   .catch(err => {
25     console.error(err)
26     process.exit(-1)
27   })
28
29 async function run () {
30   await initDatabaseModels(true)
31
32   const video = await VideoModel.loadByUUIDWithFile(program['video'])
33   if (!video) throw new Error('Video not found.')
34
35   const dataInput: VideoTranscodingPayload = program.resolution !== undefined
36    ? { type: 'new-resolution' as 'new-resolution', videoUUID: video.uuid, isNewVideo: false, resolution: program.resolution }
37    : { type: 'optimize' as 'optimize', videoUUID: video.uuid, isNewVideo: false }
38
39   await JobQueue.Instance.init()
40   await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput })
41   console.log('Transcoding job for video %s created.', video.uuid)
42 }