8

I'm trying to use FFmpeg to split video based on some start_time and end_time inputs, and replace those with other video files. For instance, for input 10 15 I will need to replace EXACT seconds 10 to 15 with another video.

OK. What comes into my mind is this:

  1. First split the video (in this case, 0-10, 10-15, and 15-end)
  2. Replace the 10-15 with the other video
  3. Merge all sub-videos back

First question: Is this the best and fastest solution? Do we have an easier command or other tools to replace a specific period of time in video with another video?

Second question: Assuming FFmpeg is the viable solution, when I simply use the command ffmpeg -I input.video -ss 10 -t 5 -c copy output.mp4, my video processing code can not read and process the file. I used mediainfo tool to compare the two files and noticed differences in the header sizes while all video codec related stuff was the same. I noticed the HEADER SIZE is different which is apparently causing the problem. Is there any ways to force the header size also (aside from codecs) be the same?

UPDATE: Here is the original .mp4 file which works fine in the emulator.

  • First we'd have to deal with the problem you mention in your second question. Please show the exact command you're using and the full, uncut command line output that you get from ffmpeg. Also show any actual errors you get when reading the file – it'd be good to know what your "processing code" is, exactly. – slhck Jul 14 '17 at 16:37
  • @slhck the video processing code is a huge code base. I can not share it. It is just that for some reasons the original file is read but not the re-generated one. I can however share the original .mp4 file if needed. – Ariana Jul 14 '17 at 18:10
  • I used mediainfo tool to compare the two files and notice differences in the header sizes and stuff – Ariana Jul 14 '17 at 18:10
  • Having the original would help, but we won't be able to troubleshoot why it can't be read, I'm afraid, if that is some internal code. – slhck Jul 14 '17 at 18:30
  • @slhck I uploaded the file. My question is updated – Ariana Jul 14 '17 at 19:43
10

Using trim and concat – source files with different codecs

For this, make sure that your individual files have the same resolution, and ideally same framerate and chroma subsampling etc. This will prevent errors or glitches during concatenation.

You can do everything in one go without splitting the file, using the trim and concat filters:

ffmpeg -i edv_g24.mp4 -i short-video.mp4 -filter_complex "\
[0:v]trim=0:10,setpts=PTS-STARTPTS[v0]; \
[1:v]trim=0:5,setpts=PTS-STARTPTS[v1]; \
[0:v]trim=15:30,setpts=PTS-STARTPTS[v2]; \
[v0][v1][v2]concat=n=3:v=1:a=0[out]" \
-map "[out]" output.mp4

Here, trim is used to specify the individual portions of the input video streams (0:v, 1:v) that you later want to concatenate. These parts are named v0 through v2. (The setpts filter resets the timestamps of these individual parts to 0, which is required for concatenation). Later, we concatenate the three parts.

If you want to trim from a certain timestamp to the end, use trim=start=15 instead of specifying the range.

If your files have audio, you have to trim those streams separately:

ffmpeg -i edv_g24_2.mp4 -i short-video.mp4 -filter_complex "\
[0:v]trim=0:10,setpts=PTS-STARTPTS[v0]; \
[0:a]atrim=0:10,asetpts=PTS-STARTPTS[a0]; \
[1:v]trim=0:5,setpts=PTS-STARTPTS[v1]; \
[1:a]atrim=0:5,asetpts=PTS-STARTPTS[v1]; \
[0:v]trim=15:30,setpts=PTS-STARTPTS[v2]; \
[0:a]atrim=15:30,asetpts=PTS-STARTPTS[a2]; \
[v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" output.mp4

Note that video and audio will be re-encoded in this case – make sure to specify appropriate output quality targets (e.g. -crf for x264, x265 or libvpx-vp9). You can read more about this on the FFmpeg Wiki, e.g. for VP9 or H.264.

Using individual segments

If you want to split the clips and later reassemble them:

ffmpeg -i edv_g24.mp4 -ss 0 -to 10 -c copy part1.mp4
ffmpeg -i edv_g24.mp4 -ss 10 -to 15 -c copy part2.mp4
ffmpeg -i edv_g24.mp4 -ss 15 -c copy part3.mp4

ffmpeg -i part1.mp4 -i short-video.mp4 -i part3.mp4 -filter_complex \
"[0:v][1:v][2:v]concat=n=3:v=1:a=0[outv]" \
-map "[outv]" -t 30 output.mp4

If the files have audio, use the same approach as above:

ffmpeg -i part1.mp4 -i short-video.mp4 -i part3.mp4 -filter_complex \
"[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1[outv][outa]" \
-map "[outv]" -map "[outa]" -t 30 output.mp4

This will again re-encode the video stream. It's a little more straightforward but otherwise should be equivalent to the above method.

Using concat demuxer

You can in principle also try to concatenate the bitstreams without re-encoding them, using the concat demuxer. Create a file called concat.txt and add the following entries (corresponding to the video clips created above):

file 'part1.mp4'
file 'short-video.mp4'
file 'part3.mp4'

Then concatenate these individual files:

ffmpeg -f concat -i concat.txt -c copy output.avi

This however requires your clips to have the same codec, resolution, framerate etc. – so it doesn't work with all kinds of heterogenous sources.

Using concat protocol

The above kind of file-level concatenation can be achieved using the concat protocol as well, with the same kind of constraints as above (same codec, resolution, etc.):

ffmpeg -i "concat:part1.avi|part2.avi|part3.avi" -c copy output.mp4

For more info on concatenation, read the respective FFmpeg Wiki page.

| improve this answer | |
  • This looks very complete! Thanks. I will try them! – Ariana Jul 15 '17 at 15:07
  • Idk why using the concat method throws an error: [concat @ 00000000025c2560] Line 1: unknown keyword 'part1.mp4 \\ text.txt: Invalid data found when processing input – Ariana Jul 17 '17 at 14:16
  • For "using individual segments", I see a halt and small cut out the output. Is there any ways to fix this? – Ariana Jul 17 '17 at 14:27
  • Method 1 works perfectly. One thing: how can we specify "until end" without stating a specific number (instead of 30 s) – Ariana Jul 17 '17 at 15:00
  • 1
    Sorry, made a mistake. For the concat demuxer, the syntax was different. Fixed now. To go until the end, use trim=start=15, see the trim filter documentation: ffmpeg.org/ffmpeg-filters.html#trim -- as for timing issues, this really depends on your source material. Haven't had issues concatenating though with the clip you uploaded. – slhck Jul 17 '17 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.