Answers.Body: <p>This should work faster...</p>
  
  <pre class="lang-bash prettyprint-override"><code>ffmpeg -ss 1:00 -t 5 -i in.mkv -ss 2:00 -t 5 -i in.mkv \
    -filter_complex "[0][1]concat=n=2:v=1:a=1" \ 
    out.mkv
  </code></pre>
  
  <p>There's no need to specify -t on the output.  There's no need to split the audio and video before the concat.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-05T00:22:47.460Z
Answers.Text: This uses the concat filter which will require re-encoding. This may or may not be desired by the user.
Answers.UserDisplayName: llogan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-04T16:55:17.697Z
Answers.LastActivityDate: 2015-06-12T00:45:19.833Z
Answers.LastEditDate: 2015-06-12T00:45:19.833Z
Answers.UserDisplayName: William Frantz
Answers.Body: <h1>concat filter</h1>
  
  <p>This method is best if you need to perform additional filtering:</p>
  
  <p>Use the <a href="http://ffmpeg.org/ffmpeg-filters.html#trim"><code>trim</code></a>, <a href="http://ffmpeg.org/ffmpeg-filters.html#atrim"><code>atrim</code></a>, <a href="http://ffmpeg.org/ffmpeg-filters.html#setpts_002c-asetpts"><code>setpts</code></a>, <a href="http://ffmpeg.org/ffmpeg-filters.html#setpts_002c-asetpts"><code>asetpts</code></a>, and <a href="http://ffmpeg.org/ffmpeg-filters.html#concat"><code>concat</code></a> filters:</p>
  
  <pre class="lang-bash prettyprint-override"><code>ffmpeg -i input -filter_complex \
  "[0:v]trim=60:65,setpts=PTS-STARTPTS[v0]; \
   [0:a]atrim=60:65,asetpts=PTS-STARTPTS[a0]; \
   [0:v]trim=120:125,setpts=PTS-STARTPTS[v1];
   [0:a]atrim=120:125,asetpts=PTS-STARTPTS[a1]; \
   [v0][a0][v1][a1]concat=n=2:v=1:a=1[out]" \
  -map "[out]" output.mkv
  </code></pre>
  
  <ul>
  <li><p><code>setpts</code> and <code>asetpts</code> will prevent a jerky output due to presentation timestamp issues.</p></li>
  <li><p>Make sure to use a recent version. See the <a href="http://ffmpeg.org/download.html">FFmpeg Download</a> page for links to builds for Windows, OS X, and Linux.</p></li>
  </ul>
  
  <hr>
  
  <h1>concat demuxer</h1>
  
  <p>Another method is to to create the segments individually and <a href="http://ffmpeg.org/ffmpeg.html#Stream-copy">stream copy</a> them instead of re-encoding (to save time and quality), and join them with the <a href="http://ffmpeg.org/ffmpeg-formats.html#concat-1">concat demuxer</a>.</p>
  
  <pre class="lang-bash prettyprint-override"><code>$ ffmpeg -ss 60 -i input -t 5 -codec copy clip1.mkv
  $ ffmpeg -ss 120 -i input -t 5 -codec copy clip2.mkv
  $ echo "file 'clip1.mkv'" &gt; concat.txt
  $ echo "file 'clip2.mkv'" &gt;&gt; concat.txt
  $ ffmpeg -f concat -i concat.txt -codec copy output.mkv
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-03-18T04:05:49.903Z
Answers.Text: Ah, thanks.  trim is what I was missing.  However, it takes a very long time to get to the time offset I specify, as if decoding everything up to it.  Also, part of the output was missing audio.  To fix, I specified the same input multiple times with -ss, then use trim to truncate the video.  You don't need setpts in that case, since ss sets pts to 0.  That seemed to work and didn't have weird random pauses during encoding.  As for copying streams, I tried that before.  It starts at the previous I-frame, so you get a different part of the movie than the one you wanted.
Answers.UserDisplayName: scubed
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-26T15:43:01.900Z
Answers.Text: Would you please answer my question it is similar to this one. http://video.stackexchange.com/questions/18247/ffmpeg-concat-demuxer-corrupted-output-freezed-video-on-some-concatenated-parts
Answers.UserDisplayName: Eftekhari
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-13T20:24:52.333Z
Answers.Text: Interestingly the `-filter_complex` works but the `concat` files doesn't - i.e. the 1st video looks fine but the quality of the 2nd is horrible.  Note, I copied these commands exactly for both (except for the input file).
Answers.UserDisplayName: bobmarksie
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-29T21:18:13.320Z
Answers.Text: How can I trim clip by frames and concatenate?
Answers.UserDisplayName: puneet18
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-29T22:41:15.547Z
Answers.Text: @puneet18 Are you using *concat filter* or *concat demuxer*?
Answers.UserDisplayName: llogan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-30T07:10:56.000Z
Answers.Text: @llogan can you please suggest any way to trim video by multiple frame numbers and then concatenate it
Answers.UserDisplayName: puneet18
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-30T18:13:13.230Z
Answers.Text: @puneet18 Answer depends on the concatenation method you intend to use. What is the concatenation method you intend to use?
Answers.UserDisplayName: llogan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-30T18:40:59.960Z
Answers.Text: @llogan I don't know about concat filter or concat demuxer. could you please suggest what the best way?
Answers.UserDisplayName: puneet18
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-30T18:42:53.957Z
Answers.Text: @llogan can you please check my question on https://stackoverflow.com/questions/63656881/how-to-trim-video-by-multiple-frame-numbers-then-concatenate-using-ffmpeg
Answers.UserDisplayName: puneet18
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-22T00:29:18.233Z
Answers.Text: How can I with a single command cut into separate files where some of the output files have jump cuts, that is, they're concatenations of some of the cut segments? For example, I want to with a single command (so there's only one seeking through the input file) cut out segments a, b, c, and d and create output files where the first one is concatenation of a+b and the second, c+d?
Answers.UserDisplayName: Display Name
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-03-17T01:09:45.547Z
Answers.LastActivityDate: 2015-06-06T17:06:24.550Z
Answers.LastEditDate: 2015-06-06T17:06:24.550Z
Answers.UserDisplayName: llogan
Body: <p>I have a long video with different scenes in it.
  I want to extract and concatenate 2 scenes from the video (that do not start on an I-frame) using ffmpeg 2.1.4.</p>
  
  <p>For argument, say I want 5 seconds from 01:00 and 02:00.</p>
  
  <p>I can do this:</p>
  
  <pre class="lang-bash prettyprint-override"><code>ffmpeg -ss 01:00 -i in.mkv -ss 02:00 -i in.mkv -filter_complex "
    [0:v]select='lt(t,5)'[v0];
    [0:a]aselect='lt(t,5)'[a0];
    [v0][a0][1:v][1:a]concat=n=2:v=1:a=1
  " -c:a libvorbis -t 10 out.mkv
  </code></pre>
  
  <p>That gives me the movie I want, but select actually forces ffmpeg to decode the entire rest of the movie.  So, how do I tell it to truncate the first movie instead of decoding it?</p>
  
  <p>I would prefer to do this with an ffmpeg complex filter, if possible.  I know that I can just use <code>-t</code> to recode separate movies first, but the extra encoding step is very slow in my case and also loses a fair amount of quality for this video.</p>
  
  <p>I can't seem to find a filter that truncates the length of a steam.  Are there filter nodes that correspond to the <code>-t</code> or <code>-ss</code> parameters?</p>
  
