4

I am trying to concat multiple clips using FFMPEG

My input videos have the following details:

  1. input1.mp4
ffprobe version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2007-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
  Duration: 00:05:11.75, start: 0.000000, bitrate: 1338 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 1204 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc. Created on: 11/09/2018.
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
    Metadata:
      handler_name    : ISO Media file produced by Google Inc. Created on: 11/09/2018.
  1. input2.mp4
ffprobe version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2007-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
  Duration: 00:00:05.62, start: 0.000000, bitrate: 2479 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1260x720 [SAR 1:1 DAR 7:4], 2376 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

I have tried 2 approaches but none seemed to work properly.

Approach #1: Using ffmpeg concat filter

Steps:

ffmpeg -i input1.mp4 -qscale:v 1 intermediate1.mpg
ffmpeg -i input2.mp4 -qscale:v 1 intermediate2.mpg
cat intermediate1.mpg intermediate2.mpg > intermediate_all.mpg
ffmpeg -i intermediate_all.mpg -qscale:v 2 output.mp4

Approach #2: Using ffmpeg concat demuxer along with timebase handling

Steps:

ffmpeg -i input1.mp4 -s hd720 -r 30000/1001 -video_track_timescale 30k -c:a copy input1-converted.mp4
ffmpeg -i input2.mp4 -s hd720 -r 30000/1001 -video_track_timescale 30k -c:a copy input2-converted.mp4
ffmpeg -f concat -safe 0 -i vidlist.txt -c copy output.mp4

References:

Expectations:

One resultant clip with both inputs merged one after another with proper length(sum of inputs) and no video freeze/audio loss.

Results:

One resultant clip with both inputs merged one after another with lengthy duration(more than the sum of inputs) and video freeze and sometimes audio loss as well.

llogan
  • 121,796
  • 28
  • 232
  • 243

2 Answers2

1

Your inputs must have the same parameters, but yours vary in width, frame rate, timescale, audio sample rate, and audio channel layout. Since input1.mp4 is the main feature you can re-encode input2.mp4 to match the properties of input1.mp4:

ffmpeg -i input2.mp4 -filter_complex "[0:v]pad=1280:720:-1:-1,fps=30000/1001[v];[0:a]aformat=sample_rates=44100:channel_layouts=stereo[a]" -map "[v]" -map "[a]" input2b.mp4

Then use the concat demuxer to concatenate them. Create input.txt:

file 'input1.mp4'
file 'input2b.mp4'

Concatenate:

ffmpeg -f concat -i input.txt -c copy output.mp4
  • This leaves input1.mp4 untouched: the process is faster, and preserves the quality of input1.mp4.

  • Even better is to re-create a new input2.mp4 from the original source while setting the proper parameters. This will avoid generation loss from re-encoding a new file from it.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Hey @llogan. It did work for the given inputs. Thanks for your reply. The inputs I have will be dynamic i.e. their parameters(fps, audio channel layout etc) might be different than the ones mentioned above. Also, the number of inputs can vary. E.g: Now I have some 20 clips that need to be merged with input1.mp4 having different parameters to the one supplied in the question above. What do you suggest in this case? Should I take out the parameters for input1 and then re-encode others accordingly or just re-encode all with fixed parameters so that checking input1 every time is not required. – Noyan Ahmed Mar 04 '20 at 08:06
  • @NoyanAhmed In that case I recommend you convert them all to a common set of attributes. It will be easier than trying to match the main file. See [How to concatenate videos in ffmpeg with different attributes?](https://stackoverflow.com/a/57367243/1109017) and [FFmpeg - Concatenate videos with not know format for examples](https://stackoverflow.com/a/42868053/1109017). – llogan Mar 04 '20 at 20:13
0

try this

ffmpeg -i input1-converted.mp4 -i input2-converted.mp4 -filter_complex "concat=n=2:v=1:a=1" output.mp4

n=2 (inputs).

v=1: 1 video stream from each video.

a=1 1 audio stream from each video.

Siddhesh A
  • 41
  • 2
  • Getting following error: `[Parsed_concat_0 @ 0x55eec7dacbc0] Input link in1:v0 parameters (size 1280x720, SAR 63:64) do not match the corresponding output link in0:v0 parameters (1280x720, SAR 1:1) [Parsed_concat_0 @ 0x55eec7dacbc0] Failed to configure output pad on Parsed_concat_0 Error reinitializing filters! Failed to inject frame into filter network: Invalid argument Error while processing the decoded data for stream #1:0 Conversion failed!` – Noyan Ahmed Mar 04 '20 at 06:54