Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

feat(media): timecode parsing for the forthcoming media_edit tool

Durganshu2fea4468b043
diff --git a/app/src/main/java/com/gotcha/tools/MediaTimeSpec.kt b/app/src/main/java/com/gotcha/tools/MediaTimeSpec.kt
new file mode 100644
index 0000000..51c5f26
--- /dev/null
+++ b/app/src/main/java/com/gotcha/tools/MediaTimeSpec.kt
@@ -0,0 +1,167 @@
+package com.gotcha.tools
+
+import java.util.Locale
+
+/**
+ * Parsing of the timecodes the model writes when trimming media ("1:23", "90s",
+ * "1h02m03s", "12.5").
+ *
+ * Kept apart from [MediaEditTool] and free of Android APIs so the arithmetic —
+ * the part most likely to be wrong — is unit-testable without Robolectric or a
+ * device codec. Mirrors the split between [PdfTool] and [PdfPageSpec].
+ *
+ * Everything is milliseconds: [android.media.MediaMetadataRetriever] reports
+ * duration in ms, and media3's `ClippingConfiguration` takes ms, so there is no
+ * unit conversion left to get wrong at the call site.
+ */
+object MediaTimeSpec {
+
+ /** Accepted spelling for "to the end of the file" as an end timecode. */
+ const val END = "end"
+
+ private const val MS_PER_SECOND = 1000L
+ private const val SECONDS_PER_MINUTE = 60
+ private const val MINUTES_PER_HOUR = 60
+ private const val COLON_PARTS_MIN_SEC = 2
+ private const val COLON_PARTS_HOUR_MIN_SEC = 3
+
+ /** "90s", "1m30s", "500ms", "1h2m" — a run of number+unit pairs with no separators. */
+ private val UNIT_FORM = Regex("^(?:\\d+(?:\\.\\d+)?(?:ms|s|m|h))+$", RegexOption.IGNORE_CASE)
+ private val UNIT_PAIR = Regex("(\\d+(?:\\.\\d+)?)(ms|s|m|h)", RegexOption.IGNORE_CASE)
+
+ /**
+ * Returns the timecode in milliseconds, or a failure whose message is written
+ * for the model to act on rather than for a log file.
+ */
+ @Suppress("ReturnCount")
+ fun parse(spec: String): Result<Long> {
+ val trimmed = spec.trim().lowercase(Locale.ROOT)
+ if (trimmed.isEmpty()) return malformed(spec)
+ if (trimmed.startsWith("-")) {
+ return Result.failure(
+ IllegalArgumentException(
+ "Timecode '$spec' is negative. Timecodes are measured from the start of the file."
+ )
+ )
+ }
+ return when {
+ trimmed.contains(':') -> parseColonForm(trimmed, spec)
+ UNIT_FORM.matches(trimmed) -> parseUnitForm(trimmed)
+ else -> trimmed.toDoubleOrNull()
+ ?.let { Result.success(secondsToMillis(it)) }
+ ?: malformed(spec)
+ }
+ }
+
+ /**
+ * Resolves a trim window against a known [durationMs]. A null [start] means
+ * the beginning; a null or "end" [end] means the end of the file.
+ *
+ * The returned range is half-open in spirit — `first` is inclusive, `last` is
+ * the exclusive end position — but expressed as a [LongRange] because callers
+ * only ever hand both numbers to media3.
+ */
+ @Suppress("ReturnCount")
+ fun parseRange(start: String?, end: String?, durationMs: Long): Result<LongRange> {
+ val startMs = start?.let { parse(it).getOrElse { e -> return Result.failure(e) } } ?: 0L
+ val endMs = when {
+ end == null || end.trim().equals(END, ignoreCase = true) -> durationMs
+ else -> parse(end).getOrElse { e -> return Result.failure(e) }
+ }
+ if (durationMs > 0 && startMs >= durationMs) {
+ return Result.failure(
+ IllegalArgumentException(
+ "Start ${format(startMs)} is at or past the end of the file (${format(durationMs)}). " +
+ "Call operation='info' to check the duration before trimming."
+ )
+ )
+ }
+ // A clip that overruns the file is the model misreading a duration, not a
+ // reason to fail: media3 stops at the last frame, so clamp and carry on.
+ val clampedEnd = if (durationMs > 0) minOf(endMs, durationMs) else endMs
+ if (clampedEnd <= startMs) {
+ return Result.failure(
+ IllegalArgumentException(
+ "End ${format(endMs)} is not after start ${format(startMs)} — that would produce an empty file. " +
+ "Write the window low-to-high."
+ )
+ )
+ }
+ return Result.success(startMs..clampedEnd)
+ }
+
+ /** Renders milliseconds back as "h:mm:ss.s" / "m:ss.s", for result messages. */
+ fun format(ms: Long): String {
+ val totalSeconds = ms / MS_PER_SECOND
+ val tenths = (ms % MS_PER_SECOND) / 100
+ val seconds = totalSeconds % SECONDS_PER_MINUTE
+ val minutes = (totalSeconds / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR
+ val hours = totalSeconds / (SECONDS_PER_MINUTE * MINUTES_PER_HOUR)
+ val head = if (hours > 0) {
+ String.format(Locale.ROOT, "%d:%02d:%02d", hours, minutes, seconds)
+ } else {
+ String.format(Locale.ROOT, "%d:%02d", minutes, seconds)
+ }
+ return if (tenths > 0) "$head.$tenths" else head
+ }
+
+ /** "1:23", "1:23.5" (m:ss) or "1:02:03" (h:mm:ss). */
+ @Suppress("ReturnCount")
+ private fun parseColonForm(trimmed: String, original: String): Result<Long> {
+ val parts = trimmed.split(":")
+ if (parts.size !in COLON_PARTS_MIN_SEC..COLON_PARTS_HOUR_MIN_SEC) return malformed(original)
+ if (parts.any { it.isBlank() }) return malformed(original)
+
+ // Only the last field may be fractional; the rest are whole units.
+ val leading = parts.dropLast(1).map { it.toIntOrNull() ?: return malformed(original) }
+ val seconds = parts.last().toDoubleOrNull() ?: return malformed(original)
+ if (leading.any { it < 0 } || seconds < 0) return malformed(original)
+
+ // 90 seconds inside a colon form is ambiguous — the model may have meant
+ // 1:30 or 90s. Refuse rather than silently picking one.
+ if (seconds >= SECONDS_PER_MINUTE) {
+ return Result.failure(
+ IllegalArgumentException(
+ "Timecode '$original' has $seconds seconds in a field that holds at most 59. " +
+ "Write '1:30' for ninety seconds, or '90s'."
+ )
+ )
+ }
+ if (parts.size == COLON_PARTS_HOUR_MIN_SEC && leading[1] >= MINUTES_PER_HOUR) {
+ return Result.failure(
+ IllegalArgumentException(
+ "Timecode '$original' has ${leading[1]} minutes in a field that holds at most 59."
+ )
+ )
+ }
+ val minutes = leading.last()
+ val hours = if (parts.size == COLON_PARTS_HOUR_MIN_SEC) leading.first() else 0
+ val totalSeconds = hours.toDouble() * SECONDS_PER_MINUTE * MINUTES_PER_HOUR +
+ minutes.toDouble() * SECONDS_PER_MINUTE + seconds
+ return Result.success(secondsToMillis(totalSeconds))
+ }
+
+ /** "90s", "1m30s", "500ms", "1h2m3s". */
+ private fun parseUnitForm(trimmed: String): Result<Long> {
+ var millis = 0.0
+ for (match in UNIT_PAIR.findAll(trimmed)) {
+ val value = match.groupValues[1].toDouble()
+ millis += when (match.groupValues[2]) {
+ "ms" -> value
+ "s" -> value * MS_PER_SECOND
+ "m" -> value * MS_PER_SECOND * SECONDS_PER_MINUTE
+ else -> value * MS_PER_SECOND * SECONDS_PER_MINUTE * MINUTES_PER_HOUR
+ }
+ }
+ return Result.success(millis.toLong())
+ }
+
+ private fun secondsToMillis(seconds: Double): Long = Math.round(seconds * MS_PER_SECOND)
+
+ private fun malformed(spec: String): Result<Long> = Result.failure(
+ IllegalArgumentException(
+ "Could not read the timecode '$spec'. Use '1:23' (m:ss), '1:02:03' (h:mm:ss), '90s', '1m30s' or " +
+ "plain seconds like '12.5'."
+ )
+ )
+}
diff --git a/app/src/test/java/com/gotcha/tools/MediaTimeSpecTest.kt b/app/src/test/java/com/gotcha/tools/MediaTimeSpecTest.kt
new file mode 100644
index 0000000..79f78ee
--- /dev/null
+++ b/app/src/test/java/com/gotcha/tools/MediaTimeSpecTest.kt
@@ -0,0 +1,161 @@
+package com.gotcha.tools
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+/**
+ * Timecode arithmetic for `media_edit`. Pure JVM: [MediaTimeSpec] deliberately
+ * touches no Android or media3 API, because a wrong conversion here silently
+ * trims the wrong window rather than failing loudly — and the encoder half of
+ * the tool needs a real device codec to exercise at all.
+ */
+class MediaTimeSpecTest {
+
+ private fun parsed(spec: String): Long = MediaTimeSpec.parse(spec).getOrThrow()
+
+ private fun error(spec: String): String {
+ val result = MediaTimeSpec.parse(spec)
+ assertTrue("expected '$spec' to be rejected, got ${result.getOrNull()}", result.isFailure)
+ return result.exceptionOrNull()!!.message.orEmpty()
+ }
+
+ @Test
+ fun `plain number is seconds`() {
+ assertEquals(0L, parsed("0"))
+ assertEquals(12_000L, parsed("12"))
+ assertEquals(12_500L, parsed("12.5"))
+ }
+
+ @Test
+ fun `colon form reads as minutes and seconds`() {
+ assertEquals(83_000L, parsed("1:23"))
+ assertEquals(83_500L, parsed("1:23.5"))
+ assertEquals(0L, parsed("0:00"))
+ }
+
+ @Test
+ fun `three colon fields read as hours minutes seconds`() {
+ assertEquals(3_723_000L, parsed("1:02:03"))
+ assertEquals(36_000_000L, parsed("10:00:00"))
+ }
+
+ @Test
+ fun `unit form accepts single and compound units`() {
+ assertEquals(90_000L, parsed("90s"))
+ assertEquals(90_000L, parsed("1m30s"))
+ assertEquals(500L, parsed("500ms"))
+ assertEquals(3_723_000L, parsed("1h2m3s"))
+ assertEquals(120_000L, parsed("2m"))
+ }
+
+ @Test
+ fun `unit form is case insensitive and tolerates surrounding space`() {
+ assertEquals(90_000L, parsed(" 1M30S "))
+ }
+
+ @Test
+ fun `ms suffix is not read as minutes plus seconds`() {
+ // The alternation must prefer "ms" over "m", or 2ms becomes 2 minutes.
+ assertEquals(2L, parsed("2ms"))
+ }
+
+ @Test
+ fun `sixty or more seconds inside a colon field is ambiguous and refused`() {
+ assertTrue(error("1:90").contains("at most 59"))
+ assertTrue(error("1:02:75").contains("at most 59"))
+ }
+
+ @Test
+ fun `sixty or more minutes inside an hour form is refused`() {
+ assertTrue(error("1:75:00").contains("at most 59"))
+ }
+
+ @Test
+ fun `negative timecodes are refused`() {
+ assertTrue(error("-5").contains("negative"))
+ assertTrue(error("-1:30").contains("negative"))
+ }
+
+ @Test
+ fun `malformed timecodes name the accepted forms`() {
+ assertTrue(error("banana").contains("1:23"))
+ assertTrue(error("").contains("1:23"))
+ assertTrue(error("1:2:3:4").contains("1:23"))
+ assertTrue(error("1::3").contains("1:23"))
+ }
+
+ // ---- ranges ----
+
+ private fun range(start: String?, end: String?, durationMs: Long = 60_000L): LongRange =
+ MediaTimeSpec.parseRange(start, end, durationMs).getOrThrow()
+
+ private fun rangeError(start: String?, end: String?, durationMs: Long = 60_000L): String {
+ val result = MediaTimeSpec.parseRange(start, end, durationMs)
+ assertTrue("expected $start..$end to be rejected", result.isFailure)
+ return result.exceptionOrNull()!!.message.orEmpty()
+ }
+
+ @Test
+ fun `null start is the beginning and null end is the duration`() {
+ assertEquals(0L..60_000L, range(null, null))
+ }
+
+ @Test
+ fun `the literal end keyword resolves to the duration`() {
+ assertEquals(10_000L..60_000L, range("10", "end"))
+ assertEquals(10_000L..60_000L, range("10", "END"))
+ }
+
+ @Test
+ fun `an end past the duration is clamped rather than refused`() {
+ // The model misreading a duration should still produce the sensible clip.
+ assertEquals(10_000L..60_000L, range("10", "5:00"))
+ }
+
+ @Test
+ fun `a start at or past the duration is refused`() {
+ assertTrue(rangeError("60", null).contains("past the end"))
+ assertTrue(rangeError("90", null).contains("past the end"))
+ }
+
+ @Test
+ fun `a backwards window is refused rather than silently emptied`() {
+ assertTrue(rangeError("30", "10").contains("not after start"))
+ assertTrue(rangeError("30", "30").contains("not after start"))
+ }
+
+ @Test
+ fun `an unknown duration leaves the end unclamped`() {
+ // MediaMetadataRetriever reports 0 for some streams; the clip should still run.
+ assertEquals(10_000L..20_000L, range("10", "20", durationMs = 0L))
+ }
+
+ @Test
+ fun `a malformed bound is reported rather than defaulted`() {
+ assertTrue(rangeError("banana", null).contains("1:23"))
+ assertTrue(rangeError(null, "banana").contains("1:23"))
+ }
+
+ // ---- formatting ----
+
+ @Test
+ fun `format renders minutes and seconds with zero padding`() {
+ assertEquals("0:00", MediaTimeSpec.format(0L))
+ assertEquals("1:23", MediaTimeSpec.format(83_000L))
+ assertEquals("1:23.5", MediaTimeSpec.format(83_500L))
+ }
+
+ @Test
+ fun `format adds an hours field only when needed`() {
+ assertEquals("1:02:03", MediaTimeSpec.format(3_723_000L))
+ assertEquals("59:59", MediaTimeSpec.format(3_599_000L))
+ }
+
+ @Test
+ fun `format round-trips through parse`() {
+ listOf("0:30", "1:23", "1:02:03").forEach { spec ->
+ assertEquals(spec, MediaTimeSpec.format(MediaTimeSpec.parse(spec).getOrThrow()))
+ }
+ }
+}