1
0
mirror of https://github.com/mifi/lossless-cut.git synced 2024-11-22 02:12:30 +01:00

read audio fps too

closes #1754
This commit is contained in:
Mikael Finstad 2023-12-03 00:00:09 +08:00
parent 233c5c017e
commit 97931887ec
No known key found for this signature in database
GPG Key ID: 25AB36E3E81CBC26
2 changed files with 38 additions and 7 deletions

View File

@ -311,7 +311,7 @@ const App = memo(() => {
}, [seekRel, zoomedDuration]);
const shortStep = useCallback((direction) => {
// If we don't know fps, just assume 30 (for example if audio file)
// If we don't know fps, just assume 30 (for example if unknown audio file)
const fps = detectedFps || 30;
// try to align with frame
@ -1492,8 +1492,15 @@ const App = memo(() => {
// throw new Error('test');
// eslint-disable-next-line no-inner-declarations
function getFps() {
if (haveVideoStream) return getStreamFps(videoStream);
if (haveAudioStream) return getStreamFps(audioStream);
return undefined;
}
if (timecode) setStartTimeOffset(timecode);
setDetectedFps(haveVideoStream ? getStreamFps(videoStream) : undefined);
setDetectedFps(getFps());
if (!haveVideoStream) setWaveformMode('big-waveform');
setMainFileMeta({ streams: fileMeta.streams, formatData: fileMeta.format, chapters: fileMeta.chapters });
setMainVideoStream(videoStream);

View File

@ -523,12 +523,36 @@ export function isProblematicAvc1(outFormat, streams) {
return isMov(outFormat) && streams.some((s) => s.codec_name === 'h264' && s.codec_tag === '0x31637661' && s.codec_tag_string === 'avc1' && s.pix_fmt === 'yuv422p10le');
}
export function getStreamFps(stream) {
function parseFfprobeFps(stream) {
const match = typeof stream.avg_frame_rate === 'string' && stream.avg_frame_rate.match(/^([0-9]+)\/([0-9]+)$/);
if (stream.codec_type === 'video' && match) {
if (!match) return undefined;
const num = parseInt(match[1], 10);
const den = parseInt(match[2], 10);
if (den > 0) return num / den;
return undefined;
}
export function getStreamFps(stream) {
if (stream.codec_type === 'video') {
const fps = parseFfprobeFps(stream);
return fps;
}
if (stream.codec_type === 'audio') {
if (typeof stream.sample_rate === 'string') {
const sampleRate = parseInt(stream.sample_rate, 10);
if (!Number.isNaN(sampleRate) && sampleRate > 0) {
if (stream.codec_name === 'mp3') {
// https://github.com/mifi/lossless-cut/issues/1754#issuecomment-1774107468
const frameSize = 1152;
return sampleRate / frameSize;
}
if (stream.codec_name === 'aac') {
// https://stackoverflow.com/questions/59173435/aac-packet-size
const frameSize = 1024;
return sampleRate / frameSize;
}
}
}
}
return undefined;
}