The swiss army knife of lossless video/audio editing
allow inputting seconds only
Mikael Finstad25050707eec5
diff --git a/src/renderer/src/dialogs/index.tsx b/src/renderer/src/dialogs/index.tsxindex 3905b465..32d9a5ff 100644--- a/src/renderer/src/dialogs/index.tsx+++ b/src/renderer/src/dialogs/index.tsx@@ -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, inputPlaceholdelse 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 againif (duration === undefined) return promptTimecode({ initialValue: value, title, text, inputPlaceholder, parseTimecode, allowRelative });diff --git a/src/renderer/src/util/duration.test.ts b/src/renderer/src/util/duration.test.tsindex 6085f480..23042f32 100644--- a/src/renderer/src/util/duration.test.ts+++ b/src/renderer/src/util/duration.test.ts@@ -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/1603diff --git a/src/renderer/src/util/duration.ts b/src/renderer/src/util/duration.tsindex 4354417d..dfaa670f 100644--- a/src/renderer/src/util/duration.ts+++ b/src/renderer/src/util/duration.ts@@ -43,13 +43,16 @@ export function formatDuration({ seconds: totalSecondsIn, fileNameFriendly, showreturn `${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 parseYoutubeexport 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);