Skip to content

Effort free video editing!

Share time units between the CLI and actions

WyattBlueab432f74a0b3
diff --git a/docs/src/docs/actions.md b/docs/src/docs/actions.md
index e14b91e5..e9ccff28 100644
--- a/docs/src/docs/actions.md
+++ b/docs/src/docs/actions.md
@@ -529,7 +529,7 @@ auto-editor video.mp4 -w:1 zoom:1..1.5:ease=inout
`:ease=curve[:duration]`
- **curve** — `linear`, `in`, `out`, or `inout`
-- **duration** — optional; e.g. `2sec` or a bare frame count. Once it elapses,
+- **duration** — optional; e.g. `2sec`, `30f`, or a bare frame count. Once it elapses,
the value holds at its end. Omitted, the animation spans the whole section.
```sh
diff --git a/docs/src/docs/range-syntax.md b/docs/src/docs/range-syntax.md
index bbbc39c3..68e83859 100644
--- a/docs/src/docs/range-syntax.md
+++ b/docs/src/docs/range-syntax.md
@@ -54,7 +54,9 @@ The default unit is the timeline's timebase. Since specifying the range in this
# Cut out the first 10 seconds.
auto-editor example.mp4 --cut start,10secs
```
-You can also use `s`, `sec`, `second`, or `seconds`, depending on your preference.
+You can also use `s`, `sec`, `secs`, `second`, or `seconds`, depending on your preference.
+
+To say frames outright, write `f`, `frame`, or `frames`: `--cut 0,30f` is the same as `--cut 0,30`.
## Negative Indexes
Negative numbers can be used to count down starting from the end.
diff --git a/src/action.nim b/src/action.nim
index 40be5369..c05ff4b7 100644
--- a/src/action.nim
+++ b/src/action.nim
@@ -1,6 +1,6 @@
import std/[algorithm, hashes, math, sequtils, strformat, strutils, options]
import ./util/[color, suggest]
-import ./lib/[dnorm16, escape]
+import ./lib/[dnorm16, escape, timeunit]
type
# The order is part of the API
@@ -437,21 +437,17 @@ proc pInt(s: string, what = "integer value"): int {.raises: [ActionParseError].}
except ValueError: raise newException(ActionParseError, &"Invalid {what}: {s}")
proc parseDuration(spec: string): (float32, DurUnit) {.raises: [ActionParseError].} =
- ## "2sec" -> (2, duSec); "30" or "30frames" -> (30, duFrames).
- var s = spec
- var unit = duFrames
- for suf in ["seconds", "second", "sec"]:
- if s.endsWith(suf):
- unit = duSec
- s = s[0 ..< s.len - suf.len]
- break
- if unit == duFrames:
- for suf in ["frames", "frame", "f"]:
- if s.endsWith(suf):
- s = s[0 ..< s.len - suf.len]
- break
- try: (parseFloat(s).float32, unit)
- except ValueError: raise newException(ActionParseError, &"Invalid duration: {spec}")
+ ## "2s" or "2sec" -> (2, duSec); "30", "30f" or "30frames" -> (30, duFrames).
+ ## The same units as times on the command line.
+ let (num, suffix) = splitTimeUnit(spec)
+ var tu: TimeUnit
+ if not parseTimeUnit(suffix, tu):
+ raise newException(ActionParseError, &"Invalid duration: {spec}")
+ let n =
+ try: parseFloat(num)
+ except ValueError: raise newException(ActionParseError, &"Invalid duration: {spec}")
+ if tu == tuFrames: (n.float32, duFrames)
+ else: (float32(n * tu.seconds), duSec)
proc parseKfTime(spec: string): KfTime {.raises: [ActionParseError].} =
## "50%" -> percent of the section; otherwise a duration, as parseDuration.
diff --git a/src/lib/timeunit.nim b/src/lib/timeunit.nim
new file mode 100644
index 00000000..7673ced4
--- /dev/null
+++ b/src/lib/timeunit.nim
@@ -0,0 +1,30 @@
+## The units a written time may carry, shared by command-line times and
+## action durations so the two accept the same spellings.
+
+type TimeUnit* = enum
+ tuFrames, tuSeconds, tuMinutes, tuHours
+
+func parseTimeUnit*(suffix: string, unit: var TimeUnit): bool {.raises: [].} =
+ ## The unit `suffix` names; a bare number ("") counts frames.
+ case suffix
+ of "", "f", "frame", "frames": unit = tuFrames
+ of "s", "sec", "secs", "second", "seconds": unit = tuSeconds
+ of "min", "mins", "minute", "minutes": unit = tuMinutes
+ of "hour", "hours": unit = tuHours
+ else: return false
+ true
+
+func seconds*(unit: TimeUnit): float64 {.raises: [].} =
+ ## How many seconds one of `unit` is. Frames have no fixed length.
+ case unit
+ of tuFrames: 0.0
+ of tuSeconds: 1.0
+ of tuMinutes: 60.0
+ of tuHours: 3600.0
+
+func splitTimeUnit*(s: string): tuple[num, suffix: string] {.raises: [].} =
+ ## "30frames" -> ("30", "frames"): the trailing letters are the unit.
+ var i = s.len
+ while i > 0 and s[i - 1] in {'a'..'z', 'A'..'Z'}:
+ dec i
+ (s[0 ..< i], s[i .. ^1])
diff --git a/src/util/fun.nim b/src/util/fun.nim
index 3b70339b..0d0242a6 100644
--- a/src/util/fun.nim
+++ b/src/util/fun.nim
@@ -3,7 +3,7 @@ from std/uri import decodeUrl
from std/math import gcd, `mod`, round, trunc, pow
import ../log
-import ../lib/dnorm16
+import ../lib/[dnorm16, timeunit]
func b64urlDecode*(s: string): seq[byte] =
var padded = s.replace('-', '+').replace('_', '/')
@@ -314,17 +314,17 @@ proc parseTimeSimple*(val: string): PackedInt =
if not (f > -packedSpan and f < packedSpan):
error &"'{val}': Time is out of range"
pack(flag, f.int64)
- if unit in ["s", "sec", "secs", "second", "seconds"]:
- return packed(true, round(num * tb))
- if unit in ["min", "mins", "minute", "minutes"]:
- return packed(true, round(num * tb * 60))
- if unit == "hour":
- return packed(true, round(num * tb * 3600))
- if unit != "":
+ var tu: TimeUnit
+ if not parseTimeUnit(unit, tu):
error &"'{val}': Time format got unknown unit: `{unit}`"
+ if tu != tuFrames:
+ return packed(true, round(num * tb * tu.seconds))
if num != trunc(num):
- error &"'{val}': Time format expects an integer"
+ if unit == "":
+ error &"'{val}': A time without a unit is a frame count, so it must be " &
+ &"whole. For seconds, write '{val}s'"
+ error &"'{val}': A frame count must be whole"
return packed(false, num)
proc parseTime*(val: string): PackedInt =