Skip to content

Commit

Permalink
re-encode flac when cutting
Browse files Browse the repository at this point in the history
and warn about it
fixes #1809
  • Loading branch information
mifi committed Aug 27, 2024
1 parent 1a674fa commit e7d2caf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/renderer/src/components/ExportConfirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ function ExportConfirm({
// some thumbnail streams (png,jpg etc) cannot always be cut correctly, so we warn if they try to.
const areWeCuttingProblematicStreams = areWeCutting && mainCopiedThumbnailStreams.length > 0;

const warnings = useMemo(() => {
const ret: string[] = [];
// https://github.com/mifi/lossless-cut/issues/1809
if (areWeCutting && outFormat === 'flac') {
ret.push(t('There is a known issue in FFmpeg with cutting FLAC files. The file will be re-encoded, which is still lossless, but the export may be slower.'));
}
return ret;
}, [areWeCutting, outFormat, t]);
const exportModeDescription = useMemo(() => ({
segments_to_chapters: t('Don\'t cut the file, but instead export an unmodified original which has chapters generated from segments'),
merge: t('Auto merge segments to one file after export'),
Expand Down Expand Up @@ -208,6 +216,14 @@ function ExportConfirm({

<table className={styles['options']}>
<tbody>
{warnings.map((warning) => (
<tr key={warning}>
<td colSpan={2}>
<div style={warningStyle}><WarningSignIcon verticalAlign="middle" color="warning" /> {warnings.join('\n')}</div>
</td>
<td />
</tr>
))}
{selectedSegments.length !== nonFilteredSegmentsOrInverse.length && (
<tr>
<td colSpan={2}>
Expand Down
5 changes: 3 additions & 2 deletions src/renderer/src/hooks/useFfmpegOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
const cuttingStart = isCuttingStart(cutFrom);
const cutFromWithAdjustment = cutFrom + cutFromAdjustmentFrames * frameDuration;
const cuttingEnd = isCuttingEnd(cutTo, videoDuration);
console.log('Cutting from', cuttingStart ? `${cutFrom} (${cutFromWithAdjustment} adjusted ${cutFromAdjustmentFrames} frames)` : 'start', 'to', cuttingEnd ? cutTo : 'end');
const areWeCutting = cuttingStart || cuttingEnd;
if (areWeCutting) console.log('Cutting from', cuttingStart ? `${cutFrom} (${cutFromWithAdjustment} adjusted ${cutFromAdjustmentFrames} frames)` : 'start', 'to', cuttingEnd ? cutTo : 'end');

let cutDuration = cutTo - cutFromWithAdjustment;
if (detectedFps != null) cutDuration = Math.max(cutDuration, frameDuration); // ensure at least one frame duration
Expand Down Expand Up @@ -279,7 +280,7 @@ function useFfmpegOperations({ filePath, treatInputFileModifiedTimeAsStart, trea
...flatMap(Object.entries(customTagsByFile[filePath] || []), ([key, value]) => ['-metadata', `${key}=${value}`]),
];

const mapStreamsArgs = getMapStreamsArgs({ copyFileStreams: copyFileStreamsFiltered, allFilesMeta, outFormat });
const mapStreamsArgs = getMapStreamsArgs({ copyFileStreams: copyFileStreamsFiltered, allFilesMeta, outFormat, areWeCutting });

const customParamsArgs = (() => {
const ret: string[] = [];
Expand Down
2 changes: 2 additions & 0 deletions src/renderer/src/util/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ function getPerStreamFlags({ stream, outputIndex, outFormat, manuallyCopyDisposi
// I think DV format only supports PCM_S16LE https://github.com/FFmpeg/FFmpeg/blob/b92028346c35dad837dd1160930435d88bd838b5/libavformat/dvenc.c#L450
addCodecArgs('pcm_s16le');
addArgs(`-ar:${outputIndex}`, '48000'); // maybe technically not lossless?
} else if (outFormat === 'flac' && areWeCutting && stream.codec_name === 'flac') { // https://github.com/mifi/lossless-cut/issues/1809
addCodecArgs('flac'); // lossless because flac is a lossless codec
} else {
addCodecArgs('copy');
}
Expand Down

0 comments on commit e7d2caf

Please sign in to comment.