mirror of https://github.com/Chocobozzz/PeerTube
Add ability to generate HLS in CLI
parent
5a71acd254
commit
dee6fe1e4f
|
@ -6,10 +6,12 @@ import { VideoModel } from '../server/models/video/video'
|
||||||
import { initDatabaseModels } from '../server/initializers'
|
import { initDatabaseModels } from '../server/initializers'
|
||||||
import { JobQueue } from '../server/lib/job-queue'
|
import { JobQueue } from '../server/lib/job-queue'
|
||||||
import { VideoTranscodingPayload } from '../server/lib/job-queue/handlers/video-transcoding'
|
import { VideoTranscodingPayload } from '../server/lib/job-queue/handlers/video-transcoding'
|
||||||
|
import { computeResolutionsToTranscode } from '@server/helpers/ffmpeg-utils'
|
||||||
|
|
||||||
program
|
program
|
||||||
.option('-v, --video [videoUUID]', 'Video UUID')
|
.option('-v, --video [videoUUID]', 'Video UUID')
|
||||||
.option('-r, --resolution [resolution]', 'Video resolution (integer)')
|
.option('-r, --resolution [resolution]', 'Video resolution (integer)')
|
||||||
|
.option('--generate-hls', 'Generate HLS playlist')
|
||||||
.parse(process.argv)
|
.parse(process.argv)
|
||||||
|
|
||||||
if (program['video'] === undefined) {
|
if (program['video'] === undefined) {
|
||||||
|
@ -32,14 +34,45 @@ run()
|
||||||
async function run () {
|
async function run () {
|
||||||
await initDatabaseModels(true)
|
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.')
|
if (!video) throw new Error('Video not found.')
|
||||||
|
|
||||||
const dataInput: VideoTranscodingPayload = program.resolution !== undefined
|
const dataInput: VideoTranscodingPayload[] = []
|
||||||
? { type: 'new-resolution' as 'new-resolution', videoUUID: video.uuid, isNewVideo: false, resolution: program.resolution }
|
const { videoFileResolution } = await video.getMaxQualityResolution()
|
||||||
: { type: 'optimize' as 'optimize', videoUUID: video.uuid, isNewVideo: false }
|
|
||||||
|
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.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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ import {
|
||||||
getVideosList,
|
getVideosList,
|
||||||
killallServers,
|
killallServers,
|
||||||
ServerInfo,
|
ServerInfo,
|
||||||
setAccessTokensToServers,
|
setAccessTokensToServers, updateCustomSubConfig,
|
||||||
uploadVideo, wait
|
uploadVideo, wait
|
||||||
} from '../../../shared/extra-utils'
|
} from '../../../shared/extra-utils'
|
||||||
import { waitJobs } from '../../../shared/extra-utils/server/jobs'
|
import { waitJobs } from '../../../shared/extra-utils/server/jobs'
|
||||||
|
@ -23,8 +23,24 @@ const expect = chai.expect
|
||||||
|
|
||||||
describe('Test create transcoding jobs', function () {
|
describe('Test create transcoding jobs', function () {
|
||||||
let servers: ServerInfo[] = []
|
let servers: ServerInfo[] = []
|
||||||
let video1UUID: string
|
let videosUUID: string[] = []
|
||||||
let video2UUID: 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 () {
|
before(async function () {
|
||||||
this.timeout(60000)
|
this.timeout(60000)
|
||||||
|
@ -33,13 +49,14 @@ describe('Test create transcoding jobs', function () {
|
||||||
servers = await flushAndRunMultipleServers(2)
|
servers = await flushAndRunMultipleServers(2)
|
||||||
await setAccessTokensToServers(servers)
|
await setAccessTokensToServers(servers)
|
||||||
|
|
||||||
|
await updateCustomSubConfig(servers[0].url, servers[0].accessToken, config)
|
||||||
|
|
||||||
await doubleFollow(servers[0], servers[1])
|
await doubleFollow(servers[0], servers[1])
|
||||||
|
|
||||||
// Upload two videos for our needs
|
for (let i = 1; i <= 5; i++) {
|
||||||
const res1 = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video1' })
|
const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video' + i })
|
||||||
video1UUID = res1.body.video.uuid
|
videosUUID.push(res.body.video.uuid)
|
||||||
const res2 = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video2' })
|
}
|
||||||
video2UUID = res2.body.video.uuid
|
|
||||||
|
|
||||||
await waitJobs(servers)
|
await waitJobs(servers)
|
||||||
})
|
})
|
||||||
|
@ -50,12 +67,13 @@ describe('Test create transcoding jobs', function () {
|
||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
const res = await getVideosList(server.url)
|
const res = await getVideosList(server.url)
|
||||||
const videos = res.body.data
|
const videos = res.body.data
|
||||||
expect(videos).to.have.lengthOf(2)
|
expect(videos).to.have.lengthOf(videosUUID.length)
|
||||||
|
|
||||||
for (const video of videos) {
|
for (const video of videos) {
|
||||||
const res2 = await getVideo(server.url, video.uuid)
|
const res2 = await getVideo(server.url, video.uuid)
|
||||||
const videoDetail: VideoDetails = res2.body
|
const videoDetail: VideoDetails = res2.body
|
||||||
expect(videoDetail.files).to.have.lengthOf(1)
|
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)
|
this.timeout(60000)
|
||||||
|
|
||||||
const env = getEnvCli(servers[0])
|
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)
|
await waitJobs(servers)
|
||||||
|
|
||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
const res = await getVideosList(server.url)
|
const res = await getVideosList(server.url)
|
||||||
const videos = res.body.data
|
const videos = res.body.data
|
||||||
expect(videos).to.have.lengthOf(2)
|
|
||||||
|
|
||||||
let infoHashes: { [ id: number ]: string }
|
let infoHashes: { [ id: number ]: string }
|
||||||
|
|
||||||
|
@ -79,8 +96,9 @@ describe('Test create transcoding jobs', function () {
|
||||||
const res2 = await getVideo(server.url, video.uuid)
|
const res2 = await getVideo(server.url, video.uuid)
|
||||||
const videoDetail: VideoDetails = res2.body
|
const videoDetail: VideoDetails = res2.body
|
||||||
|
|
||||||
if (video.uuid === video2UUID) {
|
if (video.uuid === videosUUID[1]) {
|
||||||
expect(videoDetail.files).to.have.lengthOf(4)
|
expect(videoDetail.files).to.have.lengthOf(4)
|
||||||
|
expect(videoDetail.streamingPlaylists).to.have.lengthOf(0)
|
||||||
|
|
||||||
if (!infoHashes) {
|
if (!infoHashes) {
|
||||||
infoHashes = {}
|
infoHashes = {}
|
||||||
|
@ -96,6 +114,7 @@ describe('Test create transcoding jobs', function () {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
expect(videoDetail.files).to.have.lengthOf(1)
|
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)
|
this.timeout(60000)
|
||||||
|
|
||||||
const env = getEnvCli(servers[0])
|
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)
|
await waitJobs(servers)
|
||||||
|
|
||||||
for (const server of servers) {
|
for (const server of servers) {
|
||||||
const res = await getVideosList(server.url)
|
const res = await getVideosList(server.url)
|
||||||
const videos = res.body.data
|
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
|
const videoDetail: VideoDetails = res2.body
|
||||||
|
|
||||||
expect(videoDetail.files).to.have.lengthOf(2)
|
expect(videoDetail.files).to.have.lengthOf(2)
|
||||||
|
|
||||||
expect(videoDetail.files[0].resolution.id).to.equal(720)
|
expect(videoDetail.files[0].resolution.id).to.equal(720)
|
||||||
|
|
||||||
expect(videoDetail.files[1].resolution.id).to.equal(480)
|
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)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue