FFmpeg concatenate - Error while opening encoder

0

I have an Android application that uses ffmpeg to concatenates 2 mp4 video files (360x640) together and outputs it as one .mp4. This is my command:

ffmpeg -y -i vid_1.mp4 -i vid_2.mp4 -strict experimental -s 360x640 -ar 44100 -pix_fmt yuv420p -codec:v libx264 -codec:a aac -filter_complex [0:0] setsar=1/1[sarfix];[sarfix] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1 vid_output.mp4

The command works, BUT some of my users are getting the following error (which i can not seem to reproduce myself)

The full log:

ffmpeg version N-69972-g6c91afe Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.4.3 (GCC)
ffmpeg4android 2.5_23 
  libavutil      54. 19.100 / 54. 19.100
  libavcodec     56. 25.100 / 56. 25.100
  libavformat    56. 23.100 / 56. 23.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.100 /  5. 11.100
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Splitting the commandline.
Reading option '-y' ... matched as option 'y' (overwrite output files) with argument '1'.
Reading option '-i' ... matched as input file with argument 'vid_1.mp4'.
Reading option '-i' ... matched as input file with argument 'vid_2.mp4'.
Reading option '-strict' ...Routing option strict to both codec and muxer layer
 matched as AVOption 'strict' with argument 'experimental'.
Reading option '-s' ... matched as option 's' (set frame size (WxH or abbreviation)) with argument '360x640'.
Reading option '-ar' ... matched as option 'ar' (set audio sampling rate (in Hz)) with argument '44100'.
Reading option '-pix_fmt' ... matched as option 'pix_fmt' (set pixel format) with argument 'yuv420p'.
Reading option '-codec:v' ... matched as option 'codec' (codec name) with argument 'libx264'.
Reading option '-codec:a' ... matched as option 'codec' (codec name) with argument 'aac'.
Reading option '-filter_complex' ... matched as option 'filter_complex' (create a complex filtergraph) with argument '[0:0] setsar=1/1[sarfix];[sarfix] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1'.
Reading option 'vid_output.mp4' ... matched as output file.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option y (overwrite output files) with argument 1.
Applying option filter_complex (create a complex filtergraph) with argument [0:0] setsar=1/1[sarfix];[sarfix] [0:1] [1:0] [1:1] concat=n=2:v=1:a=1.
Successfully parsed a group of options.
Parsing a group of options: input file vid_1.mp4.
Successfully parsed a group of options.
Opening an input file: vid_1.mp4.
Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100
ISO: File Type Major Brand: isom
Before avformat_find_stream_info() pos: 3143489 bytes read:39076 seeks:1
All info found
After avformat_find_stream_info() pos: 64041 bytes read:104612 seeks:2 frames:2
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'vid_1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.23.100
  Duration: 00:00:05.90, start: 0.023220, bitrate: 4262 kb/s
    Stream #0:0(eng), 1, 1/15360: Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 360x640 [SAR 1:1 DAR 9:16], 4125 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng), 1, 1/44100: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Successfully opened the file.
Parsing a group of options: input file vid_2.mp4.
Successfully parsed a group of options.
Opening an input file: vid_2.mp4.
Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100
ISO: File Type Major Brand: isom
Before avformat_find_stream_info() pos: 939642 bytes read:41527 seeks:1
All info found
After avformat_find_stream_info() pos: 32674 bytes read:74295 seeks:2 frames:2
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'vid_2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.23.100
  Duration: 00:00:08.90, start: 0.000000, bitrate: 844 kb/s
    Stream #1:0(und), 1, 1/15360: Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 360x640, 708 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #1:1(und), 1, 1/44100: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Successfully opened the file.
Parsing a group of options: output file vid_output.mp4.
Applying option s (set frame size (WxH or abbreviation)) with argument 360x640.
Applying option ar (set audio sampling rate (in Hz)) with argument 44100.
Applying option pix_fmt (set pixel format) with argument yuv420p.
Applying option codec:v (codec name) with argument libx264.
Applying option codec:a (codec name) with argument aac.
Successfully parsed a group of options.
Opening an output file: vid_output.mp4.
detected 4 logical cores
Setting 'sar' to value '1/1'
Setting 'n' to value '2'
Setting 'v' to value '1'
Setting 'a' to value '1'
Setting 'video_size' to value '360x640'
Setting 'pix_fmt' to value '0'
Setting 'time_base' to value '1/15360'
Setting 'pixel_aspect' to value '1/1'
Setting 'sws_param' to value 'flags=2'
Setting 'frame_rate' to value '30/1'
w:360 h:640 pixfmt:yuv420p tb:1/15360 fr:30/1 sar:1/1 sws_param:flags=2
Setting 'time_base' to value '1/44100'
Setting 'sample_rate' to value '44100'
Setting 'sample_fmt' to value 'fltp'
Setting 'channel_layout' to value '0x4'
tb:1/44100 samplefmt:fltp samplerate:44100 chlayout:0x4
Setting 'video_size' to value '360x640'
Setting 'pix_fmt' to value '0'
Setting 'time_base' to value '1/15360'
Setting 'pixel_aspect' to value '0/1'
Setting 'sws_param' to value 'flags=2'
Setting 'frame_rate' to value '30/1'
w:360 h:640 pixfmt:yuv420p tb:1/15360 fr:30/1 sar:0/1 sws_param:flags=2
Setting 'time_base' to value '1/44100'
Setting 'sample_rate' to value '44100'
Setting 'sample_fmt' to value 'fltp'
Setting 'channel_layout' to value '0x3'
tb:1/44100 samplefmt:fltp samplerate:44100 chlayout:0x3
Setting 'w' to value '360'
Setting 'h' to value '640'
Setting 'flags' to value '0x4'
w:360 h:640 flags:'0x4' interl:0
compat: called with args=[yuv420p]
Setting 'pix_fmts' to value 'yuv420p'
Setting 'sample_fmts' to value 'fltp'
Setting 'sample_rates' to value '44100'
Successfully opened the file.
auto-inserting filter 'auto-inserted resampler 0' between the filter 'graph 0 input from stream 1:1' and the filter 'Parsed_concat_1'
query_formats: 11 queried, 16 merged, 3 already done, 0 delayed
w:360 h:640 sar:1/1 dar:9/16 -> sar:1/1 dar:9/16
0.707107 0.707107 
ch:2 chl:stereo fmt:fltp r:44100Hz -> ch:1 chl:mono fmt:fltp r:44100Hz
w:360 h:640 fmt:yuv420p sar:1/1 -> w:360 h:640 fmt:yuv420p sar:1/1 flags:0x4
using mv_range_thread = 56
using SAR=1/1
using cpu capabilities: ARMv6 NEON
Output #0, mp4, to 'vid_output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.23.100
    Stream #0:0, 0, 0/0: Video: h264, none, q=2-31, 128 kb/s, SAR 1:1 DAR 0:0, 30 fps (default)
    Metadata:
      encoder         : Lavc56.25.100 libx264
    Stream #0:1, 0, 0/0: Audio: aac, 0 channels, 128 kb/s (default)
    Metadata:
      encoder         : Lavc56.25.100 aac
