From c7f36e4f485d92191b9d04c6a880724b02fa220b Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 29 Jan 2020 16:54:03 +0100 Subject: Fix transcoding --- .gitignore | 1 + server/helpers/ffmpeg-utils.ts | 13 ++++++------- server/tests/api/videos/video-transcoder.ts | 16 ++++++++++++---- shared/extra-utils/miscs/miscs.ts | 2 +- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 6ef90385c..07b2fb7ef 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ yarn-error.log /test5/ /test6/ /server/tests/fixtures/video_high_bitrate_1080p.mp4 +/server/tests/fixtures/video_59fps.mp4 # Production /storage/ diff --git a/server/helpers/ffmpeg-utils.ts b/server/helpers/ffmpeg-utils.ts index 63dc5b6a3..7022d3e03 100644 --- a/server/helpers/ffmpeg-utils.ts +++ b/server/helpers/ffmpeg-utils.ts @@ -263,8 +263,9 @@ async function canDoQuickTranscode (path: string): Promise { return true } -function getClosestFramerateStandard (fps: number, hd = false): number { - return VIDEO_TRANSCODING_FPS[hd ? 'HD_STANDARD' : 'STANDARD'].slice(0).sort((a, b) => fps % a - fps % b)[0] +function getClosestFramerateStandard (fps: number, type: 'HD_STANDARD' | 'STANDARD'): number { + return VIDEO_TRANSCODING_FPS[type].slice(0) + .sort((a, b) => fps % a - fps % b)[0] } // --------------------------------------------------------------------------- @@ -294,12 +295,10 @@ async function buildx264Command (command: ffmpeg.FfmpegCommand, options: Transco // On small/medium resolutions, limit FPS options.resolution !== undefined && options.resolution < VIDEO_TRANSCODING_FPS.KEEP_ORIGIN_FPS_RESOLUTION_MIN && - fps > VIDEO_TRANSCODING_FPS.AVERAGE || - // If the video is doesn't match hd standard - !VIDEO_TRANSCODING_FPS.HD_STANDARD.some(value => fps % value === 0) + fps > VIDEO_TRANSCODING_FPS.AVERAGE ) { // Get closest standard framerate by modulo: downsampling has to be done to a divisor of the nominal fps value - fps = getClosestFramerateStandard(fps) + fps = getClosestFramerateStandard(fps, 'STANDARD') } command = await presetH264(command, options.inputPath, options.resolution, fps) @@ -312,7 +311,7 @@ async function buildx264Command (command: ffmpeg.FfmpegCommand, options: Transco if (fps) { // Hard FPS limits - if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, true) + if (fps > VIDEO_TRANSCODING_FPS.MAX) fps = getClosestFramerateStandard(fps, 'HD_STANDARD') else if (fps < VIDEO_TRANSCODING_FPS.MIN) fps = VIDEO_TRANSCODING_FPS.MIN command = command.withFPS(fps) diff --git a/server/tests/api/videos/video-transcoder.ts b/server/tests/api/videos/video-transcoder.ts index 0104c94fc..55eb76b3b 100644 --- a/server/tests/api/videos/video-transcoder.ts +++ b/server/tests/api/videos/video-transcoder.ts @@ -423,7 +423,7 @@ describe('Test video transcoding', function () { let tempFixturePath: string { - tempFixturePath = await generateVideoWithFramerate() + tempFixturePath = await generateVideoWithFramerate(59) const fps = await getVideoFileFPS(tempFixturePath) expect(fps).to.be.equal(59) @@ -443,10 +443,18 @@ describe('Test video transcoding', function () { const res = await getVideosList(server.url) const video = res.body.data.find(v => v.name === videoAttributes.name) - const path = join(root(), 'test' + servers[1].internalServerNumber, 'videos', video.uuid + '-240.mp4') - const fps = await getVideoFileFPS(path) - expect(fps).to.be.equal(25) + { + const path = join(root(), 'test' + servers[ 1 ].internalServerNumber, 'videos', video.uuid + '-240.mp4') + const fps = await getVideoFileFPS(path) + expect(fps).to.be.equal(25) + } + + { + const path = join(root(), 'test' + servers[ 1 ].internalServerNumber, 'videos', video.uuid + '-720.mp4') + const fps = await getVideoFileFPS(path) + expect(fps).to.be.equal(59) + } } }) diff --git a/shared/extra-utils/miscs/miscs.ts b/shared/extra-utils/miscs/miscs.ts index c957a6abe..d04003988 100644 --- a/shared/extra-utils/miscs/miscs.ts +++ b/shared/extra-utils/miscs/miscs.ts @@ -113,7 +113,7 @@ async function generateVideoWithFramerate (fps = 60) { if (!exists) { return new Promise(async (res, rej) => { ffmpeg() - .outputOptions([ '-f rawvideo', '-video_size 320x240', '-i /dev/urandom' ]) + .outputOptions([ '-f rawvideo', '-video_size 1280x720', '-i /dev/urandom' ]) .outputOptions([ '-ac 2', '-f s16le', '-i /dev/urandom', '-t 10' ]) .outputOptions([ `-r ${fps}` ]) .output(tempFixturePath) -- cgit v1.2.3