Skip to contents

Keep one continuous section of the input with FFmpeg's fast -ss and -to seek options. It does not use the trim filter of ffm_trim. Unlike the filter, seeking can use stream copy, so it is the tool for fast, lossless cuts. The glossary in vignette("tidymedia") explains media terms such as keyframe, re-encode and stream copy.

Usage

ffm_seek(object, start = NULL, end = NULL, reencode = TRUE)

Arguments

object

An FFmpeg pipeline (ffm) object created by ffm_files().

start

The start of the kept section, in seconds or in FFmpeg time duration syntax. NULL keeps the section from the beginning.

end

The end of the kept section, in seconds or in FFmpeg time duration syntax. NULL keeps the section to the end.

reencode

A logical. TRUE (the default) re-encodes for a frame-accurate cut. FALSE makes a fast seek that is safe to copy. With ffm_copy, its start moves back to a keyframe.

Value

object with an added instruction to cut the input by seeking.

Details

The reencode argument trades accuracy against speed:

  • reencode = TRUE (the default) is frame-accurate. The section is re-encoded, so it starts and ends on the exact frames you ask for. This is the safe default.

  • reencode = FALSE puts -ss and -to before -i, so FFmpeg seeks in the input. With ffm_copy, this is a fast, lossless copy. But the start moves back to the keyframe at or before start. So the output can begin earlier and run longer than you asked. In runs with FFmpeg 9.0.2 on macOS (2026-10-18), a copy cut from 3.8 to 7 seconds of a file with a keyframe every 2 seconds started at the keyframe at 2 seconds. It ended at 7 seconds when the file had no B-frames. When the file had them, the cut held 2 more frames and ended just after 7 seconds. Without ffm_copy(), FFmpeg re-encodes the section.

References

https://ffmpeg.org/ffmpeg.html#Main-options

Examples

video <- system.file("extdata", "sample.mp4", package = "tidymedia")
# Fast, lossless copy cut (the start moves back to a keyframe)
ffm_files(video, "output.mp4") |>
  ffm_seek(start = 1, end = 5, reencode = FALSE) |>
  ffm_copy() |>
  ffm_compile()
#> [1] "-y -ss 1 -to 5 -i \"/home/runner/work/_temp/Library/tidymedia/extdata/sample.mp4\" -codec:v copy -codec:a copy -avoid_negative_ts make_zero -map \"0\" \"output.mp4\""