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