1

I am trying to merge an audio mp3 file with a video mp4 file. I want the audio file's volume to be turned down a little bit so that it is background music for the video clip.

This is how my code looks now (I tried adapting from a previous code merging 2 mp3's) --

ffmpeg \
-i output.mp4 \ 
-i music.mp3 \ 
-filter_complex "[0:a]volume=0.15[A];[1:a][A]amerge[out]" \
-map [out] -y output-final.mp4 2>&1

EDIT

There seems to be an issue on iphone when encoding mp4 videos using filter_complex with amerge.

Console output -

ffmpeg version N-90767-g768c077-syslint Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-16)
  configuration: --prefix=/usr/local/cpffmpeg --enable-shared --enable-nonfree --enable-gpl --enable-pthreads --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --extra-cflags=-I/usr/local/cpffmpeg/include --extra-ldflags=-L/usr/local/cpffmpeg/lib --enable-version3 --extra-version=syslint
  libavutil      56. 15.100 / 56. 15.100
  libavcodec     58. 19.100 / 58. 19.100
  libavformat    58. 13.100 / 58. 13.100
  libavdevice    58.  4.100 / 58.  4.100
  libavfilter     7. 18.100 /  7. 18.100
  libswscale      5.  2.100 /  5.  2.100
  libswresample   3.  2.100 /  3.  2.100
  libpostproc    55.  2.100 / 55.  2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/path/to/output-4786-second.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.13.100
  Duration: 00:00:13.58, start: 0.000000, bitrate: 798 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 767 kb/s, 24 fps, 24 tbr, 12288 tbn, 48 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 22050 Hz, mono, fltp, 29 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Input #1, mp3, from '/path/to/buddy.mp3':
  Duration: 00:02:02.12, start: 0.025057, bitrate: 112 kb/s
    Stream #1:0: Audio: mp3, 44100 Hz, stereo, fltp, 112 kb/s
    Metadata:
      encoder         : LAME3.99r
Stream mapping:
  Stream #0:1 (aac) -> amerge:in0 (graph 0)
  Stream #1:0 (mp3float) -> volume (graph 0)
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  amerge (graph 0) -> Stream #0:1 (aac)
Press [q] to stop, [?] for help
[libx264 @ 0x1cae600] using SAR=1/1
[libx264 @ 0x1cae600] using cpu capabilities: none!
[libx264 @ 0x1cae600] profile Constrained Baseline, level 3.1
[libx264 @ 0x1cae600] 264 - core 155 r2901 7d0ff22 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=22 lookahead_threads=3 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=24 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
[Parsed_amerge_2 @ 0x2ed3780] No channel layout for input 1
Output #0, mp4, to '/path/to/output-4786.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.13.100
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 24 fps, 12288 tbn, 24 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc58.19.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), 22050 Hz, 3.0, fltp, 197 kb/s (default)
    Metadata:
      encoder         : Lavc58.19.100 aac
frame=   82 fps=0.0 q=12.0 size=       0kB time=00:00:02.50 bitrate=   0.2kbits/s speed=4.97x    
frame=  157 fps=156 q=18.0 size=     512kB time=00:00:05.57 bitrate= 752.7kbits/s speed=5.53x    
frame=  210 fps=139 q=13.0 size=    1280kB time=00:00:07.80 bitrate=1344.0kbits/s speed=5.18x    
frame=  273 fps=136 q=12.0 size=    1280kB time=00:00:10.40 bitrate=1008.0kbits/s speed=5.18x    
frame=  326 fps=145 q=-1.0 Lsize=    1607kB time=00:00:13.54 bitrate= 972.3kbits/s speed=6.04x    
video:1290kB audio:310kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.406112%
[libx264 @ 0x1cae600] frame I:2     Avg QP:15.50  size: 21673
[libx264 @ 0x1cae600] frame P:324   Avg QP:13.50  size:  3942
[libx264 @ 0x1cae600] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 0x1cae600] mb P  I16..4:  1.6%  0.0%  0.0%  P16..4: 12.7%  0.0%  0.0%  0.0%  0.0%    skip:85.7%
[libx264 @ 0x1cae600] coded y,uvDC,uvAC intra: 25.5% 29.6% 12.4% inter: 4.2% 8.3% 1.4%
[libx264 @ 0x1cae600] i16 v,h,dc,p: 62% 18%  9% 11%
[libx264 @ 0x1cae600] i8c dc,h,v,p: 68% 16% 11%  5%
[libx264 @ 0x1cae600] kb/s:777.82
[aac @ 0x1caf980] Qavg: 4402.419

