From dee6fe1e4f5c024fd387e8c2b306c174b24aa8b3 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 22 Nov 2019 10:45:03 +0100 Subject: [PATCH] Add ability to generate HLS in CLI --- scripts/create-transcoding-job.ts | 45 ++++++- server/tests/cli/create-transcoding-job.ts | 133 ++++++++++++++++++--- 2 files changed, 155 insertions(+), 23 deletions(-) diff --git a/scripts/create-transcoding-job.ts b/scripts/create-transcoding-job.ts index 67a270a86..27170299d 100755 --- a/scripts/create-transcoding-job.ts +++ b/scripts/create-transcoding-job.ts @@ -6,10 +6,12 @@ import { VideoModel } from '../server/models/video/video' import { initDatabaseModels } from '../server/initializers' import { JobQueue } from '../server/lib/job-queue' import { VideoTranscodingPayload } from '../server/lib/job-queue/handlers/video-transcoding' +import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg-utils' program .option('-v, --video [videoUUID]', 'Video UUID') .option('-r, --resolution [resolution]', 'Video resolution (integer)') + .option('--generate-hls', 'Generate HLS playlist') .parse(process.argv) if (program['video'] === undefined) { @@ -32,14 +34,45 @@ run() async function run () { await initDatabaseModels(true) - const video = await VideoModel.loadByUUID(program['video']) + const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(program['video']) if (!video) throw new Error('Video not found.') - const dataInput: VideoTranscodingPayload = program.resolution !== undefined - ? { type: 'new-resolution' as 'new-resolution', videoUUID: video.uuid, isNewVideo: false, resolution: program.resolution } - : { type: 'optimize' as 'optimize', videoUUID: video.uuid, isNewVideo: false } + const dataInput: VideoTranscodingPayload[] = [] + const { videoFileResolution } = await video.getMaxQualityResolution() + + if (program.generateHls) { + const resolutionsEnabled = program.resolution + ? [ program.resolution ] + : computeResolutionsToTranscode(videoFileResolution).concat([ videoFileResolution ]) + + for (const resolution of resolutionsEnabled) { + dataInput.push({ + type: 'hls', + videoUUID: video.uuid, + resolution, + isPortraitMode: false, + copyCodecs: false + }) + } + } else if (program.resolution !== undefined) { + dataInput.push({ + type: 'new-resolution' as 'new-resolution', + videoUUID: video.uuid, + isNewVideo: false, + resolution: program.resolution + }) + } else { + dataInput.push({ + type: 'optimize' as 'optimize', + videoUUID: video.uuid, + isNewVideo: false + }) + } await JobQueue.Instance.init() - await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: dataInput }) - console.log('Transcoding job for video %s created.', video.uuid) + + for (const d of dataInput) { + await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: d }) + console.log('Transcoding job for video %s created.', video.uuid) + } } diff --git a/server/tests/cli/create-transcoding-job.ts b/server/tests/cli/create-transcoding-job.ts index 1c0e10066..7897ff1b3 100644 --- a/server/tests/cli/create-transcoding-job.ts +++ b/server/tests/cli/create-transcoding-job.ts @@ -14,7 +14,7 @@ import { getVideosList, killallServers, ServerInfo, - setAccessTokensToServers, + setAccessTokensToServers, updateCustomSubConfig, uploadVideo, wait } from '../../../shared/extra-utils' import { waitJobs } from '../../../shared/extra-utils/server/jobs' @@ -23,8 +23,24 @@ const expect = chai.expect describe('Test create transcoding jobs', function () { let servers: ServerInfo[] = [] - let video1UUID: string - let video2UUID: string + let videosUUID: string[] = [] + + const config = { + transcoding: { + enabled: false, + resolutions: { + '240p': true, + '360p': true, + '480p': true, + '720p': true, + '1080p': true, + '2160p': true + }, + hls: { + enabled: false + } + } + } before(async function () { this.timeout(60000) @@ -33,13 +49,14 @@ describe('Test create transcoding jobs', function () { servers = await flushAndRunMultipleServers(2) await setAccessTokensToServers(servers) + await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) + await doubleFollow(servers[0], servers[1]) - // Upload two videos for our needs - const res1 = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video1' }) - video1UUID = res1.body.video.uuid - const res2 = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video2' }) - video2UUID = res2.body.video.uuid + for (let i = 1; i <= 5; i++) { + const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video' + i }) + videosUUID.push(res.body.video.uuid) + } await waitJobs(servers) }) @@ -50,12 +67,13 @@ describe('Test create transcoding jobs', function () { for (const server of servers) { const res = await getVideosList(server.url) const videos = res.body.data - expect(videos).to.have.lengthOf(2) + expect(videos).to.have.lengthOf(videosUUID.length) for (const video of videos) { const res2 = await getVideo(server.url, video.uuid) const videoDetail: VideoDetails = res2.body expect(videoDetail.files).to.have.lengthOf(1) + expect(videoDetail.streamingPlaylists).to.have.lengthOf(0) } } }) @@ -64,14 +82,13 @@ describe('Test create transcoding jobs', function () { this.timeout(60000) const env = getEnvCli(servers[0]) - await execCLI(`${env} npm run create-transcoding-job -- -v ${video2UUID}`) + await execCLI(`${env} npm run create-transcoding-job -- -v ${videosUUID[1]}`) await waitJobs(servers) for (const server of servers) { const res = await getVideosList(server.url) const videos = res.body.data - expect(videos).to.have.lengthOf(2) let infoHashes: { [ id: number ]: string } @@ -79,8 +96,9 @@ describe('Test create transcoding jobs', function () { const res2 = await getVideo(server.url, video.uuid) const videoDetail: VideoDetails = res2.body - if (video.uuid === video2UUID) { + if (video.uuid === videosUUID[1]) { expect(videoDetail.files).to.have.lengthOf(4) + expect(videoDetail.streamingPlaylists).to.have.lengthOf(0) if (!infoHashes) { infoHashes = {} @@ -96,6 +114,7 @@ describe('Test create transcoding jobs', function () { } } else { expect(videoDetail.files).to.have.lengthOf(1) + expect(videoDetail.streamingPlaylists).to.have.lengthOf(0) } } } @@ -105,23 +124,103 @@ describe('Test create transcoding jobs', function () { this.timeout(60000) const env = getEnvCli(servers[0]) - await execCLI(`${env} npm run create-transcoding-job -- -v ${video1UUID} -r 480`) + await execCLI(`${env} npm run create-transcoding-job -- -v ${videosUUID[0]} -r 480`) await waitJobs(servers) for (const server of servers) { const res = await getVideosList(server.url) const videos = res.body.data - expect(videos).to.have.lengthOf(2) + expect(videos).to.have.lengthOf(videosUUID.length) - const res2 = await getVideo(server.url, video1UUID) + const res2 = await getVideo(server.url, videosUUID[0]) const videoDetail: VideoDetails = res2.body expect(videoDetail.files).to.have.lengthOf(2) - expect(videoDetail.files[0].resolution.id).to.equal(720) - expect(videoDetail.files[1].resolution.id).to.equal(480) + + expect(videoDetail.streamingPlaylists).to.have.lengthOf(0) + } + }) + + it('Should generate an HLS resolution', async function () { + this.timeout(120000) + + const env = getEnvCli(servers[0]) + await execCLI(`${env} npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`) + + await waitJobs(servers) + + for (const server of servers) { + const res = await getVideo(server.url, videosUUID[2]) + const videoDetail: VideoDetails = res.body + + expect(videoDetail.files).to.have.lengthOf(1) + expect(videoDetail.streamingPlaylists).to.have.lengthOf(1) + + const files = videoDetail.streamingPlaylists[0].files + expect(files).to.have.lengthOf(1) + expect(files[0].resolution.id).to.equal(480) + } + }) + + it('Should not duplicate an HLS resolution', async function () { + this.timeout(120000) + + const env = getEnvCli(servers[0]) + await execCLI(`${env} npm run create-transcoding-job -- -v ${videosUUID[2]} --generate-hls -r 480`) + + await waitJobs(servers) + + for (const server of servers) { + const res = await getVideo(server.url, videosUUID[2]) + const videoDetail: VideoDetails = res.body + + const files = videoDetail.streamingPlaylists[0].files + expect(files).to.have.lengthOf(1) + expect(files[0].resolution.id).to.equal(480) + } + }) + + it('Should generate all HLS resolutions', async function () { + this.timeout(120000) + + const env = getEnvCli(servers[0]) + await execCLI(`${env} npm run create-transcoding-job -- -v ${videosUUID[3]} --generate-hls`) + + await waitJobs(servers) + + for (const server of servers) { + const res = await getVideo(server.url, videosUUID[3]) + const videoDetail: VideoDetails = res.body + + expect(videoDetail.files).to.have.lengthOf(1) + expect(videoDetail.streamingPlaylists).to.have.lengthOf(1) + + const files = videoDetail.streamingPlaylists[0].files + expect(files).to.have.lengthOf(4) + } + }) + + it('Should optimize the video file and generate HLS videos if enabled in config', async function () { + this.timeout(120000) + + config.transcoding.hls.enabled = true + await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config) + + const env = getEnvCli(servers[0]) + await execCLI(`${env} npm run create-transcoding-job -- -v ${videosUUID[4]}`) + + await waitJobs(servers) + + for (const server of servers) { + const res = await getVideo(server.url, videosUUID[4]) + const videoDetail: VideoDetails = res.body + + expect(videoDetail.files).to.have.lengthOf(4) + expect(videoDetail.streamingPlaylists).to.have.lengthOf(1) + expect(videoDetail.streamingPlaylists[0].files).to.have.lengthOf(4) } })