1 import { registerTSPaths } from '../server/helpers/register-ts-paths'
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'
14 .option('-v, --video [videoUUID]', 'Video UUID')
15 .option('-r, --resolution [resolution]', 'Video resolution (integer)')
16 .option('--generate-hls', 'Generate HLS playlist')
19 const options = program.opts()
21 if (options.video === undefined) {
22 console.error('All parameters are mandatory.')
26 if (options.resolution !== undefined && Number.isNaN(+options.resolution)) {
27 console.error('The resolution must be an integer (example: 1080).')
32 .then(() => process.exit(0))
38 async function run () {
39 await initDatabaseModels(true)
41 if (isUUIDValid(options.video) === false) {
42 console.error('%s is not a valid video UUID.', options.video)
46 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.video)
47 if (!video) throw new Error('Video not found.')
49 const dataInput: VideoTranscodingPayload[] = []
50 const { videoFileResolution } = await video.getMaxQualityResolution()
53 if (options.generateHls || CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false) {
54 const resolutionsEnabled = options.resolution
55 ? [ options.resolution ]
56 : computeResolutionsToTranscode(videoFileResolution, 'vod').concat([ videoFileResolution ])
58 for (const resolution of resolutionsEnabled) {
60 type: 'new-resolution-to-hls',
61 videoUUID: video.uuid,
63 isPortraitMode: false,
69 if (options.resolution !== undefined) {
71 type: 'new-resolution-to-webtorrent',
72 videoUUID: video.uuid,
74 resolution: options.resolution
77 if (video.VideoFiles.length === 0) {
78 console.error('Cannot regenerate webtorrent files with a HLS only video.')
83 type: 'optimize-to-webtorrent',
84 videoUUID: video.uuid,
90 await JobQueue.Instance.init()
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)