allow inputting seconds only

in go to timecode
https://github.com/mifi/lossless-cut/issues/254#issuecomment-2781652373
pull/2465/head
Mikael Finstad 1 year ago
parent 2505074528
commit 25050707ee
No known key found for this signature in database
GPG Key ID: 25AB36E3E81CBC26

@ -25,7 +25,12 @@ const { downloadMediaUrl } = remote.require('./index.js');
export async function promptTimecode({ initialValue, title, text, inputPlaceholder, parseTimecode, allowRelative = false }: {
initialValue?: string | undefined, title: string, text?: string | undefined, inputPlaceholder: string, parseTimecode: ParseTimecode, allowRelative?: boolean,
initialValue?: string | undefined,
title: string,
text?: string | undefined,
inputPlaceholder: string,
parseTimecode: ParseTimecode,
allowRelative?: boolean,
}) {
const { value } = await Swal.fire<string>({
title,
@ -49,9 +54,9 @@ export async function promptTimecode({ initialValue, title, text, inputPlacehold
else if (value.startsWith('+')) relDirection = 1;
}
const value2 = allowRelative ? value.replace(/^[+-]/, '') : value;
const withoutPrefix = allowRelative ? value.replace(/^[+-]/, '') : value;
const duration = parseTimecode(value2);
const duration = parseTimecode(withoutPrefix);
// Invalid, try again
if (duration === undefined) return promptTimecode({ initialValue: value, title, text, inputPlaceholder, parseTimecode, allowRelative });

@ -45,8 +45,13 @@ it('should format and parse duration with correct rounding', () => {
expect(formatDuration({ seconds: parseDuration('00:00:00,00', 30), fps: 30 })).toBe('00:00:00.00');
expect(formatDuration({ seconds: parseDuration('00:00:00,29', 30), fps: 30 })).toBe('00:00:00.29');
expect(formatDuration({ seconds: parseDuration('01:00:01,01', 30), fps: 30 })).toBe('01:00:01.01');
expect(parseDuration('0', 30)).toBe(0);
expect(parseDuration('0,1', 30)).toBe(0.03333333333333333);
// https://github.com/mifi/lossless-cut/issues/254#issuecomment-2781652373
expect(formatDuration({ seconds: parseDuration('71.2') })).toBe('00:01:11.200');
expect(formatDuration({ seconds: parseDuration('1000') })).toBe('00:16:40.000');
});
// https://github.com/mifi/lossless-cut/issues/1603

@ -43,13 +43,16 @@ export function formatDuration({ seconds: totalSecondsIn, fileNameFriendly, show
return `${sign}${hoursPart}${minutesPadded}${delim}${secondsPadded}${fraction}`;
}
const exactDurationRegex = /^-?\d{2}:\d{2}:\d{2}.\d{3}$/;
const durationRegex = /^(-?)(?:(?:(\d+):)?(\d{1,2}):)?(\d+(?:[,.]\d+)?)$/;
// todo adapt also to frame counts and frame fractions?
export const isExactDurationMatch = (str: string) => /^-?\d{2}:\d{2}:\d{2}.\d{3}$/.test(str);
export const isExactDurationMatch = (str: string) => exactDurationRegex.test(str);
// See also parseYoutube
export function parseDuration(str: string, fps?: number) {
// eslint-disable-next-line unicorn/better-regex
const match = str.replaceAll(/\s/g, '').match(/^(-?)(?:(?:(\d{1,}):)?(\d{1,2}):)?(\d{1,2}(?:[.,]\d{1,3})?)$/);
const match = str.replaceAll(/\s/g, '').match(durationRegex);
if (!match) return undefined;
@ -68,7 +71,6 @@ export function parseDuration(str: string, fps?: number) {
}
if (min > 59) return undefined;
if (sec >= 60) return undefined;
let time = (((hour * 60) + min) * 60 + sec);

Loading…
Cancel
Save