]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/create-transcoding-job.ts
0bb9bfeab8d25d9858c4ec4cf361c05336d96c52
[github/Chocobozzz/PeerTube.git] / scripts / create-transcoding-job.ts
1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
2 registerTSPaths()
3
4 import { 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 { VideoState, VideoTranscodingPayload } from '@shared/models'
10 import { CONFIG } from '@server/initializers/config'
11 import { isUUIDValid } from '@server/helpers/custom-validators/misc'
12 import { addTranscodingJob } from '@server/lib/video'
13
14 program
15 .option('-v, --video [videoUUID]', 'Video UUID')
16 .option('-r, --resolution [resolution]', 'Video resolution (integer)')
17 .option('--generate-hls', 'Generate HLS playlist')
18 .parse(process.argv)
19
20 const options = program.opts()
21
22 if (options.video === undefined) {
23 console.error('All parameters are mandatory.')
24 process.exit(-1)
25 }
26
27 if (options.resolution !== undefined && Number.isNaN(+options.resolution)) {
28 console.error('The resolution must be an integer (example: 1080).')
29 process.exit(-1)
30 }
31
32 run()
33 .then(() => process.exit(0))
34 .catch(err => {
35 console.error(err)
36 process.exit(-1)
37 })
38
39 async function run () {
40 await initDatabaseModels(true)
41
42 if (isUUIDValid(options.video) === false) {
43 console.error('%s is not a valid video UUID.', options.video)
44 return
45 }
46
47 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.video)
48 if (!video) throw new Error('Video not found.')
49
50 const dataInput: VideoTranscodingPayload[] = []
51 const resolution = video.getMaxQualityFile().resolution
52
53 // Generate HLS files
54 if (options.generateHls || CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) {
55 const resolutionsEnabled = options.resolution
56 ? [ options.resolution ]
57 : computeResolutionsToTranscode(resolution, 'vod').concat([ resolution ])
58
59 for (const resolution of resolutionsEnabled) {
60 dataInput.push({
61 type: 'new-resolution-to-hls',
62 videoUUID: video.uuid,
63 resolution,
64 isPortraitMode: false,
65 copyCodecs: false,
66 isNewVideo: false,
67 isMaxQuality: false
68 })
69 }
70 } else {
71 if (options.resolution !== undefined) {
72 dataInput.push({
73 type: 'new-resolution-to-webtorrent',
74 videoUUID: video.uuid,
75 isNewVideo: false,
76 resolution: options.resolution
77 })
78 } else {
79 if (video.VideoFiles.length === 0) {
80 console.error('Cannot regenerate webtorrent files with a HLS only video.')
81 return
82 }
83
84 dataInput.push({
85 type: 'optimize-to-webtorrent',
86 videoUUID: video.uuid,
87 isNewVideo: false
88 })
89 }
90 }
91
92 JobQueue.Instance.init()
93
94 video.state = VideoState.TO_TRANSCODE
95 await video.save()
96
97 for (const d of dataInput) {
98 await addTranscodingJob(d, {})
99 console.log('Transcoding job for video %s created.', video.uuid)
100 }
101 }