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.
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 – 2017-07-14T16:37:31.600
@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 – 2017-07-14T18:10:25.117I used mediainfo tool to compare the two files and notice differences in the header sizes and stuff – Ariana – 2017-07-14T18:10:52.107
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 – 2017-07-14T18:30:44.210
@slhck I uploaded the file. My question is updated – Ariana – 2017-07-14T19:43:33.230