1 import { program } from 'commander'
2 import { isUUIDValid, toCompleteUUID } from '@server/helpers/custom-validators/misc'
3 import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg'
4 import { CONFIG } from '@server/initializers/config'
5 import { buildTranscodingJob } from '@server/lib/video'
6 import { VideoState, VideoTranscodingPayload } from '@shared/models'
7 import { initDatabaseModels } from '../server/initializers/database'
8 import { JobQueue } from '../server/lib/job-queue'
9 import { VideoModel } from '../server/models/video/video'
12 .option('-v, --video [videoUUID]', 'Video UUID')
13 .option('-r, --resolution [resolution]', 'Video resolution (integer)')
14 .option('--generate-hls', 'Generate HLS playlist')
17 const options = program.opts()
19 if (options.video === undefined) {
20 console.error('All parameters are mandatory.')
24 if (options.resolution !== undefined && Number.isNaN(+options.resolution)) {
25 console.error('The resolution must be an integer (example: 1080).')
30 .then(() => process.exit(0))
36 async function run () {
37 await initDatabaseModels(true)
39 const uuid = toCompleteUUID(options.video)
41 if (isUUIDValid(uuid) === false) {
42 console.error('%s is not a valid video UUID.', options.video)
46 const video = await VideoModel.loadFull(uuid)
47 if (!video) throw new Error('Video not found.')
49 const dataInput: VideoTranscodingPayload[] = []
50 const maxResolution = video.getMaxQualityFile().resolution
52 // FIXME: check the file has audio
56 if (options.generateHls || CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) {
57 const resolutionsEnabled = options.resolution
58 ? [ parseInt(options.resolution) ]
59 : computeResolutionsToTranscode({ input: maxResolution, type: 'vod', includeInput: true, strictLower: false, hasAudio })
61 for (const resolution of resolutionsEnabled) {
63 type: 'new-resolution-to-hls' as 'new-resolution-to-hls',
64 videoUUID: video.uuid,
71 isMaxQuality: maxResolution === resolution,
72 autoDeleteWebTorrentIfNeeded: false
76 if (options.resolution !== undefined) {
78 type: 'new-resolution-to-webtorrent' as 'new-resolution-to-webtorrent',
79 videoUUID: video.uuid,
81 createHLSIfNeeded: true,
86 resolution: parseInt(options.resolution)
89 if (video.VideoFiles.length === 0) {
90 console.error('Cannot regenerate webtorrent files with a HLS only video.')
95 type: 'optimize-to-webtorrent' as 'optimize-to-webtorrent',
96 videoUUID: video.uuid,
102 JobQueue.Instance.init()
104 video.state = VideoState.TO_TRANSCODE
107 for (const d of dataInput) {
108 await JobQueue.Instance.createJob(await buildTranscodingJob(d))
110 console.log('Transcoding job for video %s created.', video.uuid)