Skip to content

The swiss army knife of lossless video/audio editing

make sure disposition is not forced to default for mkv

Mikael Finstad97b5840f6970
diff --git a/src/StreamsSelector.jsx b/src/StreamsSelector.jsx
index 5956bf79..f9434e2e 100644
--- a/src/StreamsSelector.jsx
+++ b/src/StreamsSelector.jsx
@@ -10,11 +10,13 @@ import { useTranslation } from 'react-i18next';
import { askForMetadataKey, showJson5Dialog } from './dialogs';
import { formatDuration } from './util/duration';
import { getStreamFps } from './ffmpeg';
+import { deleteDispositionValue } from './util';
const activeColor = '#429777';
const dispositionOptions = ['default', 'dub', 'original', 'comment', 'lyrics', 'karaoke', 'forced', 'hearing_impaired', 'visual_impaired', 'clean_effects', 'attached_pic', 'captions', 'descriptions', 'dependent', 'metadata'];
+const unchangedDispositionValue = 'llc_disposition_unchanged';
const TagEditor = memo(({ existingTags, customTags, onTagChange, onTagReset }) => {
const { t } = useTranslation();
@@ -125,10 +127,16 @@ const EditStreamDialog = memo(({ editingStream: { streamId: editingStreamId, pat
const customTags = useMemo(() => (customTagsByStreamId[editingFile] || {})[editingStreamId] || {}, [customTagsByStreamId, editingFile, editingStreamId]);
const customDisposition = useMemo(() => (dispositionByStreamId[editingFile] || {})[editingStreamId], [dispositionByStreamId, editingFile, editingStreamId]);
- const existingDisposition = useMemo(() => (stream && stream.disposition) || {}, [stream]);
- const effectiveDisposition = customDisposition || existingDisposition;
- const currentDisposition = (Object.entries(effectiveDisposition).find(([, value]) => value === 1) || [])[0];
- // console.log({ effectiveDisposition, currentDisposition });
+ const existingDispositionsObj = useMemo(() => (stream && stream.disposition) || {}, [stream]);
+ const effectiveDisposition = useMemo(() => {
+ if (customDisposition) return customDisposition;
+ if (!existingDispositionsObj) return undefined;
+ const existingActiveDispositionEntry = Object.entries(existingDispositionsObj).find(([, value]) => value === 1);
+ if (!existingActiveDispositionEntry) return undefined;
+ return existingActiveDispositionEntry[0]; // return the key
+ }, [customDisposition, existingDispositionsObj]);
+
+ // console.log({ existingDispositionsObj, effectiveDisposition });
const { t } = useTranslation();
@@ -159,18 +167,19 @@ const EditStreamDialog = memo(({ editingStream: { streamId: editingStreamId, pat
});
}, [editingFile, editingStreamId, setCustomTagsByStreamId]);
- const onCoverArtChange = useCallback((e) => {
- const newDispositions = dispositionOptions.includes(e.target.value) ? {
- [e.target.value]: 1,
- } : undefined;
-
- // console.log(newDispositions);
+ const onDispositionChange = useCallback((e) => {
+ let newDisposition;
+ if (dispositionOptions.includes(e.target.value)) {
+ newDisposition = e.target.value;
+ } else if (e.target.value === deleteDispositionValue) {
+ newDisposition = deleteDispositionValue; // needs a separate value (not a real disposition)
+ } // else unchanged (undefined)
setDispositionByStreamId((old) => ({
...old,
[editingFile]: {
...old[editingFile],
- [editingStreamId]: newDispositions,
+ [editingStreamId]: newDisposition,
},
}));
}, [editingFile, editingStreamId, setDispositionByStreamId]);
@@ -180,8 +189,10 @@ const EditStreamDialog = memo(({ editingStream: { streamId: editingStreamId, pat
return (
<>
<Heading marginBottom={5}>{t('Track disposition')}</Heading>
- <Select marginBottom={20} value={currentDisposition || ''} onChange={onCoverArtChange}>
- <option value="">{t('Unchanged')}</option>
+ <Select marginBottom={20} value={effectiveDisposition || unchangedDispositionValue} onChange={onDispositionChange}>
+ <option value={unchangedDispositionValue}>{t('Unchanged')}</option>
+ <option value={deleteDispositionValue}>{t('Remove')}</option>
+
{dispositionOptions.map((key) => (
<option key={key} value={key}>{key}</option>
))}
diff --git a/src/hooks/useFfmpegOperations.js b/src/hooks/useFfmpegOperations.js
index aeb94822..d5a4882c 100644
--- a/src/hooks/useFfmpegOperations.js
+++ b/src/hooks/useFfmpegOperations.js
@@ -4,7 +4,7 @@ import flatMapDeep from 'lodash/flatMapDeep';
import sum from 'lodash/sum';
import pMap from 'p-map';
-import { getOutPath, transferTimestamps, getOutFileExtension, getOutDir, isMac } from '../util';
+import { getOutPath, transferTimestamps, getOutFileExtension, getOutDir, isMac, deleteDispositionValue } from '../util';
import { isCuttingStart, isCuttingEnd, handleProgress, getFfCommandLine, getFfmpegPath, getDuration, runFfmpeg, createChaptersFromSegments } from '../ffmpeg';
const execa = window.require('execa');
@@ -40,6 +40,16 @@ function getMovFlags({ preserveMovData, movFastStart }) {
return flatMap(flags, flag => ['-movflags', flag]);
}
+function getMatroskaFlags() {
+ return [
+ '-default_mode', 'infer_no_subs',
+ // because it makes sense to not force subtitles disposition to "default" if they were not default in the input file
+ // after some testing, it seems that default is actually "infer", contrary to what is documented (ffmpeg doc says "passthrough" is default)
+ // https://ffmpeg.org/ffmpeg-formats.html#Options-8
+ // https://github.com/mifi/lossless-cut/issues/972#issuecomment-1015176316
+ ];
+}
+
function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
const optionalTransferTimestamps = useCallback(async (...args) => {
if (enableTransferTimestamps) await transferTimestamps(...args);
@@ -104,13 +114,17 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
return streamCount + copiedStreamIndex;
}
- // The structure is deep! file -> stream -> key -> value Example: { 'file.mp4': { 0: { key: 'value' } } }
- const deepMap = (root, fn) => flatMapDeep(
+ const lessDeepMap = (root, fn) => flatMapDeep(
Object.entries(root), ([path, streamsMap]) => (
- Object.entries(streamsMap || {}).map(([streamId, tagsMap]) => (
- Object.entries(tagsMap || {}).map(([key, value]) => fn(path, streamId, key, value))))),
+ Object.entries(streamsMap || {}).map(([streamId, value]) => (
+ fn(path, streamId, value)
+ ))),
);
+ // The structure is deep! file -> stream -> key -> value Example: { 'file.mp4': { 0: { key: 'value' } } }
+ const deepMap = (root, fn) => lessDeepMap(root, (path, streamId, tagsMap) => (
+ Object.entries(tagsMap || {}).map(([key, value]) => fn(path, streamId, key, value))));
+
const customTagsArgs = [
// Main file metadata:
...flatMap(Object.entries(customTagsByFile[filePath] || []), ([key, value]) => ['-metadata', `${key}=${value}`]),
@@ -124,11 +138,13 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
];
// Example: { 'file.mp4': { 0: { attached_pic: 1 } } }
- const customDispositionArgs = deepMap(dispositionByStreamId, (path, streamId, disposition, value) => {
- if (value !== 1) return [];
+ const customDispositionArgs = lessDeepMap(dispositionByStreamId, (path, streamId, disposition) => {
+ if (disposition == null) return [];
const outputIndex = mapInputStreamIndexToOutputIndex(path, parseInt(streamId, 10));
if (outputIndex == null) return [];
- return [`-disposition:${outputIndex}`, String(disposition)];
+ // 0 means delete the disposition for this stream
+ const dispositionArg = disposition === deleteDispositionValue ? '0' : disposition;
+ return [`-disposition:${outputIndex}`, String(dispositionArg)];
});
const ffmpegArgs = [
@@ -146,6 +162,7 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
'-map_metadata', '0',
...getMovFlags({ preserveMovData, movFastStart }),
+ ...getMatroskaFlags(),
...customTagsArgs,
@@ -210,6 +227,8 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
const ffmetadataPath = await writeChaptersFfmetadata(outDir, chapters);
+ const shouldMapMetadata = preserveMetadataOnMerge && allStreams;
+
try {
// Keep this similar to cutSingle()
const ffmpegArgs = [
@@ -222,7 +241,7 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
'-i', '-',
// Add the first file for using its metadata. Can only do this if allStreams (-map 0) is set, or else ffmpeg might output this input instead of the concat
- ...(preserveMetadataOnMerge && allStreams ? ['-i', paths[0]] : []),
+ ...(shouldMapMetadata ? ['-i', paths[0]] : []),
// Chapters?
...(ffmetadataPath ? ['-f', 'ffmetadata', '-i', ffmetadataPath] : []),
@@ -234,9 +253,10 @@ function useFfmpegOperations({ filePath, enableTransferTimestamps }) {
// -map_metadata 0 with concat demuxer doesn't transfer metadata from the concat'ed files when merging.
// So we use the first file file (index 1) for metadata
// Can only do this if allStreams (-map 0) is set
- ...(preserveMetadataOnMerge && allStreams ? ['-map_metadata', '1'] : []),
+ ...(shouldMapMetadata ? ['-map_metadata', '1'] : []),
...getMovFlags({ preserveMovData, movFastStart }),
+ ...getMatroskaFlags(),
// See https://github.com/mifi/lossless-cut/issues/170
'-ignore_unknown',
diff --git a/src/util.js b/src/util.js
index c27ebf54..24474eec 100644
--- a/src/util.js
+++ b/src/util.js
@@ -284,4 +284,6 @@ export async function deleteFiles({ toDelete, paths: { previewFilePath, filePath
}
}
+export const deleteDispositionValue = 'llc_disposition_remove';
+
export const mirrorTransform = 'matrix(-1, 0, 0, 1, 0, 0)';