To extract video segment timestamps with FFmpeg, use the -ss option for start time and -to or -t for duration. The command structure is ffmpeg -i input.mp4 -ss START -to END output.mp4. This method allows precise selection of specific intervals within a media file for further editing or processing.
Understanding Timestamp Options
FFmpeg provides several flags to define temporal boundaries in media files. The most common are -ss, which sets the start time, and -t or -to, which define the length or end point of the segment. Understanding the difference between these options is crucial for accurate extraction.
The -ss flag specifies the start time of the output segment. It accepts time values in seconds, minutes:seconds, or hours:minutes:seconds format. For example, -ss 00:01:30 starts the segment at one minute and thirty seconds into the video. Precision depends on how FFmpeg handles seeking, which varies based on whether you place this flag before or after the input file identifier.
The -t flag defines the duration of the output segment in seconds. If you want a ten-second clip starting from the beginning, you would use -t 10. Alternatively, the -to flag specifies the absolute end time of the segment. Using -to 60 ensures the output stops exactly at the one-minute mark, regardless of the start time.
Command Structure and Placement
The position of the -ss flag relative to the -i (input) flag significantly impacts performance and accuracy. When placed before -i, FFmpeg performs a fast seek, jumping directly to the specified timestamp using keyframes. This is much faster for large files but may result in slightly less precise cuts if the start time falls between keyframes.
ffmpeg -ss 00:05:00 -i input.mp4 -t 10 output.mp4
In contrast, placing -ss after -i forces FFmpeg to decode frames sequentially from the beginning until it reaches the desired timestamp. This method is slower but offers frame-accurate precision. Use this approach when exact frame alignment is critical for your project.
ffmpeg -i input.mp4 -ss 00:05:00 -t 10 output.mp4
For most general use cases, the fast seek method (before -i) is sufficient and recommended due to its speed. However, if you notice visual artifacts or incorrect cuts, switch to the sequential decoding method by moving -ss after -i.
Stream Copy vs. Re-encoding
By default, FFmpeg re-encodes the video and audio streams during extraction. This process takes time and may reduce quality. To avoid re-encoding, add the -c copy flag to the command. This tells FFmpeg to simply copy the existing streams without modification.
ffmpeg -i input.mp4 -ss 00:05:00 -t 10 -c copy output.mp4
Stream copying is ideal when you need quick results and do not require precise frame-level accuracy. Note that using -c copy with -ss before -i relies entirely on keyframe availability. If the start time does not align with a keyframe, the output might begin slightly earlier than expected.
If you need both speed and precision, consider using -c copy with -ss after -i. Although slower, this combination ensures that the cut occurs exactly at the specified timestamp while avoiding re-encoding overhead.
Handling Negative Timestamps
When trimming from the start of a video, some users encounter negative presentation timestamps. This issue often arises when using certain demuxers or container formats. Refer to our guide on Trimming Start of Video Generates Negative Presentation Timestamps with FFmpeg for solutions to this specific problem.
Practical Examples
Here are common scenarios for extracting video segments:
- Extract first 10 seconds:
ffmpeg -i input.mp4 -t 10 -c copy output.mp4 - Extract segment from 2:00 to 2:30:
ffmpeg -i input.mp4 -ss 00:02:00 -to 00:02:30 -c copy output.mp4 - Precise 5-second clip at 1:00:
ffmpeg -i input.mp4 -ss 00:01:00 -t 5 -c:v libx264 -c:a aac output.mp4
These examples demonstrate flexibility in handling different requirements. Adjust the codec options (-c:v, -c:a) as needed for your target format.
Common Pitfalls
One frequent error is misunderstanding time formats. FFmpeg accepts various formats, including seconds (120), minutes:seconds (2:00), and ISO 8601 (PT2M). Ensure consistency in your time notation to avoid parsing errors.
Another pitfall is ignoring keyframe alignment. When using stream copy, the actual start time may differ from the requested timestamp by up to the distance to the previous keyframe. For critical applications, always verify the output using a media player or FFprobe.
Refer to Understanding FFmpeg's ss, -t, -to, Streamcopy, and Keyframe Differences for a deeper dive into how these parameters interact internally.
Frequently Asked Questions
How do I specify time in hours?
You can specify time in hours using the format HH:MM:SS. For example, -ss 01:30:00 sets the start time to one hour and thirty minutes. FFmpeg automatically parses this format correctly.
Why is my output video longer than expected?
This usually happens if you use -t incorrectly or if stream copying skips frames. Verify that -t represents duration, not end time. Also, check if keyframe misalignment caused extra frames to be included in the output.
Can I extract multiple segments in one command?
No, FFmpeg processes one output file per command. You must run separate commands for each segment. However, you can chain them in a script for automation.
Does -ss before -i affect audio sync?
Fast seeking with -ss before -i may cause slight audio-video desynchronization if keyframes are not aligned. Using -ss after -i ensures better sync but is slower.
How do I remove every Nth frame instead of extracting segments?
If your goal is frame removal rather than segment extraction, consider using filters like select. See Removing Every Nth Frame in Raw Video Using FFmpeg for detailed instructions on frame-level operations.