[Android FFMPEG development] Use FFMPEG to mix MP3 files in Android

Article Directory





1. Pre-operation (porting FFMPEG)



Refer to [Android FFMPEG Development] Execute FFMPEG Instructions in Android Blog;

Import the following dependencies in the application's build.gradle build script;

dependencies {
	implementation 'com.writingminds:FFmpegAndroid:0.3.2'
}

Then follow the process in [Android FFMPEG Development] to execute FFMPEG instructions in Android 2. Execute FFMPEG instructions in Android to develop, and pass the spliced ​​FFMPEG instructions to the ffmpeg.execute method;





2. FFMPEG mixing command



FFMPEG mixing command:

ffmpeg -i 输入文件1 -i 输入文件2 -i 输入文件3 -filter_complex amix=inputs=输入文件个数:duration=混音时间对齐策略:dropout_transition=声音渐弱时间 输出文件

-i: the full absolute path of the input file;

amix=inputs=2: Indicates the number of mixing files, yes  twenty two 2 Files for mixing;

duration=longest: set the mixing time alignment strategy, longest represents the longest audio file duration, shortest represents the shortest input duration, first represents the duration of the first file;

dropout_transition=2: Indicates that when the input stream ends, the volume changes from full volume to  0 0 0 Volume fades  twenty two 2 Disappear in seconds;


Full command in Android:

-i /data/user/0/com.example.ffmpeg_mix/files/BeardedGrain.mp3 -i /data/user/0/com.example.ffmpeg_mix/files/RainyDay.mp3 -filter_complex amix=inputs=2:duration=longest:dropout_transition=2 /data/user/0/com.example.ffmpeg_mix/files/Mix.mp3

First  1 1 1 The input file is /data/user/0/com.example.ffmpeg_mix/files/BeardedGrain.mp3;

First  twenty two 2 The input file is /data/user/0/com.example.ffmpeg_mix/files/RainyDay.mp3;

amix=inputs=2 means there is currently  twenty two 2 Input files;

duration=longest means that the mixing time is the longest input duration;

dropout_transition=2 means that when the input stream ends, the volume changes from full volume to  0 0 0 Volume fades  twenty two 2 Disappear in seconds;

The output file of the final mix is ​​/data/user/0/com.example.ffmpeg_mix/files/Mix.mp3;


Command branch comment:

-i /data/user/0/com.example.ffmpeg_mix/files/BeardedGrain.mp3 // 输入文件 1
-i /data/user/0/com.example.ffmpeg_mix/files/RainyDay.mp3 // 输入文件 2
-filter_complex amix=inputs=2:duration=longest:dropout_transition=2 // 混音参数
/data/user/0/com.example.ffmpeg_mix/files/Mix.mp3 // 输出文件





3. A complete example of Android FFMPEG audio mixing source code



A complete example of Android FFMPEG audio mixing source code:

package com.example.ffmpeg_mix

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler
import com.github.hiteshsondhi88.libffmpeg.FFmpeg
import com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler
import java.io.File

class MainActivity : AppCompatActivity() {
    val TAG = "MainActivity"

    /**
     * 应用内置存储下的 files 目录
     */
    lateinit var mFilePath: String

    lateinit var ffmpeg: FFmpeg

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mFilePath = this.filesDir.toString()

        // 初始化 FFMPEG
        ffmpeg = FFmpeg.getInstance(this)

        // 加载 FFMPEG 可执行文件
        ffmpeg.loadBinary(LoadBinaryResponseHandler())
    }

    /**
     * 拷贝文件
     */
    fun copy(view: View) {
        CommandUtils.copyAssets2File(
            this,
            "BeardedGrain.mp3",
            "${mFilePath}/BeardedGrain.mp3")

        CommandUtils.copyAssets2File(
            this,
            "RainyDay.mp3",
            "${mFilePath}/RainyDay.mp3")

        showText()
    }

    /**
     * 执行混音命令
     */
    fun mix(view: View) {
        var cmd = "-i ${mFilePath}/BeardedGrain.mp3 -i ${mFilePath}/RainyDay.mp3 -filter_complex amix=inputs=2:duration=longest:dropout_transition=2 ${mFilePath}/Mix.mp3"

        Log.i(TAG, "执行命令 : $cmd")

        var cmdArraay = cmd.split(" ").toTypedArray();
        ffmpeg.execute(cmdArraay, object : ExecuteBinaryResponseHandler(){
            override fun onStart() {
                super.onStart()
                Log.i(TAG, "onStart")
            }

            override fun onFinish() {
                super.onFinish()
                Log.i(TAG, "onStart")
                showText()
            }

            override fun onSuccess(message: String?) {
                super.onSuccess(message)
                Log.i(TAG, "onSuccess : $message")
            }

            override fun onProgress(message: String?) {
                super.onProgress(message)
                Log.i(TAG, "onProgress : $message")
            }

            override fun onFailure(message: String?) {
                super.onFailure(message)
                Log.i(TAG, "onFailure : $message")
            }
        })
    }

    /**
     * 显示内置存储目录
     */
    fun showText(){
        var fileString = ""
        var files = File(mFilePath).listFiles()
        files.forEach {
            fileString += "${it}\n"
        }
        findViewById<TextView>(R.id.text).text = fileString
    }

}


