Skip to content

The swiss army knife of lossless video/audio editing

specify copy per stream mapping

Mikael Finstad7c28d660251c
diff --git a/src/hooks/useFfmpegOperations.js b/src/hooks/useFfmpegOperations.js
index 23e49391..d1c7fd9e 100644
--- a/src/hooks/useFfmpegOperations.js
+++ b/src/hooks/useFfmpegOperations.js
@@ -174,16 +174,14 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
...inputArgs,
- '-c', 'copy',
-
- ...(shortestFlag ? ['-shortest'] : []),
-
...mapStreamsArgs,
'-map_metadata', '0',
...(chaptersPath ? ['-map_chapters', chaptersInputIndex] : []),
+ ...(shortestFlag ? ['-shortest'] : []),
+
...getMovFlags({ preserveMovData, movFastStart }),
...getMatroskaFlags(),
@@ -306,8 +304,6 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
...inputArgs,
- '-c', 'copy',
-
...mapStreamsArgs,
// -map_metadata 0 with concat demuxer doesn't transfer metadata from the concat'ed file input (index 0) when merging.
diff --git a/src/util/streams.js b/src/util/streams.js
index d9bf7b37..f8d5e627 100644
--- a/src/util/streams.js
+++ b/src/util/streams.js
@@ -102,7 +102,9 @@ export function getActiveDisposition(disposition) {
return existingActiveDispositionEntry[0]; // return the key
}
-function getPerStreamQuirksFlags({ stream, outputIndex, outFormat, manuallyCopyDisposition = false }) {
+function getPerStreamFlags({ stream, outputIndex, outFormat, manuallyCopyDisposition = false }) {
+ let outCodec = 'copy';
+
let args = [];
if (['mov', 'mp4'].includes(outFormat)) {
if (stream.codec_tag === '0x0000' && stream.codec_name === 'hevc') {
@@ -112,7 +114,7 @@ function getPerStreamQuirksFlags({ stream, outputIndex, outFormat, manuallyCopyD
// mp4/mov only supports mov_text, so convert it https://stackoverflow.com/a/17584272/6519037
// https://github.com/mifi/lossless-cut/issues/418
if (stream.codec_type === 'subtitle' && stream.codec_name !== 'mov_text') {
- args = [...args, `-c:${outputIndex}`, 'mov_text'];
+ outCodec = 'mov_text';
}
}
@@ -121,14 +123,14 @@ function getPerStreamQuirksFlags({ stream, outputIndex, outFormat, manuallyCopyD
// https://github.com/mifi/lossless-cut/issues/418
// https://www.reddit.com/r/PleX/comments/bcfvev/can_someone_eli5_subtitles/
if (stream.codec_type === 'subtitle' && stream.codec_name === 'mov_text') {
- args = [...args, `-c:${outputIndex}`, 'srt'];
+ outCodec = 'srt';
}
}
if (outFormat === 'webm') {
// Only WebVTT subtitles are supported for WebM.
if (stream.codec_type === 'subtitle' && stream.codec_name === 'mov_text') {
- args = [...args, `-c:${outputIndex}`, 'webvtt'];
+ outCodec = 'webvtt';
}
}
@@ -138,7 +140,7 @@ function getPerStreamQuirksFlags({ stream, outputIndex, outFormat, manuallyCopyD
// https://github.com/mifi/lossless-cut/issues/476
// ffmpeg cannot encode pcm_bluray
if (outFormat !== 'mpegts' && stream.codec_type === 'audio' && stream.codec_name === 'pcm_bluray') {
- args = [...args, `-c:${outputIndex}`, 'pcm_s24le'];
+ outCodec = 'pcm_s24le';
}
// when concat'ing, disposition doesn't seem to get automatically transferred by ffmpeg, so we must do it manually
@@ -149,10 +151,11 @@ function getPerStreamQuirksFlags({ stream, outputIndex, outFormat, manuallyCopyD
}
}
+ args = [...args, `-c:${outputIndex}`, outCodec];
+
return args;
}
-// eslint-disable-next-line import/prefer-default-export
export function getMapStreamsArgs({ outFormat, allFilesMeta, copyFileStreams, manuallyCopyDisposition }) {
let args = [];
let outputIndex = 0;
@@ -164,7 +167,7 @@ export function getMapStreamsArgs({ outFormat, allFilesMeta, copyFileStreams, ma
args = [
...args,
'-map', `${fileIndex}:${streamId}`,
- ...getPerStreamQuirksFlags({ stream, outputIndex, outFormat, manuallyCopyDisposition }),
+ ...getPerStreamFlags({ stream, outputIndex, outFormat, manuallyCopyDisposition }),
];
outputIndex += 1;
});
diff --git a/src/util/streams.test.js b/src/util/streams.test.js
index 19677c6f..fe24356b 100644
--- a/src/util/streams.test.js
+++ b/src/util/streams.test.js
@@ -24,13 +24,13 @@ test('getMapStreamsArgs', () => {
copyFileStreams: [{ path, streamIds: streams1.map((stream) => stream.index) }],
outFormat,
})).toEqual([
- '-map', '0:0',
- '-map', '0:1',
- '-map', '0:2',
- '-map', '0:3', '-tag:3', 'hvc1',
- '-map', '0:4',
- '-map', '0:5',
- '-map', '0:6',
+ '-map', '0:0', '-c:0', 'copy',
+ '-map', '0:1', '-c:1', 'copy',
+ '-map', '0:2', '-c:2', 'copy',
+ '-map', '0:3', '-tag:3', 'hvc1', '-c:3', 'copy',
+ '-map', '0:4', '-c:4', 'copy',
+ '-map', '0:5', '-c:5', 'copy',
+ '-map', '0:6', '-c:6', 'copy',
'-map', '0:7', '-c:7', 'mov_text',
]);
});
@@ -64,6 +64,7 @@ test('getMapStreamsArgs, disposition', () => {
})).toEqual([
'-map', '0:0',
'-disposition:0', 'attached_pic',
+ '-c:0', 'copy',
]);
});