Search code examples
videoffmpegconcatenation

ffmpeg: Concatenating videos with different formats with as little quality loss as possible


I have sets of video segments with different formats that need to be concatenated and uploaded to a database as a single video. I need to do this with as little quality loss as possible. For example, one set is:

video_1.mov - 796 frames, 432Mb,
video_2.mp4 - 913 frames, 58Mb,
video_3.mov - 625 frames, 345Mb

This should be concatenated into one file: output.mov - 2334 frames, ?Mb

So far I am doing this:

  1. Convert the .mp4 to .mov with lossless encoding:

ffmpeg -i video_2.mp4 -c:v libx264 -preset veryslow -crf 0 -c:a copy video_2.mov

an error is produced during this step:

[h264 @ 0x558298795760] error while decoding MB 110 37, bytestream -69483.1kbits/s speed=0.227x     
[h264 @ 0x558298795760] concealing 3659 DC, 3659 AC, 3659 MV errors in P frame

This still outputs video_2.mov - frames 913, 112Mb

  1. Use concat demuxer to combine the videos:

ffmpeg -f concat -safe 0 -i mylist.txt -c copy output.mov

where mylist.txt is:

file 'video_1.mov'
file 'video_2.mov'
file 'video_3.mov'

However, the concat demuxer command gives the error:

[concat @ 0x561cbae96900] Could not find codec parameters for stream 2 (Unknown: none): unknown codec.Consider increasing the value for the 'analyzeduration' and 'probesize' options

Followed by many lines of:

[mov @ 0x555b5dca4f00] Non-monotonous DTS in output stream 0:1; previous: 1527575, current: 1168413; changing to 1527576. This may result in incorrect timestamps in the output file.

The result of these steps is a output.mov which is the correct size of 432+345+112=889Mb but only has 1421 frames equal to the frames from video_1.mov - 796 frames and video_3.mov - 625 frames. When I play the concatenated version, output.mov, it freezes for the duration of the video_2.mov segment and then continues as normal for video_3.mov.

Any help with this would be hugely appreciated.

EDIT:


Running ffmpeg -n -i video_1.mov -i video_2.mp4 -i video_3.mov per the comments produced:

`ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-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 'video_1.mov':
  Metadata:
    major_brand     : qt  
    minor_version   : 0
    compatible_brands: qt  
    creation_time   : 2019-11-16T09:57:04.000000Z
    keywords        : iMovie
    description     : 
    title           : 
    com.apple.quicktime.description: 
    com.apple.quicktime.displayname: 
    com.apple.quicktime.keywords: iMovie
    com.apple.quicktime.title:
  Duration: 00:00:31.84, start: 0.000000, bitrate: 108530 kb/s
    Stream #0:0(eng): Audio: pcm_s16be (lpcm / 0x6D63706C), 48000 Hz, stereo, s16, 1536 kb/s (default)
    Metadata:
      creation_time   : 2019-11-16T09:57:04.000000Z
      handler_name    : Core Media Data Handler
    Stream #0:1(und): Video: prores (apcn / 0x6E637061), yuv422p10le(bt709, progressive), 1920x1080, 106992 kb/s, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 25 tbn, 25 tbc (default)
    Metadata:
      creation_time   : 2019-11-16T09:57:04.000000Z
      handler_name    : Core Media Data Handler
      encoder         : Apple ProRes 422
      timecode        : 00:00:00:00
    Stream #0:2(und): Data: none (tmcd / 0x64636D74), 0 kb/s (default)
    Metadata:
      creation_time   : 2019-11-16T09:57:04.000000Z
      handler_name    : Core Media Data Handler
      timecode        : 00:00:00:00
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'video_2.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 1
    compatible_brands: mp41mp42isom
    creation_time   : 2019-11-16T09:55:40.000000Z
  Duration: 00:00:30.71, start: 0.000000, bitrate: 15320 kb/s
    Stream #1:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 165 kb/s (default)
    Metadata:
      creation_time   : 2019-11-16T09:55:40.000000Z
      handler_name    : Core Media Audio
    Stream #1:1(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1920x1080, 15277 kb/s, 29.98 fps, 29.97 tbr, 90k tbn, 60 tbc (default)
    Metadata:
      creation_time   : 2019-11-16T09:55:40.000000Z
      handler_name    : Core Media Video
Input #2, mov,mp4,m4a,3gp,3g2,mj2, from 'video_3.mov':
  Metadata:
    major_brand     : qt  
    minor_version   : 0
    compatible_brands: qt  
    creation_time   : 2019-11-16T09:57:19.000000Z
    keywords        : iMovie
    description     : 
    title           : 
    com.apple.quicktime.description: 
    com.apple.quicktime.displayname: 
    com.apple.quicktime.keywords: iMovie
    com.apple.quicktime.title:
  Duration: 00:00:25.00, start: 0.000000, bitrate: 110329 kb/s
    Stream #2:0(eng): Audio: pcm_s16be (lpcm / 0x6D63706C), 48000 Hz, stereo, s16, 1536 kb/s (default)
    Metadata:
      creation_time   : 2019-11-16T09:57:19.000000Z
      handler_name    : Core Media Data Handler
    Stream #2:1(und): Video: prores (apcn / 0x6E637061), yuv422p10le(bt709, progressive), 1920x1080, 108790 kb/s, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 25 tbn, 25 tbc (default)
    Metadata:
      creation_time   : 2019-11-16T09:57:19.000000Z
      handler_name    : Core Media Data Handler
      encoder         : Apple ProRes 422
      timecode        : 00:00:00:00
    Stream #2:2(und): Data: none (tmcd / 0x64636D74), 0 kb/s (default)
    Metadata:
      creation_time   : 2019-11-16T09:57:19.000000Z
      handler_name    : Core Media Data Handler
      timecode        : 00:00:00:00

Solution

  • All inputs to be concatenated must have the same attributes and formats. video_2.mp4 is the oddball being H.264 + AAC in MP4, while the others are ProRes + PCM in MOV.

    One option is to re-encode video_2.mp4 to match the other inputs:

    ffmpeg -i video_2.mp4 -map 0 -c:a pcm_s16be -c:v prores -r 25 -video_track_timescale 25 video_2.mov
    

    Make mylist.txt:

    file 'video_1.mov'
    file 'video_2.mov'
    file 'video_3.mov'
    

    Concatenate:

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