Skip to content

The swiss army knife of lossless video/audio editing

Address third review of copying metadata

Claude956d13070b76
diff --git a/src/renderer/src/StreamsSelector.tsx b/src/renderer/src/StreamsSelector.tsx
index ab6170a1..0567c69b 100644
--- a/src/renderer/src/StreamsSelector.tsx
+++ b/src/renderer/src/StreamsSelector.tsx
@@ -35,7 +35,7 @@ import { basename } from './util/path';
// for "Copy from": the source's metadata including the user's changes, without muxer-written keys
function getCopyFromSource({ key, getLabel, existing, custom }: { key: string, getLabel: (metadata: Record<string, string>) => string, existing: Record<string, string> | undefined, custom: Record<string, string> | undefined }) {
const metadata = getCopyableMetadata(getEffectiveMetadata(existing, custom));
- return Object.keys(metadata).length > 0 ? { key, label: getLabel(metadata), metadata } : [];
+ return Object.keys(metadata).length > 0 ? { key, label: getLabel(metadata), metadata } : undefined;
}
type ResetParams = (path: string, streamId?: number) => void;
@@ -292,7 +292,7 @@ const EditFileDialog = memo(({ editingFile, allFilesMeta, paramsByFile, setParam
}, [updateFileParams]);
const copyFromSources = useMemo(() => Object.entries(allFilesMeta).flatMap(([path, { format: otherFormat }]) => (
- path !== editingFile ? getCopyFromSource({ key: path, getLabel: () => basename(path), existing: otherFormat.tags, custom: paramsByFile.get(path)?.metadata }) : []
+ (path !== editingFile && getCopyFromSource({ key: path, getLabel: () => basename(path), existing: otherFormat.tags, custom: paramsByFile.get(path)?.metadata })) || []
)), [allFilesMeta, editingFile, paramsByFile]);
const commonTagInfo = useCommonTagInfo();
@@ -383,13 +383,14 @@ const EditStreamDialog = memo(({ editingStream: { streamId: editingStreamId, pat
const copyFromSources = useMemo(() => {
const multipleFiles = Object.keys(allFilesMeta).length > 1;
return Object.entries(allFilesMeta).flatMap(([path, { streams: fileStreams }]) => fileStreams.flatMap((s) => {
- if (path === editingFile && s.index === editingStreamId) return [];
+ // attachments only have metadata describing the attached file itself
+ if ((path === editingFile && s.index === editingStreamId) || s.codec_type === 'attachment') return [];
return getCopyFromSource({
key: `${path}:${s.index}`,
getLabel: ({ title }) => `${multipleFiles ? `${basename(path)}: ` : ''}#${s.index + 1} ${s.codec_type}${title ? ` – ${title}` : ''}`,
existing: s.tags,
custom: getStreamParams({ paramsByFile, filePath: path, streamId: s.index })?.metadata,
- });
+ }) ?? [];
}));
}, [allFilesMeta, editingFile, editingStreamId, paramsByFile]);
diff --git a/src/renderer/src/components/TagEditor.tsx b/src/renderer/src/components/TagEditor.tsx
index 74dfb8f2..5111730f 100644
--- a/src/renderer/src/components/TagEditor.tsx
+++ b/src/renderer/src/components/TagEditor.tsx
@@ -89,10 +89,13 @@ function TagEditor({ existingTags = emptyObject, customTags = emptyObject, editi
}
}, [onTagsChange]);
- // only the keys that differ, so unchanged tags aren't shown as edited
+ // Only the keys that differ, so unchanged tags aren't shown as edited.
+ // A copied value equal to the original resets the user's edit instead
const onCopyFrom = useCallback((metadata: Record<string, string>) => {
- onTagsChange(Object.fromEntries(Object.entries(metadata).filter(([key, value]) => effectiveTags[key] !== value)));
- }, [effectiveTags, onTagsChange]);
+ const changes = Object.entries(metadata).filter(([key, value]) => effectiveTags[key] !== value);
+ changes.filter(([key, value]) => existingTags[key] === value).forEach(([key]) => onTagReset(key));
+ onTagsChange(Object.fromEntries(changes.filter(([key, value]) => existingTags[key] !== value)));
+ }, [effectiveTags, existingTags, onTagReset, onTagsChange]);
const saveTag = useCallback(() => {
invariant(editingTag != null);
diff --git a/src/renderer/src/util/metadata.test.ts b/src/renderer/src/util/metadata.test.ts
index ba7f6cdb..0c1566f9 100644
--- a/src/renderer/src/util/metadata.test.ts
+++ b/src/renderer/src/util/metadata.test.ts
@@ -68,5 +68,9 @@ test('getCopyableMetadata', () => {
major_brand: 'isom',
handler_name: 'SoundHandler',
iTunSMPB: ' 00000000 00000840',
+ creation_time: '2024-01-01T00:00:00.000000Z',
+ filename: 'font.ttf',
})).toEqual({ title: 'a', language: 'eng' });
+ expect(getCopyableMetadata({ handler_name: 'English Commentary' })).toEqual({ handler_name: 'English Commentary' });
+ expect(getCopyableMetadata({ handler_name: '\u0010DJI.Subtitle' })).toEqual({});
});
diff --git a/src/renderer/src/util/metadata.ts b/src/renderer/src/util/metadata.ts
index 1ace3d52..da2cfdcc 100644
--- a/src/renderer/src/util/metadata.ts
+++ b/src/renderer/src/util/metadata.ts
@@ -22,22 +22,27 @@ export const getEffectiveMetadata = (existing: Record<string, string> | undefine
Object.fromEntries(Object.entries({ ...existing, ...custom }).filter(([, value]) => value !== ''))
);
-// Keys written by the muxer/encoder, describing the file/track's own data
-const muxerWrittenMetadataKeys = ['encoder', 'major_brand', 'minor_version', 'compatible_brands', 'vendor_id', 'timecode', 'iTunSMPB', 'iTunNORM'] as const;
+// Keys written by the muxer/encoder (or describing an attachment), describing the file/track's own data.
+// creation_time is included, because copying it would give the output another recording's date
+const muxerWrittenMetadataKeys = ['encoder', 'major_brand', 'minor_version', 'compatible_brands', 'vendor_id', 'timecode', 'iTunSMPB', 'iTunNORM', 'creation_time', 'filename', 'mimetype'] as const;
// Matroska track statistics, optionally with a language suffix (e.g. BPS-eng), plus any _STATISTICS_* key
const matroskaStatisticsKeys = ['DURATION', 'NUMBER_OF_FRAMES', 'NUMBER_OF_BYTES', 'BPS', 'ENCODER', 'ENCODER_SETTINGS'] as const;
-// handler_name is usually a default like "SoundHandler" (and is used to detect e.g. GPS tracks), so don't copy it either
-const nonCopyableMetadataKeys = new Set<string>([...muxerWrittenMetadataKeys, 'handler_name', ...matroskaStatisticsKeys].map((key) => key.toLowerCase()));
+const nonCopyableMetadataKeys = new Set<string>([...muxerWrittenMetadataKeys, ...matroskaStatisticsKeys].map((key) => key.toLowerCase()));
-const isCopyableMetadataKey = (key: string) => {
- const [baseKey = key] = key.toLowerCase().split('-');
+// handler_name is often the track name in MP4/MOV, but is usually a muxer default like "SoundHandler",
+// "Core Media Video" or "\u0010DJI.Subtitle" (used to detect GPS tracks), which shouldn't be copied
+const isDefaultHandlerName = (value: string) => /handler|core media|iso media|dji\./i.test(value);
+
+const isCopyableMetadata = (key: string, value: string) => {
+ const baseKey = key.toLowerCase().split('-')[0]!;
+ if (baseKey === 'handler_name') return !isDefaultHandlerName(value);
return !nonCopyableMetadataKeys.has(baseKey) && !baseKey.startsWith('_statistics_');
};
// The source's metadata without keys that describe its own data, so it can be copied to another track/file
export const getCopyableMetadata = (metadata: Record<string, string>) => (
- Object.fromEntries(Object.entries(metadata).filter(([key]) => isCopyableMetadataKey(key)))
+ Object.fromEntries(Object.entries(metadata).filter(([key, value]) => isCopyableMetadata(key, value)))
);
// Common codec tags (FourCC) for use with `-tag` when outputting MP4/MOV