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