Skip to content

fix(silence_cutter): handle negative timestamps and segments without video - #995

Open
Alvaro8317 wants to merge 2 commits into
OpenMontageInc:mainfrom
Alvaro8317:fix/silence-cutter-edge-cases
Open

Alvaro8317 wants to merge 2 commits into
OpenMontageInc:mainfrom
Alvaro8317:fix/silence-cutter-edge-cases

Conversation

@Alvaro8317

Copy link
Copy Markdown

Summary

Fixes two cases where silence_cutter returns success: True with a wrong result. I found both while cutting a 7m43s narrated screen recording.

1. A negative or exponent silence_start is misread. When a stream starts slightly before 0, FFmpeg prints silence_start: -0.000166667. The pattern [\d.]+ does not match it, so that start is lost and every later start is paired with the wrong end. On the original recording, 152 silences produced a single speech segment, so nothing would be cut. Exponent values have a related problem: 2.08333e-05 was read as 2.08333.

2. Audio longer than video corrupts the output. If a rendered segment falls past the last video frame, it is audio-only, and concatenating it breaks the output timeline. A 0.11 s audio tail was enough: the output declared 2429.9 s of video instead of about 388.7 s, and decoding reported an error.

Related issue

None. I searched open and closed issues and PRs for silence and silence_cutter and found no duplicate. #898 changed the same tool but covers detection failures.

Changes

  • Accept a minus sign and exponent notation in silencedetect values, and clamp the start to 0.
  • Leave segments without video packets out of the concat, in both remove and speed_up, only when the source has a real video stream. Audio-only files and cover art keep every segment.
  • Add dropped_segments_without_video to the result so the drop is visible, and exclude the dropped time from output_duration.
  • Add 13 regression tests.

Testing

  • 12 of the 13 new tests fail before the fix and all pass after it. Five of those 12 (matching streams, audio-only, cover art) fail only because they check the new result field or helper; they guard against regressions. The other 7 fail because of the bugs or the missing count.
  • Full suite (pytest tests) on Python 3.11, Linux, FFmpeg 6.1.1: 2917 passed before, 2930 passed after, 17 skipped in both.
  • The same recording, remove mode, after the fix: 388.70 s video and 388.70 s audio, no decode errors, one segment dropped.
  • Not tested on Windows or macOS, or with other FFmpeg versions.

Notes for reviewers

  • Trade-off: when audio outlasts video, the audio in a fully video-less segment is dropped. In my case it was 0.08 s. If a source had several seconds of speech after its last frame, that speech would be lost, with only the new count in the result to show it. Padding with the last frame would keep it, but seemed too large a change for this fix. Happy to switch if you prefer it.
  • Each rendered segment now costs one extra ffprobe call.

Checklist

  • The change is focused on a single logical concern.
  • I ran the relevant tests locally (make test-contracts / make test) where applicable.
  • I updated docs/README if behavior or usage changed. (Not needed: the docs that mention the tool do not describe this behavior.)
  • No unrelated files (build artifacts, local config) are included in the diff.

🤖 Generated with Claude Code

- tools/video/silence_cutter.py: accept a leading minus sign and exponent notation when parsing
  silencedetect output, and clamp the start to 0, so a missed first start no longer shifts every
  later start/end pair
- tests/tools/test_silence_timestamp_parsing.py: new regression tests for negative and exponent
  first timestamps
- tools/video/silence_cutter.py: when the source has video, leave out of the concat any rendered
  segment without video packets, which happens when the audio outlasts the video and corrupts the
  output timeline; report the count and exclude it from output_duration. Audio-only sources and
  cover art keep every segment
- tests/tools/test_silence_segments_without_video.py: new native FFmpeg tests for audio longer than
  video, matching streams, audio-only input and cover art
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant