<p>You seem to have a fundamental misunderstanding of how video works.  Video is composed of frames.  You can't have millisecond precision because video doesn't have millisecond precision.  If there are 30 frames per second in the video, you can only stop after one of those frames.  That means your options are only every 1/30th of a second roughly.
If the format is group of pictures based it may be limited when it can end as well depending on the formats ability to end a group of pictures early. (A group of pictures is used in formats where some frames are stored as changes from other related frames rather than full images.)</p>

<p>What are you trying to accomplish that you need this precision for?  Perhaps there may be an alternate way to accomplish your goal.</p>

CC BY-SA 3.0
2013-06-12T08:25:39.273Z
I need to synchronize the video with another machine. The video is too much longer and i need to trim it (just need 2s). I don't have 1/30th of a second precision (~=33ms). I cut between 1062ms and 3640ms but i get a video length of 2130ms (not 2578 as planned). I can send you the video if you want to test it. Thanks for your answer.
RMI
CC BY-SA 3.0
2013-06-12T13:36:28.347Z
@RMI - Maybe I'm not reading your command lines correctly, but they say a start point of 1520ms, not 1062ms.  This makes the output of 2130ms correct.
AJ Henderson
CC BY-SA 3.0
2013-06-13T09:49:16.377Z
If i trim with the first parameters (1520 to 3640) i have an length error too > than 1/30. I try with another video, it's the same problem. Perhaps the problem is the video codec.
RMI
CC BY-SA 3.0
2013-06-13T13:19:11.113Z
@RMI - what is the result with the first parameter at 1520?
AJ Henderson
CC BY-SA 3.0
2013-06-14T07:18:45.963Z
2s197 with or without keyframe
RMI
CC BY-SA 3.0
2013-06-14T13:29:20.593Z
@RMI Hmm, so it looks like you are getting one extra frame.  Bizar.  I'm out of ideas right now, but I'll post back if I can think of anything else.
AJ Henderson
CC BY-SA 3.0
2017-01-01T07:22:56.770Z
Can't agree with the "fundamental misunderstanding" part: a movie is a stream of audio frames and video (image) frames. The audio frames are typically very short. Assuming that it's about the audio sample rate is reasonable. (Though relying on that assumption is probably not good in general.) We trim to very high precision, and that's trimming the audio stream. Video frames either fall inside of the range of interest, or they don't. The audio is usually much more important to trim accurately, because a single video frame is hardly visible, but a few ms of audio can contain (un)wanted sounds.
Evgeni Sergeev
CC BY-SA 3.0
2017-01-01T15:29:12.977Z
@Evgeni Sergey - describing an audio sample as a frame isn't accurate. A sample is more similar to a pixel than a frame as it holds essentially no data without surrounding samples. While you are correct that audio can be trimmed more closely, the question was about video frames, not even necessarily an audio video file. We don't trim video based on audio. Video and audio are independent streams that are synchronized. You can't have a video file with a partial frame, it has to end on a full frame, though some formats can alter frame rate mid stream.
AJ Henderson
CC BY-SA 3.0
2017-01-01T15:56:41.903Z
@EvgeniSergeev Audio frames are usually 1000 samples for PCM and 1024 samples for AAC, so on the order of 21-25 ms. Not much different than typical video frame duration. Perhaps you are thinking of individual audio samples.
Gyan
CC BY-SA 3.0
2017-01-01T16:00:11.330Z
@Mulvya - interesting, I've never heard of audio samples being grouped in to frames, though I suppose it would make sense for synchronization purposes.  I don't do a whole lot specifically on audio for video though, so that isn't too surprising that I've missed that little technical detail.
AJ Henderson
CC BY-SA 3.0
2017-01-01T16:08:45.017Z
Every one understand video framing because pixels have two presentation properties on which they can differ i.e. time and space. So all pixels that share same timestamp but differ in spatial position are naturally grouped into a single frame. Audio samples only differ along the axis of time but they still have to be grouped (packetized) for interleaving and transmission purposes. At the user or NLE level, that info is useless, unlike pixel grouping. so not widely known.
Gyan
CC BY-SA 3.0
2013-06-11T13:43:16.067Z
2017-01-01T15:41:45.830Z
2017-01-01T15:41:45.830Z
AJ Henderson
<p>I need to trim videos with a precise end frame, however, <code>ffmpeg</code> seems to cut the video at the nearest first key-frame. So I don't have enough control over the ending frame.
I try to insert key-frame before trimming, but it doesn't work.
These are my command lines:</p>

<pre><code>ffmpeg -i &lt;sVideoFilename&gt; -force_key_frames 00:00:1:520 -vcodec wmv1 -acodec wmav2 -q:a 0 -q:v 0 &lt;sVideoFilenamekf&gt;

ffmpeg -i &lt;sVideoFilenamekf&gt; -ss 00:00:1:520 -to 00:00:3:640 -vcodec wmv1 -acodec wmav2 -q:v 0 -q:a 10 -r 30 &lt;sVideoFilenameTrimmed&gt;*
</code></pre>

