Add ability to generate HLS in CLI

This commit is contained in:
Chocobozzz 2019-11-22 10:45:03 +01:00
parent 5a71acd254
commit dee6fe1e4f
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 155 additions and 23 deletions

View File

@ -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 })
for (const d of dataInput) {
await JobQueue.Instance.createJob({ type: 'video-transcoding', payload: d })
console.log('Transcoding job for video %s created.', video.uuid)
}
}

View File

@ -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)
}
})