From 8c19d62158d32a4b5ae1f5df110ed59b6a31b0f1 Mon Sep 17 00:00:00 2001 From: xufuji456 <839789740@qq.com> Date: Thu, 5 Aug 2021 00:44:52 +0800 Subject: [PATCH] fix mix audio: using -vn; add feature: merger audio --- .../ffmpeg/activity/AudioHandleActivity.kt | 11 ++++++++-- .../com/frank/ffmpeg/util/FFmpegUtil.java | 22 ++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt index 2c12966..71093d3 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt @@ -193,8 +193,13 @@ class AudioHandleActivity : BaseActivity() { if (mixSuffix == null || mixSuffix.isEmpty()) { return } - val mixFile = PATH + File.separator + "mix" + mixSuffix - commandLine = FFmpegUtil.mixAudio(srcFile, appendFile, mixFile) + commandLine = if (mixAudio) { + val mixAudio = PATH + File.separator + "mix" + mixSuffix + FFmpegUtil.mixAudio(srcFile, appendFile, mixAudio) + } else { + val mergeAudio = PATH + File.separator + "merge" + mixSuffix + FFmpegUtil.mergeAudio(srcFile, appendFile, mergeAudio) + } } R.id.btn_play_audio//use AudioTrack to play audio -> { @@ -316,6 +321,8 @@ class AudioHandleActivity : BaseActivity() { private val PATH = Environment.getExternalStorageDirectory().path private const val useFFmpeg = true + + private const val mixAudio = true } } diff --git a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java index 3edf4ec..fc30529 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java +++ b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java @@ -70,21 +70,31 @@ public class FFmpegUtil { } /** - * mix one and another audio + * mix multiple audio inputs into a single output * * @param inputPath input file - * @param mixFile background music + * @param mixPath background music * @param outputPath output file * @return mix success or not */ - public static String[] mixAudio(String inputPath, String mixFile, String outputPath) { - //adjust volume:using '-vol 50', which is form 0 to 100 - String mixAudioCmd = "ffmpeg -i %s -i %s -filter_complex amix=inputs=2:duration=first -strict -2 %s"; - mixAudioCmd = String.format(mixAudioCmd, inputPath, mixFile, outputPath); + public static String[] mixAudio(String inputPath, String mixPath, String outputPath) { + //duration: first shortest longest + String mixAudioCmd = "ffmpeg -i %s -i %s -filter_complex amix=inputs=2:duration=longest -vn %s"; + mixAudioCmd = String.format(mixAudioCmd, inputPath, mixPath, outputPath); return mixAudioCmd.split(" "); } //mixing formula: value = sample1 + sample2 - ((sample1 * sample2) >> 0x10) + /** + * merge multiple audio streams into a single multi-channel stream + * + */ + public static String[] mergeAudio(String inputPath, String mergePath, String outputPath) { + String mergeCmd = "ffmpeg -i %s -i %s -filter_complex [0:a][1:a]amerge=inputs=2[aout] -map [aout] %s"; + mergeCmd = String.format(mergeCmd, inputPath, mergePath, outputPath); + return mergeCmd.split(" "); + } + /** * Set echo and delay effect *