Execution result: This is the output content of the correct mixing;

执行命令 : -i /data/user/0/com.example.ffmpeg_mix/files/BeardedGrain.mp3 -i /data/user/0/com.example.ffmpeg_mix/files/Rain
onStart
onProgress : ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
onProgress :   built with gcc 4.8 (GCC)
onProgress :   configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-andro
onProgress :   libavutil      55. 17.103 / 55. 17.103
onProgress :   libavcodec     57. 24.102 / 57. 24.102
onProgress :   libavformat    57. 25.100 / 57. 25.100
onProgress :   libavdevice    57.  0.101 / 57.  0.101
onProgress :   libavfilter     6. 31.100 /  6. 31.100
onProgress :   libswscale      4.  0.100 /  4.  0.100
onProgress :   libswresample   2.  0.101 /  2.  0.101
onProgress :   libpostproc    54.  0.100 / 54.  0.100
onProgress : [mp3 @ 0xe8230000] Skipping 0 bytes of junk at 51635.
onProgress : [mjpeg @ 0xe80af400] Changing bps to 8
onProgress : Input #0, mp3, from '/data/user/0/com.example.ffmpeg_mix/files/BeardedGrain.mp3':
onProgress :   Metadata:
onProgress :     encoder         : Lavf57.83.100
onProgress :     album           : 芒种
onProgress :     title           : 芒种
onProgress :     artist          : 步束
onProgress :     disc            : 01
onProgress :     track           : 1
onProgress :   Duration: 00:03:35.48, start: 0.025056, bitrate: 321 kb/s
onProgress :     Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
onProgress :     Metadata:
onProgress :       encoder         : Lavc57.10
onProgress :     Stream #0:1: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 640x640 [SAR 72:72 DAR 1:1], 90k tb
onProgress :     Metadata:
onProgress :       comment         : Media (e.g. label side of CD)
onProgress : [mp3 @ 0xe8230600] Skipping 0 bytes of junk at 18961.
onProgress : [mjpeg @ 0xe80b0400] Changing bps to 8
onProgress : Input #1, mp3, from '/data/user/0/com.example.ffmpeg_mix/files/RainyDay.mp3':
onProgress :   Metadata:
onProgress :     encoder         : Lavf57.83.100
onProgress :   Duration: 00:04:25.85, start: 0.025056, bitrate: 320 kb/s
onProgress :     Stream #1:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
onProgress :     Metadata:
onProgress :       encoder         : Lavc57.10
onProgress :     Stream #1:1: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 640x640 [SAR 72:72 DAR 1:1], 90k tb
onProgress :     Metadata:
onProgress :       comment         : Media (e.g. label side of CD)
onProgress : [swscaler @ 0xe7f21000] deprecated pixel format used, make sure you did set range correctly
onProgress : [swscaler @ 0xe7f21000] No accelerated colorspace conversion found from yuv420p to rgb24.
onProgress : [mp3 @ 0xe8230c00] Frame rate very high for a muxer not efficiently supporting it.
onProgress : Please consider specifying a lower framerate, a different muxer or -vsync 2
onProgress : Output #0, mp3, to '/data/user/0/com.example.ffmpeg_mix/files/Mix.mp3':
onProgress :   Metadata:
onProgress :     Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, stereo, fltp (default)
onProgress :     Metadata:
onProgress :       encoder         : Lavc57.24.102 libmp3lame
onProgress :     Stream #0:1: Video: png, rgb24, 640x640 [SAR 1:1 DAR 1:1], q=2-31, 200 kb/s, 90k fps, 90k tbn, 90k tb
onProgress :     Metadata:
onProgress :       comment         : Media (e.g. label side of CD)
onProgress :       encoder         : Lavc57.24.102 png
onProgress : Stream mapping:
onProgress :   Stream #0:0 (mp3) -> amix:input0 (graph 0)
onProgress :   Stream #1:0 (mp3) -> amix:input1 (graph 0)
onProgress :   amix (graph 0) -> Stream #0:0 (libmp3lame)
onProgress :   Stream #0:1 -> #0:1 (mjpeg (native) -> png (native))
onProgress : Press [q] to stop, [?] for help
onProgress : frame=    1 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate= 870.5kbits/s speed=0.00213x    
onProgress : frame=    1 fps=0.0 q=0.0 size=       0kB time=00:03:31.41 bitrate=   0.0kbits/s speed=3.25x    
onProgress : frame=    1 fps=0.0 q=0.0 size=       0kB time=00:03:33.00 bitrate=   0.0kbits/s speed=3.25x    
onProgress : frame=    1 fps=0.0 q=0.0 size=       0kB time=00:03:34.57 bitrate=   0.0kbits/s speed=3.25x    
onProgress : Error while filtering: Out of memory
onProgress : frame=    1 fps=0.0 q=-0.0 Lsize=    3995kB time=00:03:35.45 bitrate= 151.9kbits/s speed=3.25x    
onProgress : video:628kB audio:3367kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.008727%
onSuccess : ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
      built with gcc 4.8 (GCC)
      configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
      libavutil      55. 17.103 / 55. 17.103
      libavcodec     57. 24.102 / 57. 24.102
      libavformat    57. 25.100 / 57. 25.100
      libavdevice    57.  0.101 / 57.  0.101
      libavfilter     6. 31.100 /  6. 31.100
      libswscale      4.  0.100 /  4.  0.100
      libswresample   2.  0.101 /  2.  0.101
      libpostproc    54.  0.100 / 54.  0.100
    [mp3 @ 0xe8230000] Skipping 0 bytes of junk at 51635.
    [mjpeg @ 0xe80af400] Changing bps to 8
    Input #0, mp3, from '/data/user/0/com.example.ffmpeg_mix/files/BeardedGrain.mp3':
      Metadata:
      Duration: 00:03:35.48, start: 0.025056, bitrate: 321 kb/s
        Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
        Metadata:
          encoder         : Lavc57.10
        Stream #0:1: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 640x640 [SAR 72:72 DAR 1:1], 90k tbr, 90k tbn, 90k tbc
        Metadata:
          comment         : Media (e.g. label side of CD)
    [mp3 @ 0xe8230600] Skipping 0 bytes of junk at 18961.
    [mjpeg @ 0xe80b0400] Changing bps to 8
    Input #1, mp3, from '/data/user/0/com.example.ffmpeg_mix/files/RainyDay.mp3':
      Metadata:
        encoder         : Lavf57.83.100

      Duration: 00:04:25.85, start: 0.025056, bitrate: 320 kb/s
        Stream #1:0: Audio: mp3, 44100 Hz, stereo, s16p, 320 kb/s
        Metadata:
          encoder         : Lavc57.10
        Stream #1:1: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 640x640 [SAR 72:72 DAR 1:1], 90k tbr, 90k tbn, 90k tbc
        Metadata:
          comment         : Media (e.g. label side of CD)
    [swscaler @ 0xe7f21000] deprecated pixel format used, make sure you did set range correctly
    [swscaler @ 0xe7f21000] No accelerated colorspace conversion found from yuv420p to rgb24.
    [mp3 @ 0xe8230c00] Frame rate very high for a muxer not efficiently supporting it.
    Please consider specifying a lower framerate, a different muxer or -vsync 2
    Output #0, mp3, to '/data/user/0/com.example.ffmpeg_mix/files/Mix.mp3':
      Metadata:
        Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, stereo, fltp (default)
        Metadata:
          encoder         : Lavc57.24.102 libmp3lame
        Stream #0:1: Video: png, rgb24, 640x640 [SAR 1:1 DAR 1:1], q=2-31, 200 kb/s, 90k fps, 90k tbn, 90k tbc
        Metadata:
          comment         : Media (e.g. label side of CD)
          encoder         : Lavc57.24.102 png
    Stream mapping:
      Stream #0:0 (mp3) -> amix:input0 (graph 0)
      Stream #1:0 (mp3) -> amix:input1 (graph 0)
      amix (graph 0) -> Stream #0:0 (libmp3lame)
      Str
2021-06-03 20:16:57.824 30155-30155/com.example.ffmpeg_mix I/MainActivity: onStart

File content: ffmpeg is an executable file, Mix.mp3 is mixed from the other two mp3 files;

Insert picture description here





Fourth, the blog source code



Blog source code: