Skip to content

The swiss army knife of lossless video/audio editing

Address review of copying metadata from another track/file

Claudec01a3771475a
diff --git a/src/renderer/src/StreamsSelector.tsx b/src/renderer/src/StreamsSelector.tsx
index f722bd5e..31315793 100644
--- a/src/renderer/src/StreamsSelector.tsx
+++ b/src/renderer/src/StreamsSelector.tsx
@@ -30,12 +30,12 @@ import GpsMap from './components/GpsMap';
import Switch from './components/Switch';
-const getFileName = (path: string) => path.replace(/.*\/([^/]+)$/, '$1');
+const { basename } = window.require('node:path');
// for "Copy from": the source's metadata including the user's changes, without muxer-written keys
-function getCopyFromSource(label: string, existing: Record<string, string> | undefined, custom: Record<string, string> | undefined) {
+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 ? { label, metadata } : undefined;
+ return Object.keys(metadata).length > 0 ? { key, label: getLabel(metadata), metadata } : [];
}
type UpdateStreamParams = (fileId: string, streamId: number, setter: (a: StreamParams) => void) => void;
@@ -266,7 +266,7 @@ const EditFileDialog = memo(({ editingFile, allFilesMeta, paramsByFile, setParam
}, [updateFileParams]);
const copyFromSources = useMemo(() => Object.entries(allFilesMeta).flatMap(([path, { format: otherFormat }]) => (
- path !== editingFile ? getCopyFromSource(getFileName(path), otherFormat.tags, 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();
@@ -357,10 +357,12 @@ const EditStreamDialog = memo(({ editingStream: { streamId: editingStreamId, pat
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 [];
- const custom = getStreamParams({ paramsByFile, filePath: path, streamId: s.index })?.metadata;
- const title = custom?.['title'] ?? s.tags?.title;
- const label = `${multipleFiles ? `${getFileName(path)}: ` : ''}#${s.index + 1} ${s.codec_type}${title ? ` – ${title}` : ''}`;
- return getCopyFromSource(label, s.tags, custom) ?? [];
+ 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]);
@@ -678,7 +680,7 @@ function FileHeading({ path, format, onEditClick, toggleCopyAllStreams, chapters
return (
<div style={{ display: 'flex', marginBottom: '.2em', borderBottom: '1px solid var(--gray-7)' }}>
- <div title={path} style={{ wordBreak: 'break-all', marginRight: '1em', fontWeight: 'bold' }}>{getFileName(path)}</div>
+ <div title={path} style={{ wordBreak: 'break-all', marginRight: '1em', fontWeight: 'bold' }}>{basename(path)}</div>
<div style={{ flexGrow: 1 }} />
diff --git a/src/renderer/src/components/TagEditor.tsx b/src/renderer/src/components/TagEditor.tsx
index 9fb63d4f..74dfb8f2 100644
--- a/src/renderer/src/components/TagEditor.tsx
+++ b/src/renderer/src/components/TagEditor.tsx
@@ -51,7 +51,7 @@ function TagEditor({ existingTags = emptyObject, customTags = emptyObject, editi
onApplyToAll?: ((tag: string, value: string) => void) | undefined,
applyToAllTitle?: string,
// other tracks/files to copy metadata from
- copyFromSources?: { label: string, metadata: Record<string, string> }[],
+ copyFromSources?: { key: string, label: string, metadata: Record<string, string> }[],
}) {
const { t } = useTranslation();
const editRef = useRef<HTMLInputElement & HTMLTextAreaElement>(null);
@@ -89,6 +89,11 @@ function TagEditor({ existingTags = emptyObject, customTags = emptyObject, editi
}
}, [onTagsChange]);
+ // only the keys that differ, so unchanged tags aren't shown as edited
+ const onCopyFrom = useCallback((metadata: Record<string, string>) => {
+ onTagsChange(Object.fromEntries(Object.entries(metadata).filter(([key, value]) => effectiveTags[key] !== value)));
+ }, [effectiveTags, onTagsChange]);
+
const saveTag = useCallback(() => {
invariant(editingTag != null);
invariant(editingTagVal != null);
@@ -254,16 +259,16 @@ function TagEditor({ existingTags = emptyObject, customTags = emptyObject, editi
{({ onClick }) => <Button color="gray" size="1" onClick={onClick}><FaClipboardList />{t('Copy to clipboard')}</Button>}
</CopyClipboardButton>
- <Button color="gray" size="1" onClick={onPasteClick} style={{ marginRight: '.3em' }}><FaClipboard />{t('Paste')}</Button>
+ <Button color="gray" size="1" disabled={editingTag != null} onClick={onPasteClick} style={{ marginRight: '.3em' }}><FaClipboard />{t('Paste')}</Button>
{copyFromSources.length > 0 && (
<DropdownMenu.Root>
- <DropdownMenu.Trigger>
+ <DropdownMenu.Trigger disabled={editingTag != null}>
<Button color="gray" size="1"><FaCopy />{t('Copy from')}<FaChevronDown /></Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content size="1" style={{ maxHeight: '20em' }}>
- {copyFromSources.map(({ label, metadata }) => (
- <DropdownMenu.Item key={label} onSelect={() => onTagsChange(metadata)}>{label}</DropdownMenu.Item>
+ {copyFromSources.map(({ key, label, metadata }) => (
+ <DropdownMenu.Item key={key} onSelect={() => onCopyFrom(metadata)}>{label}</DropdownMenu.Item>
))}
</DropdownMenu.Content>
</DropdownMenu.Root>
diff --git a/src/renderer/src/util/metadata.test.ts b/src/renderer/src/util/metadata.test.ts
index cb170797..ba7f6cdb 100644
--- a/src/renderer/src/util/metadata.test.ts
+++ b/src/renderer/src/util/metadata.test.ts
@@ -66,5 +66,7 @@ test('getCopyableMetadata', () => {
_STATISTICS_WRITING_APP: 'mkvmerge',
encoder: 'Lavf',
major_brand: 'isom',
+ handler_name: 'SoundHandler',
+ iTunSMPB: ' 00000000 00000840',
})).toEqual({ title: 'a', language: 'eng' });
});
diff --git a/src/renderer/src/util/metadata.ts b/src/renderer/src/util/metadata.ts
index f9832439..f096dc5e 100644
--- a/src/renderer/src/util/metadata.ts
+++ b/src/renderer/src/util/metadata.ts
@@ -22,8 +22,9 @@ export const getEffectiveMetadata = (existing: Record<string, string> | undefine
Object.fromEntries(Object.entries({ ...existing, ...custom }).filter(([, value]) => value !== ''))
);
-// Keys that describe the source's own data (written by the muxer), so they shouldn't be copied to another track/file
-const nonCopyableMetadataKeyRegex = /^(encoder|major_brand|minor_version|compatible_brands|(DURATION|NUMBER_OF_FRAMES|NUMBER_OF_BYTES|BPS|_STATISTICS_[A-Z_]+)(-\w+)?)$/i;
+// Keys that describe the source's own data (mostly written by the muxer/encoder), so they shouldn't be copied to another track/file.
+// handler_name is excluded too, because it's usually a default like "SoundHandler" (and is used to detect e.g. GPS tracks)
+const nonCopyableMetadataKeyRegex = /^(encoder|major_brand|minor_version|compatible_brands|handler_name|vendor_id|timecode|iTunSMPB|iTunNORM|(DURATION|NUMBER_OF_FRAMES|NUMBER_OF_BYTES|BPS|ENCODER|ENCODER_SETTINGS|_STATISTICS_[A-Z_]+)(-\w+)?)$/i;
export const getCopyableMetadata = (metadata: Record<string, string>) => (
Object.fromEntries(Object.entries(metadata).filter(([key]) => !nonCopyableMetadataKeyRegex.test(key)))