From cd57d6cbe351726b772736c8320383f63a89e520 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Fri, 27 Aug 2021 17:13:34 +0700 Subject: [PATCH] fix brokne mapInputStreamIndexToOutputIndex it would not take into account disabled streams --- src/hooks/useFfmpegOperations.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/hooks/useFfmpegOperations.js b/src/hooks/useFfmpegOperations.js index e86252a5..9d5601f9 100644 --- a/src/hooks/useFfmpegOperations.js +++ b/src/hooks/useFfmpegOperations.js @@ -85,15 +85,23 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) { const rotationArgs = rotation !== undefined ? ['-metadata:s:v:0', `rotate=${360 - rotation}`] : []; + // This function tries to calculate the output stream index needed for -metadata:s:x and -disposition:x arguments + // It is based on the assumption that copyFileStreamsFiltered contains the order of the input files (and their respective streams orders) sent to ffmpeg, to hopefully calculate the same output stream index values that ffmpeg does internally. + // It also takes into account previously added files that have been removed and disabled streams. function mapInputStreamIndexToOutputIndex(inputFilePath, inputFileStreamIndex) { let streamCount = 0; - const found = copyFileStreamsFiltered.find(({ path: path2, streamIds }) => { + // Count copied streams of all files until this input file + const foundFile = copyFileStreamsFiltered.find(({ path: path2, streamIds }) => { if (path2 === inputFilePath) return true; streamCount += streamIds.length; return false; }); - if (!found) return undefined; // Could happen if a tag has been edited on an external file, then the file was removed - return streamCount + inputFileStreamIndex; + if (!foundFile) return undefined; // Could happen if a tag has been edited on an external file, then the file was removed + + // Then add the index of the current stream index to the count + const copiedStreamIndex = foundFile.streamIds.indexOf(String(inputFileStreamIndex)); + if (copiedStreamIndex === -1) return undefined; // Could happen if a tag has been edited on a stream, but the stream is disabled + return streamCount + copiedStreamIndex; } const customTagsArgs = [