<p>What's wrong with my command line ?
Thanks for your answer</p>

CC BY-SA 3.0
2013-06-12T13:28:17.290Z
I updated the question to reflect that you are looking for frame level precision rather than millisecond precision.  That may pull in more answers.  I don't personally know FFMPEG all that well as I always use other commercial encoders.
AJ Henderson
<video><ffmpeg>
How to trim a video with frame precision using ffmpeg?
RMI
# How to trim a video with frame precision using ffmpeg?

(Asked by: username_0 on 2013-06-11)

I need to trim videos with a precise end frame, however, ffmpeg seems to cut the video at the nearest first key-frame. So I don't have enough control over the ending frame.
I try to insert key-frame before trimming, but it doesn't work.
These are my command lines:
ffmpeg -i  -force_key_frames 00:00:1:520 -vcodec wmv1 -acodec wmav2 -q:a 0 -q:v 0 

ffmpeg -i  -ss 00:00:1:520 -to 00:00:3:640 -vcodec wmv1 -acodec wmav2 -q:v 0 -q:a 10 -r 30 *

What's wrong with my command line ?
Thanks for your answer

**Tags:** video, ffmpeg

> **Comments:**
> **username_1**: I updated the question to reflect that you are looking for frame level precision rather than millisecond precision.  That may pull in more answers.  I don't personally know FFMPEG all that well as I always use other commercial encoders.  

---

## 1 answers

### Answer by username_1 on 2013-06-11. Score: 2

You seem to have a fundamental misunderstanding of how video works. Video is composed of frames. You can't have millisecond precision because video doesn't have millisecond precision. If there are 30 frames per second in the video, you can only stop after one of those frames. That means your options are only every 1/30th of a second roughly.
If the format is group of pictures based it may be limited when it can end as well depending on the formats ability to end a group of pictures early. (A group of pictures is used in formats where some frames are stored as changes from other related frames rather than full images.)

What are you trying to accomplish that you need this precision for? Perhaps there may be an alternate way to accomplish your goal.

> **Comments:**
> **username_0**: I need to synchronize the video with another machine. The video is too much longer and i need to trim it (just need 2s). I don't have 1/30th of a second precision (~=33ms). I cut between 1062ms and 3640ms but i get a video length of 2130ms (not 2578 as planned). I can send you the video if you want to test it. Thanks for your answer.  
> **username_1**: @username_0 - Maybe I'm not reading your command lines correctly, but they say a start point of 1520ms, not 1062ms.  This makes the output of 2130ms correct.  
> **username_0**: If i trim with the first parameters (1520 to 3640) i have an length error too > than 1/30. I try with another video, it's the same problem. Perhaps the problem is the video codec.  
> **username_1**: @username_0 - what is the result with the first parameter at 1520?  
> **username_0**: 2s197 with or without keyframe  
> **username_1**: @username_0 Hmm, so it looks like you are getting one extra frame.  Bizar.  I'm out of ideas right now, but I'll post back if I can think of anything else.  
> **username_2**: Can't agree with the "fundamental misunderstanding" part: a movie is a stream of audio frames and video (image) frames. The audio frames are typically very short. Assuming that it's about the audio sample rate is reasonable. (Though relying on that assumption is probably not good in general.) We trim to very high precision, and that's trimming the audio stream. Video frames either fall inside of the range of interest, or they don't. The audio is usually much more important to trim accurately, because a single video frame is hardly visible, but a few ms of audio can contain (un)wanted sounds.  
> **username_1**: @Evgeni Sergey - describing an audio sample as a frame isn't accurate. A sample is more similar to a pixel than a frame as it holds essentially no data without surrounding samples. While you are correct that audio can be trimmed more closely, the question was about video frames, not even necessarily an audio video file. We don't trim video based on audio. Video and audio are independent streams that are synchronized. You can't have a video file with a partial frame, it has to end on a full frame, though some formats can alter frame rate mid stream.  
> **username_3**: @EvgeniSergeev Audio frames are usually 1000 samples for PCM and 1024 samples for AAC, so on the order of 21-25 ms. Not much different than typical video frame duration. Perhaps you are thinking of individual audio samples.  
> **username_1**: @Mulvya - interesting, I've never heard of audio samples being grouped in to frames, though I suppose it would make sense for synchronization purposes.  I don't do a whole lot specifically on audio for video though, so that isn't too surprising that I've missed that little technical detail.  
> **username_3**: Every one understand video framing because pixels have two presentation properties on which they can differ i.e. time and space. So all pixels that share same timestamp but differ in spatial position are naturally grouped into a single frame. Audio samples only differ along the axis of time but they still have to be grouped (packetized) for interleaving and transmission purposes. At the user or NLE level, that info is useless, unlike pixel grouping. so not widely known.  

---

