Answers.Body: <p>So looked into <a href="https://www.ffmpeg.org/ffmpeg.html#Main-options" rel="nofollow noreferrer">ffmpeg documentation</a> and found that by changing position of -ss decodes and then creates the clip, which is way more faster than command posted in question</p>
  
  <pre><code>ffmpeg -i source.mp4 -ss 0:14:42.000 -codec copy -t 0:00:02.000 -y output.mp4
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-17T06:34:22.980Z
Answers.Text: Since you're copying, it doesn't matter, since there's no decoding but it's the other way around. `ss` before `i` is much faster,
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-17T07:16:38.653Z
Answers.Text: It does decoding, but it does not tries to re-encode. Please see documentation.
Answers.UserDisplayName: Nitishkumar Singh
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-17T07:19:42.353Z
Answers.Text: If frames were decoded, then they would be saved as raw uncompressed frames, if not re-encoded. That's not what happens with `-codec copy`.
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-16T19:47:44.847Z
Answers.LastActivityDate: 2018-02-17T07:16:53.693Z
Answers.LastEditDate: 2018-02-17T07:16:53.693Z
Answers.UserDisplayName: Nitishkumar Singh
Answers.Body: <p>The <a href="https://trac.ffmpeg.org/wiki/Seeking" rel="noreferrer">help for seeking using ffmpeg</a> explains the problem.</p>
  
  <blockquote>
    <p><strong>Seeking while doing a codec copy</strong></p>
    
    <p>Using -ss as input option together with -c:v copy <strong>might not be accurate</strong> since ffmpeg is forced to <strong>only use/split on i-frames</strong>. Though it will—if possible—adjust the start time of the stream to a negative value to compensate for that. Basically, if you specify "second 157" and there is no key frame until second 159, it will include two seconds of audio (with no video) at the start, then will start from the first key frame. So be careful when splitting and doing codec copy. </p>
  </blockquote>
  
  <p>If you transcode (not copy) then:</p>
  
  <blockquote>
    <p>As of FFmpeg 2.1, when transcoding with ffmpeg (i.e. not just stream copying), -ss is now also "frame-accurate" even when used as an input option.</p>
  </blockquote>
  
  <p>Your options are:</p>
  
  <ul>
  <li><p>Accept splits on i-frames and work with the output as-is, based on the options used. This is what you have now.</p></li>
  <li><p>Recompress:</p>
  
  <p><strong>Fast Input Seeking</strong>: The <strong>-ss</strong> parameter needs to be specified somewhere <strong>before -i</strong>. The input will be parsed using keyframes, which is very fast. </p>
  
  <blockquote>
    <p>ffmpeg -ss 00:23:00 -i Mononoke.Hime.mkv -frames:v 30 out1.mkv</p>
  </blockquote>
  
  <p><strong>Slow Output Seeking</strong>: The <strong>-ss</strong> parameter needs to be specified <strong>after -i</strong>. This will be done very slowly, frame by frame. As of FFmpeg 2.1, the main advantage is that when applying filters to the output stream, the timestamps aren't reset prior to filtering (i.e. when ​burning subtitles into a video, you don't need to modify the subtitle timestamps), but the drawback is that it will take a lot of time until it finally reaches that time point. </p>
  
  <blockquote>
    <p>ffmpeg -i Mononoke.Hime.mkv -ss 00:23:00 -frames:v 30 out2.mkv</p>
  </blockquote></li>
  <li><p>Adjust the start time forward so that when it ends on an i-frame the total duration is <strong>close to</strong> the desired duration.</p>
  
  <p><strong>Slow Combined Seeking</strong>: For this we specify the -ss parameter before and after -i. This approach uses keyframes to seek until 00:22:30, and then seeks frame-by-frame until it reaches 00:00:30 (00:22:30 + 00:00:01), continue increasing the second <strong>-ss</strong> option until the duration is short enough; unfortunately you'll lose an exact start and end time and the duration may only be approximately close to the desired amount.</p>
  
  <blockquote>
    <p>ffmpeg -ss 00:22:30 -i Mononoke.Hime.mkv -ss 00:00:01 -frames:v 30 out3.mkv</p>
  </blockquote>
  
  <p><strong>Note:</strong> Cutting small sections</p>
  
  <blockquote>
    <p>To extract only a small segment in the middle of a movie, it can be used in combination with -t which specifies the duration, like -ss 60 -t 10 to capture from second 60 to 70. Or you can use the -to option to specify an out point, like -ss 60 -to 70 to capture from second 60 to 70. -t and -to are mutually exclusive. If you use both, -t will be used.</p>
    
    <p>If you specify -ss before -i only, the timestamps will be reset to zero, so -t and -to have not the same effect. If you want to keep the original timestamps, add the -copytsoption.</p>
    
    <p>Cut from 00:01:00 to 00:03:00 (in the original), using the faster seek:</p>
  </blockquote>
  
  <pre><code>  ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy cut.mp4
  </code></pre>
  
  <blockquote>
    <p>Cut from 00:01:00 to 00:02:00, as intended, using the slower seek:</p>
  </blockquote>
  
  <pre><code>  ffmpeg -i video.mp4 -ss 00:01:00 -to 00:02:00 -c copy cut.mp4
  </code></pre>
  
  <blockquote>
    <p>Cut from 00:01:00 to 00:02:00, as intended, using the faster seek:</p>
  </blockquote>
  
  <pre><code>  ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy -copyts cut.mp4
  </code></pre>
  
  <blockquote>
    <p>If you cut with stream copy (-c copy) you need to use the <strong>-avoid_negative_ts 1</strong> option if you want to use that segment with the ​<strong>concat</strong> demuxer. Example:</p>
  </blockquote>
  
  <pre><code>  ffmpeg -ss 00:03:00 -i video.mp4 -t 60 -c copy -avoid_negative_ts 1 cut.mp4
  </code></pre>
  
  <blockquote>
    <p>Valid values for the <strong>avoid_negative_ts</strong> variable are:</p>
  </blockquote>
  
  <p><strong>avoid_negative_ts</strong> <strong><em>integer</em></strong> (output)</p>
  
  <pre><code>‘make_non_negative’
  
  Shift timestamps to make them non-negative. Also note that this affects only 
  leading negative timestamps, and not non-monotonic negative timestamps. 
  
  ‘make_zero’
  
  Shift timestamps so that the first timestamp is 0. 
  
  ‘auto’ (default)
  
  Enables shifting when required by the target format. 
  
  ‘disabled’
  
  Disables shifting of timestamp. 
  
  When shifting is enabled, all output timestamps are shifted by the same 
  amount. Audio, video, and subtitles desynching and relative timestamp
  differences are preserved compared to how they would have been without 
  Shifting. 
  </code></pre></li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-16T20:15:04.800Z
