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