Stream mapping:
  Stream #0:0 (h264) -> setsar
  Stream #0:1 (aac) -> concat:in0:a0
  Stream #1:0 (h264) -> concat:in1:v0
  Stream #1:1 (aac) -> concat:in1:a0
  concat:out:v0 -> Stream #0:0 (libx264)
  concat:out:a0 -> Stream #0:1 (aac)
Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
exit_program: 1 
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Any idea why some people are having this error, can someone explain me what's happening? Thank you!

SebSob

Posted 2016-04-12T16:14:48.693

Reputation: 121

Show the properties of the input. – Gyan – 2016-04-12T17:02:37.403

@Mulvya I have edited my post with the full log, so you can have a look, thank you. – SebSob – 2016-04-12T20:41:03.353

Apply the setsar to the 2nd input. The first one already has a valid SAR. – Gyan – 2016-04-12T20:48:48.310

Can you please show me how to do that, because I am not familiar with the -filter_complex options, would much appreciate. Thanks – SebSob – 2016-04-12T21:29:30.703

[1:v]setsar=1[sarfix];[0:0][0:1][sarfix][1:1] <-- start the filters this way. – Gyan – 2016-04-13T05:37:21.170

Thanks, but when i use a video of size 176x144, i get the following error: Input link in1:v0 parameters (size 176x144, SAR 1:1) do not match the corresponding output link in0:v0 parameters (176x144, SAR 12:11) -> see full log , btw: before merging I am 100% sure they both input videos have same size, should I use satsar anyway?

– SebSob – 2016-04-13T09:12:32.690

My suggestion is for the inputs as shown in the logfile. In general, the concat videos should have the same properties, including resolution and SAR. So, you'll have to probe inputs beforehand and modify your command template accordingly. – Gyan – 2016-04-13T11:21:13.893

Ok thank you @Mulvya, This is my scenario: I let the user trim his original video, that gives me: trim_vid.mp4. Then I take a frame from that video and make that into a video: vid_from_frame.mp4 , lastely I concat both videos to eachother. So I concat 2 videos that I both 'created' with ffmpeg, maybe i can set the properties there? These are my commands, do you have any suggestions? Thank you so much!

– SebSob – 2016-04-13T13:10:35.460

Answers

1

Addressing your core requirement, this single command should do it

ffmpeg -ss 2 -t 8 -i original_vid.mp4 \
       -filter_complex "[0:v]split[v]f]; \
                        [f]loop=150:1:30,trim=start_frame=30:end_frame=180[fr]; \
                        [v][fr]concat=n=2,scale=640x360,setsar=1[vid]; \
                        [0:a]apad[aud]" \
-map "[vid]" -map "[aud]" \
-r 30 -c:a aac -ar 44100 -strict experimental -t 13 -y vid_output.mp4

The ss and t provide the trimmed segment you need.

The loop filter loops frame #30 i.e. the frame at 1 second in a 30 fps video for 150 frames i.e. 5 seconds at 30 fps.

A trim is then applied to extract the looped portion, start frame is frame index at 1 second and end frame is five seconds later, so +150 for a 30 fps video. This is then concatenated with the original trimmed segment, and finally scaled.

The audio doesn't need to be padded here, just did it to keep stream lengths the same.

Finally, since there's an apad filter which prolongs the audio indefinitely, the -t 13 stops the process when the 8 second trim + 5 second image loop have been encoded.

Gyan

Posted 2016-04-12T16:14:48.693

Reputation: 21 016

Thank you! Unfortunately the flow of my application does not allow to do it all in a single command (e.g. the extracted frame is being manipulated by the user by: placing 'stickers' (.png images) on the image, and the user also choose the music that will be added on the video created from that frame). Anyway thank you for the explanation, makes me understand ffmpeg better! – SebSob – 2016-04-13T19:47:51.020

I will try your suggestion "So, you'll have to probe inputs beforehand and modify your command template accordingly" by retrieving the SAR and then in the concat filter set it as setsar for both inputs. The trimmed video (176x144) has SAR 12:11 when i probe, but i think creating a video from the frame makes it a video with SAR 1:1 - and causes the concat to fail - not 100% sure if that makes any sense – SebSob – 2016-04-13T19:56:26.317