Tags: <ffmpeg>
Title: How to concatenate clips from the same video with ffmpeg
UserDisplayName: scubed
ThreadText: # How to concatenate clips from the same video with ffmpeg
  
  (Asked by: username_0 on 2014-03-15)
  
  I have a long video with different scenes in it.
  I want to extract and concatenate 2 scenes from the video (that do not start on an I-frame) using ffmpeg 2.1.4.
  
  For argument, say I want 5 seconds from 01:00 and 02:00.
  
  I can do this:
  
  ```bash
  ffmpeg -ss 01:00 -i in.mkv -ss 02:00 -i in.mkv -filter_complex "
    [0:v]select='lt(t,5)'[v0];
    [0:a]aselect='lt(t,5)'[a0];
    [v0][a0][1:v][1:a]concat=n=2:v=1:a=1
  " -c:a libvorbis -t 10 out.mkv
  
  ```
  
  That gives me the movie I want, but select actually forces ffmpeg to decode the entire rest of the movie. So, how do I tell it to truncate the first movie instead of decoding it?
  
  I would prefer to do this with an ffmpeg complex filter, if possible. I know that I can just use `-t` to recode separate movies first, but the extra encoding step is very slow in my case and also loses a fair amount of quality for this video.
  
  I can't seem to find a filter that truncates the length of a steam. Are there filter nodes that correspond to the `-t` or `-ss` parameters?
  
  **Tags:** ffmpeg
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2014-03-17. Score: 25 | Accepted answer
  
  # concat filter
  
  This method is best if you need to perform additional filtering:
  
  Use the [`trim`](http://ffmpeg.org/ffmpeg-filters.html#trim), [`atrim`](http://ffmpeg.org/ffmpeg-filters.html#atrim), [`setpts`](http://ffmpeg.org/ffmpeg-filters.html#setpts_002c-asetpts), [`asetpts`](http://ffmpeg.org/ffmpeg-filters.html#setpts_002c-asetpts), and [`concat`](http://ffmpeg.org/ffmpeg-filters.html#concat) filters:
  
  ```bash
  ffmpeg -i input -filter_complex \
  "[0:v]trim=60:65,setpts=PTS-STARTPTS[v0]; \
   [0:a]atrim=60:65,asetpts=PTS-STARTPTS[a0]; \
   [0:v]trim=120:125,setpts=PTS-STARTPTS[v1];
   [0:a]atrim=120:125,asetpts=PTS-STARTPTS[a1]; \
   [v0][a0][v1][a1]concat=n=2:v=1:a=1[out]" \
  -map "[out]" output.mkv
  
  ```
  
  * `setpts` and `asetpts` will prevent a jerky output due to presentation timestamp issues.
  * Make sure to use a recent version. See the [FFmpeg Download](http://ffmpeg.org/download.html) page for links to builds for Windows, OS X, and Linux.
  
  ---
  
  # concat demuxer
  
  Another method is to to create the segments individually and [stream copy](http://ffmpeg.org/ffmpeg.html#Stream-copy) them instead of re-encoding (to save time and quality), and join them with the [concat demuxer](http://ffmpeg.org/ffmpeg-formats.html#concat-1).
  
  ```bash
  $ ffmpeg -ss 60 -i input -t 5 -codec copy clip1.mkv
  $ ffmpeg -ss 120 -i input -t 5 -codec copy clip2.mkv
  $ echo "file 'clip1.mkv'" > concat.txt
  $ echo "file 'clip2.mkv'" >> concat.txt
  $ ffmpeg -f concat -i concat.txt -codec copy output.mkv
  
  ```
  
  > **Comments:**
  > **username_0**: Ah, thanks.  trim is what I was missing.  However, it takes a very long time to get to the time offset I specify, as if decoding everything up to it.  Also, part of the output was missing audio.  To fix, I specified the same input multiple times with -ss, then use trim to truncate the video.  You don't need setpts in that case, since ss sets pts to 0.  That seemed to work and didn't have weird random pauses during encoding.  As for copying streams, I tried that before.  It starts at the previous I-frame, so you get a different part of the movie than the one you wanted.  
  > **username_3**: Would you please answer my question it is similar to this one. http://video.stackexchange.com/questions/18247/ffmpeg-concat-demuxer-corrupted-output-freezed-video-on-some-concatenated-parts  
  > **username_4**: Interestingly the `-filter_complex` works but the `concat` files doesn't - i.e. the 1st video looks fine but the quality of the 2nd is horrible.  Note, I copied these commands exactly for both (except for the input file).  
  > **username_5**: How can I trim clip by frames and concatenate?  
  > **username_2**: @username_5 Are you using *concat filter* or *concat demuxer*?  
  > **username_5**: @username_2 can you please suggest any way to trim video by multiple frame numbers and then concatenate it  
  > **username_2**: @username_5 Answer depends on the concatenation method you intend to use. What is the concatenation method you intend to use?  
  > **username_5**: @username_2 I don't know about concat filter or concat demuxer. could you please suggest what the best way?  
  > **username_5**: @username_2 can you please check my question on https://stackoverflow.com/questions/63656881/how-to-trim-video-by-multiple-frame-numbers-then-concatenate-using-ffmpeg  
  > **username_6**: How can I with a single command cut into separate files where some of the output files have jump cuts, that is, they're concatenations of some of the cut segments? For example, I want to with a single command (so there's only one seeking through the input file) cut out segments a, b, c, and d and create output files where the first one is concatenation of a+b and the second, c+d?  
  
  ---
  
  ### Answer by username_1 on 2015-06-04. Score: 8
  
  This should work faster...
  
  ```bash
  ffmpeg -ss 1:00 -t 5 -i in.mkv -ss 2:00 -t 5 -i in.mkv \
    -filter_complex "[0][1]concat=n=2:v=1:a=1" \ 
    out.mkv
  
  ```
  
  There's no need to specify -t on the output. There's no need to split the audio and video before the concat.
  
  > **Comments:**
  > **username_2**: This uses the concat filter which will require re-encoding. This may or may not be desired by the user.  
  
  ---
  
  