From 0adbc73eb9fe2017d5fd6b0200053871d72f3b14 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 12 Jun 2024 09:04:00 +0200 Subject: [PATCH] Fix HLS audio desync on some videos --- packages/ffmpeg/src/ffmpeg-vod.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/ffmpeg/src/ffmpeg-vod.ts b/packages/ffmpeg/src/ffmpeg-vod.ts index 5e6ef5da8..aec8888cb 100644 --- a/packages/ffmpeg/src/ffmpeg-vod.ts +++ b/packages/ffmpeg/src/ffmpeg-vod.ts @@ -112,8 +112,8 @@ export class FFmpegVOD { return this.ended } - private async buildWebVideoCommand (options: TranscodeVODOptions) { - const { resolution, fps, inputPath } = options + private async buildWebVideoCommand (options: TranscodeVODOptions & { canCopyAudio?: boolean, canCopyVideo?: boolean }) { + const { resolution, fps, inputPath, canCopyAudio = true, canCopyVideo = true } = options if (resolution === VideoResolution.H_NOVIDEO) { presetOnlyAudio(this.commandWrapper) @@ -136,8 +136,8 @@ export class FFmpegVOD { resolution, input: inputPath, - canCopyAudio: true, - canCopyVideo: true, + canCopyAudio, + canCopyVideo, fps, scaleFilterValue }) @@ -193,9 +193,15 @@ export class FFmpegVOD { const videoPath = this.getHLSVideoPath(options) - if (options.copyCodecs) presetCopy(this.commandWrapper) - else if (options.resolution === VideoResolution.H_NOVIDEO) presetOnlyAudio(this.commandWrapper) - else await this.buildWebVideoCommand(options) + if (options.copyCodecs) { + presetCopy(this.commandWrapper) + } else if (options.resolution === VideoResolution.H_NOVIDEO) { + presetOnlyAudio(this.commandWrapper) + } else { + // If we cannot copy codecs, we do not copy them at all to prevent issues like audio desync + // See for example https://github.com/Chocobozzz/PeerTube/issues/6438 + await this.buildWebVideoCommand({ ...options, canCopyAudio: false, canCopyVideo: false }) + } this.addCommonHLSVODCommandOptions(command, videoPath) }