7

I have two videos, and I need to drop both ends for both videos and leave only middle part, and then concatenate middle parts together. Both videos are exactly the same origin - same FPS and same codec.

What I am trying to do:

ffmpeg -i 0.mp4 -c:v h264_nvenc -preset fast -rc vbr -qmin 1 -qmax 1 -filter_complex "[0:v]trim=start=01.00:duration=03.00,setpts=PTS-STARTPTS[vmiddle]" -map [vmiddle] middle_0.mp4
ffmpeg -i 1.mp4 -c:v h264_nvenc -preset fast -rc vbr -qmin 1 -qmax 1 -filter_complex "[0:v]trim=start=01.00:duration=03.00,setpts=PTS-STARTPTS[vmiddle]" -map [vmiddle] middle_1.mp4

These two commands cut only middle part from each video, starting at 01.00 and ending at 04.00.

Then I concatenate videos:

ffmpeg -y -f concat -i concat.txt -c copy concat.mp4

With concat.txt file obviously containing:

file 'middle0.mp4'
file 'middle1.mp4'

But this does not work. The output video concat.mp4 contains weird glitches and I get a lot of errors in console:

[mov,mp4,m4a,3gp,3g2,mj2 @ 00000000025ead60] Auto-inserting h264_mp4toannexb bitstream filter
[concat @ 0000000000dd6bc0] DTS 34666 < 45568 out of order
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45568, current: 34666; changing to 45569. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45569, current: 35050; changing to 45570. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45570, current: 35434; changing to 45571. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45571, current: 35818; changing to 45572. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45572, current: 36202; changing to 45573. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45573, current: 36586; changing to 45574. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45574, current: 36970; changing to 45575. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45575, current: 37354; changing to 45576. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45576, current: 37738; changing to 45577. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45577, current: 38122; changing to 45578. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45578, current: 38506; changing to 45579. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45579, current: 38890; changing to 45580. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45580, current: 39274; changing to 45581. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45581, current: 39658; changing to 45582. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45582, current: 40042; changing to 45583. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45583, current: 40426; changing to 45584. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45584, current: 40810; changing to 45585. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45585, current: 41194; changing to 45586. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45586, current: 41578; changing to 45587. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45587, current: 41962; changing to 45588. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45588, current: 42346; changing to 45589. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45589, current: 42730; changing to 45590. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45590, current: 43114; changing to 45591. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45591, current: 43498; changing to 45592. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45592, current: 43882; changing to 45593. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45593, current: 44266; changing to 45594. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45594, current: 44650; changing to 45595. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45595, current: 45034; changing to 45596. This may result in incorrect timestamps in the output file.
[mp4 @ 0000000000ddbfe0] Non-monotonous DTS in output stream 0:0; previous: 45596, current: 45418; changing to 45597. This may result in incorrect timestamps in the output file.

Note, I am already setting presentation timestamp with setpts=PTS-STARTPTS so each video starts at 00.00. But how to fix this non-monotonous DTS? I am really stuck, please help.

I know that I could fix this with re-encoding video during concatenation, but I really can't afford this. This step is a part of long pipeline and time is crucial, I have to do it with copy codec.

Added:

Requested ffprobe output for both videos. I know little about ffprobe output, could you please explain what's going on and why is there a discrepancy and what does it mean? (512 steps in 1st video and 384 steps in 2nd video)

Middle0 (first video)

Middle1 (second video)

2
  • 2
    You can do the trim and concat in one step, thus only one encode cycle. But that's a workaround. To solve this issue, run ffprobe -show_streams -show_entries packet=pts -of compact=p=0:nk=1 video.mp4 > video.txt for both files and link them here.
    – Gyan
    Commented Nov 27, 2016 at 16:33
  • I added ffprobe output and it already gives some hints, thank you. If you could help me interpreting it, it would be great) Commented Nov 28, 2016 at 9:21

2 Answers 2

5

Use

ffmpeg -i 0.mp4 -c:v h264_nvenc -preset fast -rc vbr -qmin 1 -qmax 1 -filter_complex "[0:v]trim=start=01.00:duration=03.00,setpts=PTS-STARTPTS[vmiddle]" -video_track_timescale 18000 -map [vmiddle] middle_0.mp4

Those numbers in the ffprobe readout are presentation timestamps and inform the player when to display each frame. They are denominated in terms of a timebase. So, a PTS of 600 with a timebase of 1/1200 (or timescale of 1200), means a display time of 600 * 1/1200 = 0.5 seconds, as does a PTS of 700 with a timescale of 1400. Now, when two videos with different timebases are joined, the result won't be correct since ffmpeg will adopt the timebase of the first video as the definitive value. I added a parameter to make those uniform.

7
  • It now does not play on iOS devices, neither in Safari browser nor in QuickTime. But plays perfectly on Windows and Linux. Maybe there's something wrong with the value 18000, where does it come from? Commented Nov 29, 2016 at 15:09
  • If you know the input framerate, you can just use that same value or that value x 512. Don't have a Mac so can't check myself.
    – Gyan
    Commented Nov 29, 2016 at 15:17
  • I thought my fps is 30, so I tried 30 and 15360, and it didn't work (exact same effect, works on Windows, does not work on iOS). Then I looked at ffmpeg output, for one video it says 29.85fps, 29.83tbr, 30k tbn, 60k tbc (default). For another it says 29.87fps, 29.83tbr, 30k tbn, 60k tbc (default). My videos come from screen capturing tool, so that's why fps is slightly different? Which value should I choose? Commented Nov 29, 2016 at 15:29
  • I don't think this is related to the timescale. Show the result of ffprobe concat.mp4
    – Gyan
    Commented Nov 29, 2016 at 15:31
  • pastebin.com/tP2fHYvX here it is. Not exactly the same video, but done by same scripts. Commented Nov 29, 2016 at 15:37
0

As per https://trac.ffmpeg.org/wiki/Concatenate

I had this same issue. I'm running ffmpeg version 2.6.3 on a mac. I converted my source clips to the .MTS format with an added tag of -q 0, updated the extensions in the text file and set my output file to a .MTS when I ran the concat command in ffmpeg. It worked like a charm. Here is my workflow.

ffmpeg -i clip-1.MOV -q 0 clip-1.MTS

ffmpeg -i clip-2.MOV -q 0 clip-2.MTS

updated the text file (mylist.txt) to read:

file clip-1.MTS
file clip-2.MTS

then ran the concat command as follows:

ffmpeg -f concat -i mylist.txt -c copy output.MTS

Worked for me! You can convert the final output.MTS to whatever format you need.

2
  • This certainly succeeds, if the input files contain no non-monotonous timestamps. Even if they do contain some (typically, this will be only at the join between input files) the suggested command will still succeed up to a point: the files will appear to concatenate correctly, the output will certainly play, and the audio and video will remain in-sync; but a careful examination of the join points will nevertheless reveal a small glitch (on both sound and picture) caused by a tiny pause in the playback. This might be acceptable, to many users, but is really a workaround rather than a solution.
    – Ed999
    Commented Jan 4, 2018 at 10:04
  • On some of my tests, I have had to use the following variation in order for this workaround to succeed (but be aware that, sometimes though not in all cases, explicitly including "-flags global_header" causes this to fail, so try omitting it if you find the command doesn't work): ffmpeg -i 1.mov -c copy 1.ts && ffmpeg -i 2.mov -c copy 2.ts && ffmpeg -i "concat:1.ts|2.ts" -c copy -bsf:a aac_adtstoasc -flags global_header -movflags faststart output.mp4
    – Ed999
    Commented Jan 4, 2018 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .