Sunday 22 September 2019

ffmpeg - merging video and audio with custom panning


I have:



  1. a video which has mono audio inside

  2. a audio (mono)


I'd like to merge those two to a single video file containing:



  • video from #1

  • audio from #1 full left pan + audio #2 full right pan


Is this possible in ffmpeg using 1 command?


I've tried the following which almost does this but the video/audio gets out of sync:


$ ffmpeg -i video.mp4 -filter_complex "amovie=audio.wav [r] ; [r] amerge" output.mp4 -y
ffmpeg version N-61286-gdbc3e11 Copyright (c) 2000-2014 the FFmpeg developers
built on Mar 11 2014 22:01:37 with gcc 4.8.2 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 52. 66.101 / 52. 66.101
libavcodec 55. 52.102 / 55. 52.102
libavformat 55. 34.100 / 55. 34.100
libavdevice 55. 11.100 / 55. 11.100
libavfilter 4. 3.100 / 4. 3.100
libswscale 2. 5.101 / 2. 5.101
libswresample 0. 18.100 / 0. 18.100
libpostproc 52. 3.100 / 52. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.34.100
Duration: 00:00:08.05, start: 0.050000, bitrate: 360 kb/s
Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 226 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 32000 Hz, mono, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
[Parsed_amovie_0 @ 02be7b40] Channel layout is not set in output stream 0, guessed channel layout is 'mono'
[Parsed_amerge_1 @ 02be9f40] No channel layout for input 2
[Parsed_amerge_1 @ 02be9f40] Input channel layouts overlap: output layout will be determined by the number of distinct input channels
[libx264 @ 033052e0] using SAR=1/1
[libx264 @ 033052e0] using cpu capabilities: MMX2 SSE2Slow SlowCTZ
[libx264 @ 033052e0] profile High, level 3.0
[libx264 @ 033052e0] 264 - core 142 r2389 956c8d8 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 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
Output #0, mp4, to 'output.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.34.100
Stream #0:0: Audio: aac (libvo_aacenc) ([64][0][0][0] / 0x0040), 11025 Hz, stereo, s16, 128 kb/s (default)
Stream #0:1(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 640x360 [SAR 1:1 DAR 16:9], q=-1--1, 12800 tbn, 25 tbc (default)
Metadata:
handler_name : VideoHandler
Stream mapping:
Stream #0:1 (aac) -> amerge:in1 (graph 0)
amerge (graph 0) -> Stream #0:0 (libvo_aacenc)
Stream #0:0 -> #0:1 (h264 -> libx264)
Press [q] to stop, [?] for help
Error while decoding stream #0:1: Error number -12 occurred
Last message repeated 26 times
frame= 201 fps= 49 q=-1.0 Lsize= 333kB time=00:00:07.96 bitrate= 342.7kbits/s dup=1 drop=0
video:213kB audio:115kB subtitle:0 data:0 global headers:0kB muxing overhead 1.561755%
[libvo_aacenc @ 0315dc40] 1 frames left in the queue on closing
[libx264 @ 033052e0] frame I:1 Avg QP:16.14 size: 12758
[libx264 @ 033052e0] frame P:51 Avg QP:17.19 size: 2648
[libx264 @ 033052e0] frame B:149 Avg QP:19.82 size: 468
[libx264 @ 033052e0] consecutive B-frames: 1.0% 0.0% 1.5% 97.5%
[libx264 @ 033052e0] mb I I16..4: 52.6% 33.8% 13.6%
[libx264 @ 033052e0] mb P I16..4: 2.8% 5.9% 0.1% P16..4: 35.1% 9.8% 5.4% 0.0% 0.0% skip:40.8%
[libx264 @ 033052e0] mb B I16..4: 0.1% 0.1% 0.0% B16..8: 22.9% 1.1% 0.1% direct: 4.0% skip:71.8% L0:45.0% L1:52.8% BI: 2.1%
[libx264 @ 033052e0] 8x8 transform intra:60.1% inter:76.4%
[libx264 @ 033052e0] coded y,uvDC,uvAC intra: 10.4% 53.2% 15.0% inter: 3.2% 11.2% 0.3%
[libx264 @ 033052e0] i16 v,h,dc,p: 50% 25% 17% 7%
[libx264 @ 033052e0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 7% 11% 74% 1% 2% 2% 2% 1% 1%
[libx264 @ 033052e0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 26% 16% 23% 5% 6% 7% 5% 6% 5%
[libx264 @ 033052e0] i8c dc,h,v,p: 53% 23% 20% 3%
[libx264 @ 033052e0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 033052e0] ref P L0: 63.7% 9.0% 18.5% 8.8%
[libx264 @ 033052e0] ref B L0: 86.7% 11.2% 2.1%
[libx264 @ 033052e0] ref B L1: 96.3% 3.7%
[libx264 @ 033052e0] kb/s:216.50

I've managed to do it with multiple commands:


#1 create right panned audio
ffmpeg -i audio.wav -ac 2 -vbr 5 audio-stereo.mp3 -y
ffmpeg -i audio-stereo.mp3 -af pan=stereo:c1=c1 audio-right.mp3 -y

#2 create left panned video
ffmpeg -i video.mp4 -af pan=stereo:c0=c0 video-left.mp4 -y

#3 merge the two
ffmpeg -i video-left.mp4 -i audio-right.mp3 -c:v copy -filter_complex "amix=inputs=2" video-mixed.mp4 -y

It does the job, but is it possible with 1 command?



Answer



Works for me with:


ffmpeg -i video.mp4 -i audio.wav -filter_complex "[0:a][1:a]amerge[a]" \
-map 0:v -map "[a]" -c:v copy output.mp4


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...