Most recent command attempted --

ffmpeg \
-i '.$thepath.'/output-'.$v_Id.'-second.mp4 \
-i '.$musicpath .'/'.$music.'.mp3 \
-filter_complex "[1:a]volume=0.15,apad[A];[0:a][A]amerge[out]" \
-c:v libx264 -c:a aac -map 0:v -map "[out]" \
-preset ultrafast -threads 0 -pix_fmt yuv420p -y '.$thepath.'/output-'.$v_Id.'.mp4 2>&1

2 Answers 2

3

Use

ffmpeg \
-i output.mp4 \
-i music.mp3 \
-filter_complex "[1:a]volume=0.15,apad[A];[0:a][A]amerge[out]" \
-c:v copy -map 0:v -map [out] -y output-final.mp4
11
  • im getting this error -- sh: line 3: -filter_complex: command not found
    – 730wavy
    Commented Aug 11, 2018 at 15:22
  • Removed trailing whitespace
    – Gyan
    Commented Aug 11, 2018 at 15:40
  • that wasnt the issue
    – 730wavy
    Commented Aug 11, 2018 at 16:10
  • Just put it all on one line and execute
    – Gyan
    Commented Aug 11, 2018 at 16:21
  • 4
    Add -profile:v baseline -ac 2 to your current command.
    – Gyan
    Commented Aug 21, 2018 at 14:15
0

please try this way

fun mix(): Array<String?> {
        val cmdList = ArrayList<String>()
        var cmds: Array<String?>? = null
        try {
            cmdList.add("-y")
            cmdList.add("-ss")
            cmdList.add(startVideoDuration.toString())
            cmdList.add("-t")
            cmdList.add(endVideoDuration.toString())
            cmdList.add("-i")
            cmdList.add(videoPath)
            cmdList.add("-ss")
             cmdList.add(startAudioDuration.toString())
            cmdList.add("-t")
            cmdList.add(endVideoDuration.toString())

            cmdList.add("-stream_loop")
            cmdList.add("-1")

            cmdList.add("-i")
            cmdList.add(audioPath)

            if (videovolume == -1f && audiovolume > -1) {
                cmdList.add("-c:v")
                if (startVideoDuration > 0) {
                    cmdList.add("mpeg4")
                    cmdList.add("-b:v")
                    cmdList.add("2304000")
                } else {
                    cmdList.add("copy")
                }
                cmdList.add("-c:a")
                cmdList.add("aac")
                cmdList.add("-map")
                cmdList.add("1:a:0")
                cmdList.add("-map")
                cmdList.add("0:v:0")
                cmdList.add("-strict")
                cmdList.add("-2")
                if (audiovolume < 0.99 || audiovolume > 1.01) {
                    cmdList.add("-vol")
                    cmdList.add((audiovolume * 100).toInt().toString())
                }
            }else{
                cmdList.add("-c:v")
                if (startVideoDuration > 0) {
                    cmdList.add("mpeg4")
                    cmdList.add("-b:v")
                    cmdList.add("2304000")
                } else {
                    cmdList.add("copy")
                }
                cmdList.add("-map")
                cmdList.add("0:v:0")
                cmdList.add("-strict")
                cmdList.add("-2")
                cmdList.add("-filter_complex")
                cmdList.add(
                    String.format(
                        "[0:a]aformat=sample_fmts=fltp:sample_rates=48000:channel_layouts=stereo,volume=%f[a0];" +
                                "[1:a]aformat=sample_fmts=fltp:sample_rates=48000:channel_layouts=stereo,volume=%f[a1];" +
                                "[a0][a1]amix=inputs=2:duration=first[aout]",
                        videovolume,
                        audiovolume
                    )
                )
                cmdList.add("-map")
                cmdList.add("[aout]")
            }

            cmdList.add("-b:a")
            cmdList.add("128000")
            cmdList.add("-ac")
            cmdList.add("2")
            cmdList.add("-ar")
            cmdList.add("44100")
            cmdList.add("-movflags")
            cmdList.add("faststart")
            cmdList.add("-preset")
            cmdList.add("ultrafast")
            cmdList.add(outputpath)

            cmds = arrayOfNulls<String>(cmdList.size)
            cmdList.toArray(cmds)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        return cmds!!
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .