]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/create-transcoding-job.ts
Stop using tsconfig register
[github/Chocobozzz/PeerTube.git] / scripts / create-transcoding-job.ts
CommitLineData
8cc61201 1import { program } from 'commander'
f8360396 2import { isUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc'
ad5db104 3import { computeLowerResolutionsToTranscode } from '@server/helpers/ffprobe-utils'
20eb3a5b 4import { CONFIG } from '@server/initializers/config'
0305db28 5import { addTranscodingJob } from '@server/lib/video'
f8360396
C
6import { VideoState, VideoTranscodingPayload } from '@shared/models'
7import { initDatabaseModels } from '../server/initializers/database'
8import { JobQueue } from '../server/lib/job-queue'
9import { VideoModel } from '../server/models/video/video'
0c948c16
C
10
11program
12 .option('-v, --video [videoUUID]', 'Video UUID')
05623b90 13 .option('-r, --resolution [resolution]', 'Video resolution (integer)')
dee6fe1e 14 .option('--generate-hls', 'Generate HLS playlist')
0c948c16
C
15 .parse(process.argv)
16
ba5a8d89
C
17const options = program.opts()
18
19if (options.video === undefined) {
0c948c16
C
20 console.error('All parameters are mandatory.')
21 process.exit(-1)
22}
23
ba5a8d89 24if (options.resolution !== undefined && Number.isNaN(+options.resolution)) {
05623b90
F
25 console.error('The resolution must be an integer (example: 1080).')
26 process.exit(-1)
27}
28
0c948c16
C
29run()
30 .then(() => process.exit(0))
31 .catch(err => {
32 console.error(err)
33 process.exit(-1)
34 })
35
36async function run () {
37 await initDatabaseModels(true)
38
c186a67f
C
39 const uuid = toCompleteUUID(options.video)
40
41 if (isUUIDValid(uuid) === false) {
9aeef9aa
C
42 console.error('%s is not a valid video UUID.', options.video)
43 return
44 }
45
c186a67f 46 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(uuid)
0c948c16
C
47 if (!video) throw new Error('Video not found.')
48
dee6fe1e 49 const dataInput: VideoTranscodingPayload[] = []
ad5db104 50 const maxResolution = video.getMaxQualityFile().resolution
dee6fe1e 51
20eb3a5b
C
52 // Generate HLS files
53 if (options.generateHls || CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) {
ba5a8d89 54 const resolutionsEnabled = options.resolution
8aad7ae4 55 ? [ parseInt(options.resolution) ]
ad5db104 56 : computeLowerResolutionsToTranscode(maxResolution, 'vod').concat([ maxResolution ])
dee6fe1e
C
57
58 for (const resolution of resolutionsEnabled) {
59 dataInput.push({
24516aa2 60 type: 'new-resolution-to-hls',
dee6fe1e
C
61 videoUUID: video.uuid,
62 resolution,
63 isPortraitMode: false,
9129b769 64 copyCodecs: false,
0305db28 65 isNewVideo: false,
ad5db104
C
66 isMaxQuality: maxResolution === resolution,
67 autoDeleteWebTorrentIfNeeded: false
dee6fe1e
C
68 })
69 }
dee6fe1e 70 } else {
20eb3a5b
C
71 if (options.resolution !== undefined) {
72 dataInput.push({
73 type: 'new-resolution-to-webtorrent',
74 videoUUID: video.uuid,
75 isNewVideo: false,
8aad7ae4 76 resolution: parseInt(options.resolution)
20eb3a5b
C
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 }
dee6fe1e 90 }
0c948c16 91
e1ab52d7 92 JobQueue.Instance.init(true)
0305db28
JB
93
94 video.state = VideoState.TO_TRANSCODE
95 await video.save()
dee6fe1e
C
96
97 for (const d of dataInput) {
0305db28 98 await addTranscodingJob(d, {})
dee6fe1e
C
99 console.log('Transcoding job for video %s created.', video.uuid)
100 }
0c948c16 101}