3

To concat two mp4 or ts files (I've both formats) ffmpeg offers some calls that I've tried. Both "concat protocol" (for h264 TS files) and "Concat demuxer" (for h264 PS files) produce artifacts that are unpleasant to watch:

  • Concat protocol with AVC (MPEG-TS - High profile) pixelates about 5 frames during the transition
  • Concat demux with AVC (MPEG-PS - High Profile) skips frames and mixes some frames from the second clip into the first.

My goal was to copy the packages, not to remux them. So the calls were for the concat protocol

ffmpeg -i "concat:clip1.ts|clip2.ts" -c copy out.m2t

and the concat demuxer:

ffmpeg -hide_banner -y  -f  concat  -safe 0 -i merge.txt -c copy out.mp4

Below the codec information of the two clips:

ffmpeg -n -i "clip1.mp4" -i "clip2.mp4" 
ffmpeg version n4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 9.3.0 (Arch Linux 9.3.0-1)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmfx --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-nvdec --enable-nvenc --enable-omx --enable-shared --enable-version3
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'clip1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 01:28:05.12, start: 0.000000, bitrate: 8714 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 8253 kb/s, 50 fps, 50 tbr, 90k tbn, 100 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(deu): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, stereo, fltp, 448 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
    Side data:
      audio service type: main
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'clip2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:03:05.68, start: 0.000000, bitrate: 8680 kb/s
    Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 8223 kb/s, 50 fps, 50 tbr, 90k tbn, 100 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #1:1(deu): Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, stereo, fltp, 448 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
    Side data:
      audio service type: main

The "cat" option does not work for codecs with timestamps (which AVC/h264 has). Since I'm reluctant to remux the clips I wonder if there is another way to achieve a clean merge

7
  • You may want to clarify: first lines mention mp4, then you provide examples for .ts streams. Is your ffmpeg -i option valid here? As far as I know, it should be -f concat.
    – vanadium
    Apr 18, 2020 at 14:04
  • @vanadium - thanks for the tip. It wasn't clear that I've had both types of streams. @ llogan - I've added that information
    – kanehekili
    Apr 18, 2020 at 19:35
  • Did you try to use the concat demuxer with these MP4 files? It is unclear in your question. Reads like you converted to MPEG-PS first. Try the concat demuxer with the MP4 files if you haven't already. They have the same attributes so I expect it to work.
    – llogan
    Apr 19, 2020 at 19:15
  • The original files are TS files. I've converted them to PS files. So I've got both "flavours".. As I've have written above: The TS files pixelates, the PS files (implicitly using the h264_mp4toannexb filter during remuxing - not transcoding) show an intertwined pattern of "Frames of clip2 and clip1". So yes. I've tried both ways - just as described: Conact demuxer for PS and Concat protocol for TS.
    – kanehekili
    Apr 19, 2020 at 20:07
  • @llogan - you were right, the first sentence in my question was not correct, I fixed it now
    – kanehekili
    Apr 19, 2020 at 20:15

1 Answer 1

0

mp4 is not mpeg. Thus, ffmpeg requires mapping.

To use ffmpeg to concat various a/v files, enter the following in terminal:

ffmpeg -i input1.mp4 -i input2.webm -i input3.mov \ -filter_complex "[0:v:0][0:a:0][1:v:0][1:a:0][2:v:0][2:a:0]concat=n=3:v=1:a=1[outv][outa]" \ -map "[outv]" -map "[outa]" output.mkv

notes . If all files are mp4, them change the file extensions to mp4. . If more than three input files (eg 4), then change n=3 to n=4. If 2 files, then n=2. etc.

2
  • Not various codecs, they are all the same. The question was asked because I do NOT want to reencode. But I've written a muxer that can handle it. And mp4 is not mpeg (the former a container, the latter a codec). In my example the codec is AVC/h264 - I did not mention mpeg (which is easily to concat)
    – kanehekili
    May 13 at 19:15
  • I have seen too many instances where people have asked how to use ffmpeg to append a/v files, only to read too many incorrect responses (in fact, no correct responses). The command noted will work in all cases. True, it re-encodes the file. So you have compiled a muxer that appends without re-encode? Great! Where may I find this to try?
    – rob grune
    May 14 at 1:50

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.