2

I am trying to combine an audio (.wav) file and picture file (.png) into a video file. The video's length should be the same length as the input audio. Also, the output video should be youtube-friendly (be able to upload to YouTube).

I've tried a couple commands, but the duration of the video file produced is always way too long.

The audio file has a length 00:00:10.71.

Command 1

ffmpeg -loop 1 -framerate 1 -i 000.png -i 000.wav -c:a aac -c:v libx264 -preset slow -crf 0  -shortest out.mp4

output:

$ ffmpeg -loop 1 -framerate 1 -i 000.png -i 000.wav -c:a aac -c:v libx264 -preset slow -crf 0  -shortest out.mp4
ffmpeg version n4.1.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181127
  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-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --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. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
[png_pipe @ 0x561e7bc1e600] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, png_pipe, from '000.png':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: png, rgba(pc), 1920x1080 [SAR 3937:3937 DAR 16:9], 1 tbr, 1 tbn, 1 tbc
Guessed Channel Layout for Input Stream #1.0 : mono
Input #1, wav, from '000.wav':
  Duration: 00:00:10.71, bitrate: 256 kb/s
    Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (png (native) -> h264 (libx264))
  Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x561e7bc6dbc0] using SAR=1/1
[libx264 @ 0x561e7bc6dbc0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x561e7bc6dbc0] profile High 4:4:4 Predictive, level 5.0, 4:4:4, 8-bit
[libx264 @ 0x561e7bc6dbc0] 264 - core 157 r2945 72db437 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=5 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=8 psy=0 mixed_ref=1 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=0 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=2 keyint=250 keyint_min=1 scenecut=40 intra_refresh=0 rc=cqp mbtree=0 qp=0
Output #0, mp4, to 'out.mp4':
  Metadata:
    encoder         : Lavf58.20.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv444p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 1 fps, 16384 tbn, 1 tbc
    Metadata:
      encoder         : Lavc58.35.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 69 kb/s
    Metadata:
      encoder         : Lavc58.35.100 aac
frame=   25 fps=0.0 q=-1.0 Lsize=     168kB time=00:00:24.00 bitrate=  57.3kbits/s speed=28.9x    
video:63kB audio:103kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.409398%
[libx264 @ 0x561e7bc6dbc0] frame I:1     Avg QP: 0.00  size: 61676
[libx264 @ 0x561e7bc6dbc0] frame P:24    Avg QP: 0.00  size:    76
[libx264 @ 0x561e7bc6dbc0] mb I  I16..4: 88.8%  0.0% 11.2%
[libx264 @ 0x561e7bc6dbc0] mb P  I16..4:  0.0%  0.0%  0.0%  P16..4:  0.0%  0.0%  0.0%  0.0%  0.0%    skip:100.0%
[libx264 @ 0x561e7bc6dbc0] coded y,u,v intra: 8.5% 7.6% 7.5% inter: 0.0% 0.0% 0.0%
[libx264 @ 0x561e7bc6dbc0] i16 v,h,dc,p: 97%  1%  2%  0%
[libx264 @ 0x561e7bc6dbc0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 55% 26% 11%  3%  2%  1%  1%  0%  1%
[libx264 @ 0x561e7bc6dbc0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x561e7bc6dbc0] kb/s:20.32
[aac @ 0x561e7bc6fe80] Qavg: 10368.243

The video ends up with a length of 00:00:25.00. full ffprobe output

Command 2

ffmpeg -loop 1 -i 000.png -i 000.wav -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest out.mp4

output:

$ ffmpeg -loop 1 -i 000.png -i 000.wav -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest out.mp4
ffmpeg version n4.1.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181127
  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-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --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. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
[png_pipe @ 0x559a83ee3640] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, png_pipe, from '000.png':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: png, rgba(pc), 1920x1080 [SAR 3937:3937 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
Guessed Channel Layout for Input Stream #1.0 : mono
Input #1, wav, from '000.wav':
  Duration: 00:00:10.71, bitrate: 256 kb/s
    Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (png (native) -> h264 (libx264))
  Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x559a83f32480] using SAR=1/1
[libx264 @ 0x559a83f32480] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x559a83f32480] profile High 4:4:4 Predictive, level 4.0, 4:4:4, 8-bit
[libx264 @ 0x559a83f32480] 264 - core 157 r2945 72db437 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=4 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
[aac @ 0x559a83f33a40] Too many bits 12288.000000 > 6144 per frame requested, clamping to max
Output #0, mp4, to 'out.mp4':
  Metadata:
    encoder         : Lavf58.20.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv444p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 12800 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.35.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 16000 Hz, mono, fltp, 96 kb/s
    Metadata:
      encoder         : Lavc58.35.100 aac
frame=  324 fps= 46 q=-1.0 Lsize=     230kB time=00:00:12.84 bitrate= 146.9kbits/s speed=1.83x    
video:118kB audio:103kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.943480%
[libx264 @ 0x559a83f32480] frame I:2     Avg QP: 9.45  size: 45012
[libx264 @ 0x559a83f32480] frame P:82    Avg QP:10.08  size:   120
[libx264 @ 0x559a83f32480] frame B:240   Avg QP:12.67  size:    85
[libx264 @ 0x559a83f32480] consecutive B-frames:  1.2%  0.0%  0.0% 98.8%
[libx264 @ 0x559a83f32480] mb I  I16..4: 87.4%  0.0% 12.6%
[libx264 @ 0x559a83f32480] mb P  I16..4:  0.0%  0.0%  0.0%  P16..4:  0.1%  0.0%  0.0%  0.0%  0.0%    skip:99.9%
[libx264 @ 0x559a83f32480] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  0.2%  0.0%  0.0%  direct: 0.0%  skip:99.8%  L0:61.0% L1:39.0% BI: 0.0%
[libx264 @ 0x559a83f32480] coded y,u,v intra: 8.4% 0.1% 0.1% inter: 0.0% 0.0% 0.0%
[libx264 @ 0x559a83f32480] i16 v,h,dc,p: 95%  3%  2%  0%
[libx264 @ 0x559a83f32480] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 43% 16% 18%  4%  4%  5%  4%  3%  3%
[libx264 @ 0x559a83f32480] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x559a83f32480] ref P L0: 95.9%  2.4%  1.4%  0.4%
[libx264 @ 0x559a83f32480] ref B L0: 28.9% 71.1%
[libx264 @ 0x559a83f32480] kb/s:74.29
[aac @ 0x559a83f33a40] Qavg: 60199.988

The video ends up with length 00:00:12.96, still more than the target 00:00:10.71. Also, processing takes longer. full ffprobe output

Command 3

$ ffmpeg -framerate 1 -i 000.png -i 000.wav -c:a aac -c:v libx264 -preset fast -crf 0 out.mp4

The duration is fine for this one: 00:00:10.77. This one is a lot faster than the others to process. The output video isn't youtube-friendly though, likely because it only has 1 frame. Full ffprobe output

Why is this happening and how do I get the video to match the length of the input audio?

2

The most reliable way to ensure a desired length for an output is to specify -t X on the output.

But if you need adaptive limiting of the output duration, use

ffmpeg -loop 1 -framerate 5 -i 000.png -i 000.wav -c:v libx264 -c:a aac -b:a 192k -shortest -fflags +shortest -max_interleave_delta 500M out.mp4

The fflag is applied deeper in the processing pipeline and should be used in conjunction with the -shortest switch. The max_interleave_delta with a high value is set to avoid writing packets already queued for output.

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.