Trimming Videos Is a Trade-off: copy-cut, re-encode, or filter-graph
Part of the Meicut Engineering series — how we build an in-browser media toolkit. ffmpeg -i in.mp4 -ss 60 -c copy -t 30 out.mp4 is everywhere. So is -i in.mp4 -c:v libx264 ... . And so is -filter_complex "...concat..." . Each one is presented as the way to trim a video. In practice there is no single best way: three mechanisms, three different trade-offs, and the right one depends entirely on…
Trimming videos can be achieved through three different methods: copy-cut, re-encode, and filter-graph splicing. Each approach has its own trade-offs, depending on the specific constraints and requirements.
Copy-cut is a lossless shortcut that tells ffmpeg not to decode or re-encode any data. It simply copies compressed packets and modifies container metadata. This method is significantly faster than any encoding process, as the bottleneck is usually I/O rather than CPU. However, it sacrifices frame accuracy since it can only cut between keyframes, resulting in a small delay (typically a fraction of a second to a couple of seconds) from the requested timestamp.
Additionally, the resulting output may not be frame-accurate, especially if B-frames are involved. Copy-cut is suitable for quick, bit-identical trims, such as removing intros or outros, or for batch processing files with consistent parameters.
The second method, re-encode with input seeking, offers frame-accurate results at the cost of speed. Input seeking locates the region at the demux level before a full re-encode produces the segment. By decoding from the nearest keyframe and starting to write exactly at the requested timestamp, this method ensures frame-level precision.
However, it is inherently slower due to the encoding process involved. The output is container-portable and filter-capable, allowing for adjustments like resolution changes, codec modifications, bitrate adjustments, and the application of effects such as fades.
The third approach, filter-graph splicing, combines trimming, timestamp resetting, and concatenation into a single -filter_complex pass. This method is frame-accurate and produces clean, continuous timestamp joins. It also allows for seamless transitions and applies effects in a single pass. Although it requires decoding the entire input to process multiple segments, the overall overhead is manageable, particularly when compared to performing separate re-encodes.
However, this method is more complex to implement and debug, and it can be resource-intensive for long files.
Ultimately, the choice between these methods depends on the specific needs of the project. For single-segment trims requiring frame accuracy, input seeking followed by re-encoding is the recommended approach. When multi-segment trims, precise fades, or cross-container output are essential, filter-graph splicing proves to be the most suitable solution. While each method has its trade-offs, understanding these aspects can help in making informed decisions based on the given constraints.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.