Answers.LastActivityDate: 2018-02-16T20:15:04.800Z
Answers.UserDisplayName: Rob
Body: <p>I am trying to create small clips from video file using <strong>ffmpeg</strong>3. The type of clip and video will be same i.e. mp4 to mp4 only. Below is command, which is being used for same.</p>
  
  <pre><code>ffmpeg -ss 00:07:00.000 -t 0:00:02.000 -i source.mp4 -codec copy -y output.mp4
  </code></pre>
  
  <p>The duration for clips generated is not same, they always have additional seconds with them i.e. 4,5 or 6 seconds. I have tried this on multiple videos and behavior is same.</p>
  
  <p>Since, the output and input type is same. So I am not using any encoding or decoding, as it will be time consuming process.</p>
  
  <p>Please share suggestions on same.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-10T18:50:46.167Z
Comments.Text: For videos with codecs that use temporal compression, it's not possible All frames that are required for decoding will be included in the output. So, if your inpoint isn't a keyframe, output duration will be longer. You need to re-encode to be assured of output duration.
Comments.UserDisplayName: Gyan
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-16T19:48:55.930Z
Comments.Text: You are right, we need to re-encode
Comments.UserDisplayName: Nitishkumar Singh
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-05T16:13:53.897Z
Comments.Text: correct, I was using `-c copy` while creating a clip from MKV using ffmpeg. When clip duration is small like 2seconds or 10 seconds then it was not picking up video stream. When duration is bigger like 35econds or 1minute then it produces audio+video streams in output file. Now, I have removed `-c copy` so it re-encodes and even 1second. 2second clips are fine now. thanks bunch!
Comments.UserDisplayName: Pranav
Tags: <ffmpeg>
Title: ffmpeg not creating exact duration clip
UserDisplayName: Nitishkumar Singh
ThreadText: # ffmpeg not creating exact duration clip
  
  (Asked by: username_0 on 2018-02-08)
  
  I am trying to create small clips from video file using **ffmpeg**3. The type of clip and video will be same i.e. mp4 to mp4 only. Below is command, which is being used for same.
  
  ```
  ffmpeg -ss 00:07:00.000 -t 0:00:02.000 -i source.mp4 -codec copy -y output.mp4
  
  ```
  
  The duration for clips generated is not same, they always have additional seconds with them i.e. 4,5 or 6 seconds. I have tried this on multiple videos and behavior is same.
  
  Since, the output and input type is same. So I am not using any encoding or decoding, as it will be time consuming process.
  
  Please share suggestions on same.
  
  **Tags:** ffmpeg
  
  > **Comments:**
  > **username_1**: For videos with codecs that use temporal compression, it's not possible All frames that are required for decoding will be included in the output. So, if your inpoint isn't a keyframe, output duration will be longer. You need to re-encode to be assured of output duration.  
  > **username_0**: You are right, we need to re-encode  
  > **username_2**: correct, I was using `-c copy` while creating a clip from MKV using ffmpeg. When clip duration is small like 2seconds or 10 seconds then it was not picking up video stream. When duration is bigger like 35econds or 1minute then it produces audio+video streams in output file. Now, I have removed `-c copy` so it re-encodes and even 1second. 2second clips are fine now. thanks bunch!  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2018-02-16. Score: 0 | Accepted answer
  
  So looked into [ffmpeg documentation](https://www.ffmpeg.org/ffmpeg.html#Main-options) and found that by changing position of -ss decodes and then creates the clip, which is way more faster than command posted in question
  
  ```
  ffmpeg -i source.mp4 -ss 0:14:42.000 -codec copy -t 0:00:02.000 -y output.mp4
  
  ```
  
  > **Comments:**
  > **username_1**: Since you're copying, it doesn't matter, since there's no decoding but it's the other way around. `ss` before `i` is much faster,  
  > **username_0**: It does decoding, but it does not tries to re-encode. Please see documentation.  
  > **username_1**: If frames were decoded, then they would be saved as raw uncompressed frames, if not re-encoded. That's not what happens with `-codec copy`.  
  
  ---
  
  ### Answer by username_3 on 2018-02-16. Score: 5
  
  The [help for seeking using ffmpeg](https://trac.ffmpeg.org/wiki/Seeking) explains the problem.
  
  > **Seeking while doing a codec copy**
  >
  > Using -ss as input option together with -c:v copy **might not be accurate** since ffmpeg is forced to **only use/split on i-frames**. Though it will—if possible—adjust the start time of the stream to a negative value to compensate for that. Basically, if you specify "second 157" and there is no key frame until second 159, it will include two seconds of audio (with no video) at the start, then will start from the first key frame. So be careful when splitting and doing codec copy.
  
  If you transcode (not copy) then:
  
  > As of FFmpeg 2.1, when transcoding with ffmpeg (i.e. not just stream copying), -ss is now also "frame-accurate" even when used as an input option.
  
  Your options are:
  
  * Accept splits on i-frames and work with the output as-is, based on the options used. This is what you have now.
  * Recompress:
  
    **Fast Input Seeking**: The **-ss** parameter needs to be specified somewhere **before -i**. The input will be parsed using keyframes, which is very fast.
  
    > ffmpeg -ss 00:23:00 -i Mononoke.Hime.mkv -frames:v 30 out1.mkv
  
    **Slow Output Seeking**: The **-ss** parameter needs to be specified **after -i**. This will be done very slowly, frame by frame. As of FFmpeg 2.1, the main advantage is that when applying filters to the output stream, the timestamps aren't reset prior to filtering (i.e. when ​burning subtitles into a video, you don't need to modify the subtitle timestamps), but the drawback is that it will take a lot of time until it finally reaches that time point.
  
    > ffmpeg -i Mononoke.Hime.mkv -ss 00:23:00 -frames:v 30 out2.mkv
  * Adjust the start time forward so that when it ends on an i-frame the total duration is **close to** the desired duration.
  
    **Slow Combined Seeking**: For this we specify the -ss parameter before and after -i. This approach uses keyframes to seek until 00:22:30, and then seeks frame-by-frame until it reaches 00:00:30 (00:22:30 + 00:00:01), continue increasing the second **-ss** option until the duration is short enough; unfortunately you'll lose an exact start and end time and the duration may only be approximately close to the desired amount.
  
    > ffmpeg -ss 00:22:30 -i Mononoke.Hime.mkv -ss 00:00:01 -frames:v 30 out3.mkv
  
    **Note:** Cutting small sections
  
    > To extract only a small segment in the middle of a movie, it can be used in combination with -t which specifies the duration, like -ss 60 -t 10 to capture from second 60 to 70. Or you can use the -to option to specify an out point, like -ss 60 -to 70 to capture from second 60 to 70. -t and -to are mutually exclusive. If you use both, -t will be used.
    >
    > If you specify -ss before -i only, the timestamps will be reset to zero, so -t and -to have not the same effect. If you want to keep the original timestamps, add the -copytsoption.
    >
    > Cut from 00:01:00 to 00:03:00 (in the original), using the faster seek:
  
    ```
      ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy cut.mp4
  
    ```
  
    > Cut from 00:01:00 to 00:02:00, as intended, using the slower seek:
  
    ```
      ffmpeg -i video.mp4 -ss 00:01:00 -to 00:02:00 -c copy cut.mp4
  
    ```
  
    > Cut from 00:01:00 to 00:02:00, as intended, using the faster seek:
  
    ```
      ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy -copyts cut.mp4
  
    ```
  
    > If you cut with stream copy (-c copy) you need to use the **-avoid\_negative\_ts 1** option if you want to use that segment with the ​**concat** demuxer. Example:
  
    ```
      ffmpeg -ss 00:03:00 -i video.mp4 -t 60 -c copy -avoid_negative_ts 1 cut.mp4
  
    ```
  
    > Valid values for the **avoid\_negative\_ts** variable are:
  
    **avoid\_negative\_ts** ***integer*** (output)
  
    ```
    ‘make_non_negative’
  
    Shift timestamps to make them non-negative. Also note that this affects only 
    leading negative timestamps, and not non-monotonic negative timestamps. 
  
    ‘make_zero’
  
    Shift timestamps so that the first timestamp is 0. 
  
    ‘auto’ (default)
  
    Enables shifting when required by the target format. 
  
    ‘disabled’
  
    Disables shifting of timestamp. 
  
    When shifting is enabled, all output timestamps are shifted by the same 
    amount. Audio, video, and subtitles desynching and relative timestamp
    differences are preserved compared to how they would have been without 
    Shifting. 
  
    ```
  
  
  ---
  
  