fix mix audio: using -vn;

add feature: merger audio
dev
xufuji456 3 years ago
parent 62f7b27ba9
commit 8c19d62158
  1. 11
      app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.kt
  2. 22
      app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java

@ -193,8 +193,13 @@ class AudioHandleActivity : BaseActivity() {
if (mixSuffix == null || mixSuffix.isEmpty()) { if (mixSuffix == null || mixSuffix.isEmpty()) {
return return
} }
val mixFile = PATH + File.separator + "mix" + mixSuffix commandLine = if (mixAudio) {
commandLine = FFmpegUtil.mixAudio(srcFile, appendFile, mixFile) 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 R.id.btn_play_audio//use AudioTrack to play audio
-> { -> {
@ -316,6 +321,8 @@ class AudioHandleActivity : BaseActivity() {
private val PATH = Environment.getExternalStorageDirectory().path private val PATH = Environment.getExternalStorageDirectory().path
private const val useFFmpeg = true private const val useFFmpeg = true
private const val mixAudio = true
} }
} }

@ -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 inputPath input file
* @param mixFile background music * @param mixPath background music
* @param outputPath output file * @param outputPath output file
* @return mix success or not * @return mix success or not
*/ */
public static String[] mixAudio(String inputPath, String mixFile, String outputPath) { public static String[] mixAudio(String inputPath, String mixPath, String outputPath) {
//adjust volume:using '-vol 50', which is form 0 to 100 //duration: first shortest longest
String mixAudioCmd = "ffmpeg -i %s -i %s -filter_complex amix=inputs=2:duration=first -strict -2 %s"; String mixAudioCmd = "ffmpeg -i %s -i %s -filter_complex amix=inputs=2:duration=longest -vn %s";
mixAudioCmd = String.format(mixAudioCmd, inputPath, mixFile, outputPath); mixAudioCmd = String.format(mixAudioCmd, inputPath, mixPath, outputPath);
return mixAudioCmd.split(" "); return mixAudioCmd.split(" ");
} }
//mixing formula: value = sample1 + sample2 - ((sample1 * sample2) >> 0x10) //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 * Set echo and delay effect
* *

Loading…
Cancel
Save