The swiss army knife of lossless video/audio editing
check file compatibility in concat dialog
Mikael Finstadfe897b5f4beb
diff --git a/src/App.jsx b/src/App.jsxindex d9b76ac6..482e5ace 100644--- a/src/App.jsx+++ b/src/App.jsx@@ -2566,7 +2566,7 @@ const App = memo(() => {</Table></Sheet>- <ConcatDialog isShown={batchFiles.length > 0 && concatDialogVisible} onHide={() => setConcatDialogVisible(false)} initialPaths={batchFilePaths} onConcat={userConcatFiles} setAlwaysConcatMultipleFiles={setAlwaysConcatMultipleFiles} alwaysConcatMultipleFiles={alwaysConcatMultipleFiles} />+ <ConcatDialog isShown={batchFiles.length > 0 && concatDialogVisible} onHide={() => setConcatDialogVisible(false)} paths={batchFilePaths} onConcat={userConcatFiles} setAlwaysConcatMultipleFiles={setAlwaysConcatMultipleFiles} alwaysConcatMultipleFiles={alwaysConcatMultipleFiles} /><KeyboardShortcuts isShown={keyboardShortcutsVisible} onHide={() => setKeyboardShortcutsVisible(false)} keyBindings={keyBindings} setKeyBindings={setKeyBindings} currentCutSeg={currentCutSeg} resetKeyBindings={resetKeyBindings} /></div>diff --git a/src/components/ConcatDialog.jsx b/src/components/ConcatDialog.jsxindex ad43f97f..9c5de0ef 100644--- a/src/components/ConcatDialog.jsx+++ b/src/components/ConcatDialog.jsx@@ -1,12 +1,15 @@import React, { memo, useState, useCallback, useEffect, useMemo } from 'react';import { useTranslation } from 'react-i18next';-import { Dialog, Checkbox, Button, Paragraph, CogIcon } from 'evergreen-ui';+import { Alert, Checkbox, Dialog, Button, Paragraph } from 'evergreen-ui';import { AiOutlineMergeCells } from 'react-icons/ai';+import { FaQuestionCircle, FaCheckCircle, FaExclamationTriangle } from 'react-icons/fa';+import i18n from 'i18next';import { readFileMeta, getSmarterOutFormat } from '../ffmpeg';import useFileFormatState from '../hooks/useFileFormatState';import OutputFormatSelect from './OutputFormatSelect';import useUserSettings from '../hooks/useUserSettings';+import { isMov } from '../util/streams';const { basename } = window.require('path');@@ -17,29 +20,31 @@ const rowStyle = {};const ConcatDialog = memo(({- isShown, onHide, initialPaths, onConcat,+ isShown, onHide, paths, onConcat,alwaysConcatMultipleFiles, setAlwaysConcatMultipleFiles,}) => {const { t } = useTranslation();const { preserveMovData, setPreserveMovData, segmentsToChapters, setSegmentsToChapters, preserveMetadataOnMerge, setPreserveMetadataOnMerge } = useUserSettings();- const [paths, setPaths] = useState(initialPaths);const [includeAllStreams, setIncludeAllStreams] = useState(false);const [fileMeta, setFileMeta] = useState();+ const [allFilesMetaCache, setAllFilesMetaCache] = useState({});const [clearBatchFilesAfterConcat, setClearBatchFilesAfterConcat] = useState(false);const [settingsVisible, setSettingsVisible] = useState(false);+ const [enableReadFileMeta, setEnableReadFileMeta] = useState(false);const { fileFormat, setFileFormat, detectedFileFormat, setDetectedFileFormat, isCustomFormatSelected } = useFileFormatState();const firstPath = useMemo(() => {- if (initialPaths.length === 0) return undefined;- return initialPaths[0];- }, [initialPaths]);+ if (paths.length === 0) return undefined;+ return paths[0];+ }, [paths]);useEffect(() => {if (!isShown) return undefined;let aborted = false;+(async () => {setFileMeta();setFileFormat();@@ -51,14 +56,67 @@ const ConcatDialog = memo(({setFileFormat(fileFormatNew);setDetectedFileFormat(fileFormatNew);})().catch(console.error);+return () => {aborted = true;};}, [firstPath, isShown, setDetectedFileFormat, setFileFormat]);+ const allFilesMeta = useMemo(() => {+ if (paths.length === 0) return undefined;+ const filtered = paths.map((path) => [path, allFilesMetaCache[path]]).filter(([, it]) => it);+ return filtered.length === paths.length ? filtered : undefined;+ }, [allFilesMetaCache, paths]);++ const problemsByFile = useMemo(() => {+ if (!allFilesMeta) return [];+ const allFilesMetaExceptFirstFile = allFilesMeta.slice(1);+ const [, firstFileMeta] = allFilesMeta[0];+ const errors = [];+ allFilesMetaExceptFirstFile.forEach(([path, { streams }]) => {+ streams.some((stream, i) => {+ const referenceStream = firstFileMeta.streams[i];+ if (!referenceStream) {+ errors.push([path, i18n.t('Extraneous track {{index}}', { index: stream.index })]);+ return true;+ }+ // check all these parameters+ ['codec_name', 'width', 'height', 'fps', 'pix_fmt', 'level', 'profile', 'sample_fmt', 'r_frame_rate', 'time_base'].some((key) => {+ const val = stream[key];+ const referenceVal = referenceStream[key];+ if (val !== referenceVal) {+ errors.push([path, i18n.t('Track {{index}} mismatch: {{key1}} {{value1}} != {{value2}}', { index: stream.index, key1: key, value1: val, value2: referenceVal })]);+ return true;+ }+ return false;+ });+ return false;+ });+ });+ return Object.fromEntries(errors);+ }, [allFilesMeta]);+useEffect(() => {- setPaths(initialPaths);- }, [initialPaths]);+ if (!isShown || !enableReadFileMeta) return undefined;++ let aborted = false;++ (async () => {+ // eslint-disable-next-line no-restricted-syntax+ for (const path of paths) {+ if (aborted) return;+ if (!allFilesMetaCache[path]) {+ // eslint-disable-next-line no-await-in-loop+ const fileMetaNew = await readFileMeta(path);+ setAllFilesMetaCache((existing) => ({ ...existing, [path]: fileMetaNew }));+ }+ }+ })().catch(console.error);++ return () => {+ aborted = true;+ };+ }, [allFilesMetaCache, enableReadFileMeta, isShown, paths]);const onOutputFormatUserChange = useCallback((newFormat) => setFileFormat(newFormat), [setFileFormat]);@@ -73,12 +131,16 @@ const ConcatDialog = memo(({topOffset="3vh"width="90vw"footer={(- <>- <Button iconBefore={CogIcon} onClick={() => setSettingsVisible(true)}>{t('Options')}</Button>- {fileFormat && detectedFileFormat && <OutputFormatSelect style={{ maxWidth: 180 }} detectedFileFormat={detectedFileFormat} fileFormat={fileFormat} onOutputFormatUserChange={onOutputFormatUserChange} />}- <Button onClick={onHide} style={{ marginLeft: 10 }}>Cancel</Button>+ <div style={{ display: 'flex', alignItems: 'center' }}>+ <Checkbox checked={enableReadFileMeta} onChange={(e) => setEnableReadFileMeta(e.target.checked)} label={t('Check files')} marginLeft={10} marginRight={10} />+ <Button iconBefore={FaCheckCircle} onClick={() => setSettingsVisible(true)}>{t('Options')}</Button>+ {fileFormat && detectedFileFormat ? (+ <OutputFormatSelect style={{ maxWidth: 180 }} detectedFileFormat={detectedFileFormat} fileFormat={fileFormat} onOutputFormatUserChange={onOutputFormatUserChange} />+ ) : (+ <Button disabled isLoading>{t('Loading')}</Button>+ )}<Button iconBefore={<AiOutlineMergeCells />} isLoading={detectedFileFormat == null} appearance="primary" onClick={onConcatClick}>{t('Merge!')}</Button>- </>+ </div>)}><div style={containerStyle}>@@ -89,13 +151,22 @@ const ConcatDialog = memo(({<div>{paths.map((path, index) => (<div key={path} style={rowStyle} title={path}>- {index + 1}- {'. '}- <span style={{ color: 'rgba(0,0,0,0.7)' }}>{basename(path)}</span>+ <div>+ {index + 1}+ {'. '}+ <span style={{ color: 'rgba(0,0,0,0.7)' }}>{basename(path)}</span>+ {!allFilesMetaCache[path] && <FaQuestionCircle color="#996A13" style={{ marginLeft: 10 }} />}+ {problemsByFile[path] && <FaExclamationTriangle color="#996A13" style={{ marginLeft: 10 }} />}+ </div>+ {problemsByFile[path] && <div style={{ marginBottom: 7, color: '#996A13', fontWeight: 'bold' }}>{problemsByFile[path]}</div>}</div>))}</div></div>++ {enableReadFileMeta && (!allFilesMeta || Object.values(problemsByFile).length > 0) && (+ <Alert intent="warning">{t('A mismatch was detected in at least one file. You may proceed, but the resulting file might not be playable.')}</Alert>+ )}</Dialog><Dialog isShown={settingsVisible} onCloseComplete={() => setSettingsVisible(false)} title={t('Options')} hasCancel={false} confirmLabel={t('Close')}>@@ -103,7 +174,7 @@ const ConcatDialog = memo(({<Checkbox checked={preserveMetadataOnMerge} onChange={(e) => setPreserveMetadataOnMerge(e.target.checked)} label={t('Preserve original metadata when merging? (slow)')} />- <Checkbox checked={preserveMovData} onChange={(e) => setPreserveMovData(e.target.checked)} label={t('Preserve all MP4/MOV metadata?')} />+ {isMov(fileFormat) && <Checkbox checked={preserveMovData} onChange={(e) => setPreserveMovData(e.target.checked)} label={t('Preserve all MP4/MOV metadata?')} />}<Checkbox checked={segmentsToChapters} onChange={(e) => setSegmentsToChapters(e.target.checked)} label={t('Create chapters from merged segments? (slow)')} />