1

I have a script that automates encode and concat a series of input video files. I'm trying to get encode and concat in a single command.

At the moment it does not work causing immense empty pauses in the final video output.

I would like to get the result with concat demux instead of concat protocol because it is much harder to automate in a script

Do you know if it is possible to archive this result with concat demux? Thanks a lot!

Massimo

ENCODE / CONCAT 1080p

for f in ./*.mp4; do echo "file '$f'" >> list.txt; done

ffmpeg -f concat -safe 0 -y -i list.txt -i ../logo/logo.png -c:a aac -b:a 384k -ar 48000 -ac 2 -af aresample=async=1000 -c:v libx264 -x264opts keyint=50:min-keyint=50:no-scenecut -bf 0 -r 25 -b:v 4800k -maxrate 4800k -bufsize 3000k -profile:v main -crf 22 -filter_complex "[0:v][1:v]overlay=main_w-overlay_w-10:10,scale=1920:1080,setsar=1" ../buffer/1080set.mp4

It seems to add long pause at low frame-rate:

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fb03f05e600] Auto-inserting h264_mp4toannexb bitstream filter

frame= 9745 fps= 13 q=27.0 size= 186368kB time=00:18:52.56 bitrate=1348.0kbits

frame= 9745 fps= 13 q=27.0 size= 186368kB time=00:18:56.57 bitrate=1343.3kbits/

frame= 9745 fps= 13 q=27.0 size= 186368kB time=00:19:00.65 bitrate=1338.5kbits/

frame= 9745 fps= 13 q=27.0 size= 186368kB time=00:19:02.29 bitrate=1336.5kbits/

More than 1000 frames duplicated

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 2016-02-27T22:25:49.000000Z
    encoder         : HandBrake 0.10.5 2016021100
  Duration: 00:06:29.87, start: 0.000000, bitrate: 3447 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 768x432, 3282 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      creation_time   : 2016-02-27T22:25:49.000000Z
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 159 kb/s (default)
    Metadata:
      creation_time   : 2016-02-27T22:25:49.000000Z
      handler_name    : Stereo

Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '2.mp4':
  Metadata:
    major_brand     : M4V 
    minor_version   : 1
    compatible_brands: M4V mp42isom
    creation_time   : 2016-02-27T18:52:37.000000Z
  Duration: 00:07:16.93, start: 0.000000, bitrate: 1184 kb/s
    Stream #1:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m), 768x432, 991 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc (default)
    Metadata:
      creation_time   : 2016-02-27T18:52:37.000000Z
      handler_name    : Mainconcept MP4 Video Media Handler
      encoder         : AVC Coding
    Stream #1:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 189 kb/s (default)
    Metadata:
      creation_time   : 2016-02-27T18:52:37.000000Z
      handler_name    : Mainconcept MP4 Sound Media Handler

Input #2, mov,mp4,m4a,3gp,3g2,mj2, from '3.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp42mp41
    creation_time   : 2017-06-27T17:58:17.000000Z
  Duration: 00:05:05.05, start: 0.000000, bitrate: 2622 kb/s
    Stream #2:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m), 768x432, 2301 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc (default)
    Metadata:
      creation_time   : 2017-06-27T17:58:17.000000Z
      handler_name    : Alias Data Handler
      encoder         : AVC Coding
    Stream #2:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 317 kb/s (default)
    Metadata:
      creation_time   : 2017-06-27T17:58:17.000000Z
      handler_name    : Alias Data Handler

Input #3, mov,mp4,m4a,3gp,3g2,mj2, from '4.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp42mp41
    creation_time   : 2017-06-27T17:53:32.000000Z
  Duration: 00:06:47.36, start: 0.000000, bitrate: 3144 kb/s
    Stream #3:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m), 768x432, 2819 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc (default)
    Metadata:
      creation_time   : 2017-06-27T17:53:32.000000Z
      handler_name    : Alias Data Handler
      encoder         : AVC Coding
    Stream #3:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 317 kb/s (default)
    Metadata:
      creation_time   : 2017-06-27T17:53:32.000000Z
      handler_name    : Alias Data Handler
Community
  • 1
  • 1
  • First manually make sure your `ffmpeg` commands work, unscripted in a terminal. Then once you get it to work you can implement it into the script. Right now I don't know if the problem is somehow due to the scripting or due to your ffmpeg command or both. You can make it easier for us to help but providing any inputs that are required to duplicate the issue, show the actual ffmpeg commands that you are executing, and the complete console output from these commands. – llogan Mar 14 '18 at 21:18
  • 1
    Secondly, the console output from ffmpeg is not meant to be parsed in a script but is only for informative purposes. That's what ffprobe is for. The ffmpeg output is not standardized, so the output may be unpredictable to be parsed properly. Also, the frame rate it outputs is a shorthand value so it is not accurate. [Use ffprobe instead](http://trac.ffmpeg.org/wiki/FFprobeTips#FrameRate). Run your script though shellcheck.net for some bash suggestions. – llogan Mar 14 '18 at 21:24
  • Hi @LordNeckbeard I will modify it, currently the script extracts only frame-rate and calculates the closed frame-rate – Massimo Vantaggio Mar 14 '18 at 21:43
  • Thanks for suggestions , actually it's sure not perfect, I'll update it with ffprobe but I'm sure the result is the same – Massimo Vantaggio Mar 14 '18 at 21:48
  • Can you show info on each individual input which is to be concatenated? `ffmpeg -i input1 -i input2 -i input3` – llogan Mar 14 '18 at 22:38
  • @LordNeckbeard thanks for the help this thing dont make me sleep at night: currently i performs the encoding and then the concat with another encoding to avoid PTS discrepancy,the process is too stupid to be acceptable, if you need i upload the videos. – Massimo Vantaggio Mar 14 '18 at 23:38
  • I'm not sure if this applies to your case but take a look of [link](https://stackoverflow.com/questions/32716643/strange-pause-in-video-after-concatenation-of-two-mp4-videos). – tshiono Mar 15 '18 at 00:17

1 Answers1

1

concat demxuer

The concat demuxer needs all inputs to have the same formats, number of streams, type of streams, order of streams (or use exact_stream_id), timebase, frame rate, audio sample rate, audio channel layout, and it is also recommended to have the same width and height.

In your case the timebase (tbn) of the first input is different than the rest. You can pre-process this input to conform it to the others:

ffmpeg -i 1.mp4 -c copy -video_track_timescale 25k 1_reset.mp4

Then run your command using the concat demuxer.

concat filter

Because you're re-encoding anyway it may be easier for you to use the concat filter. This allows you to conform each input using various filters then concate all in one command. There are several examples of such use on this site.

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Thank you so much! Great lesson, I'm thinking on a script that find the file with wrong tbn and corrects it before encoding, with -c copy seems like an instant correction, I'm not sure I want to leave early concat demux now, I have to do some tests – Massimo Vantaggio Mar 15 '18 at 12:28
  • or maybe i can correct all track to a default tbn before the command for concat/demux – Massimo Vantaggio Mar 15 '18 at 12:32
  • 1
    @MassimoVantaggio Using `-c copy` to stream copy will be relatively quick as it avoids re-encoding. You can check timebase with `ffprobe` using `ffprobe -loglevel error -show_entries stream=time_base -select_streams v:0 -of csv=p=0 input.mp4` Note that `-video_track_timescale` is only for MP4/M4V/M4A/MOV. – llogan Mar 15 '18 at 17:37
  • Whats about tbc, how to conform it? – Massimo Vantaggio Apr 12 '18 at 12:21
  • @llogan Lenght of the video is also important? My videos trying to concat have same codec, frame rate, width, height, time base, .. but same error... only differrnce is the leght of the video – Ionut Bejinariu Feb 01 '23 at 15:23