From d3040bcf6d2df651f32e091d6adca6ad41cba428 Mon Sep 17 00:00:00 2001 From: xufulong <839789740@qq.com> Date: Fri, 3 Jan 2020 00:44:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=88=E5=B9=B6=E9=9F=B3?= =?UTF-8?q?=E9=A2=91=E7=AC=AC=E4=BA=8C=E6=AE=B5=E8=87=AA=E5=8A=A8=E8=B7=B3?= =?UTF-8?q?=E8=BF=87=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复合并音频第二段自动跳过问题 --- .../ffmpeg/activity/AudioHandleActivity.java | 7 ++++++- .../com/frank/ffmpeg/util/FFmpegUtil.java | 21 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java index 67793a5..fd03370 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java +++ b/app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java @@ -10,6 +10,8 @@ import android.widget.LinearLayout; import android.widget.ProgressBar; import java.io.File; +import java.util.ArrayList; +import java.util.List; import com.frank.ffmpeg.AudioPlayer; import com.frank.ffmpeg.R; @@ -140,8 +142,11 @@ public class AudioHandleActivity extends BaseActivity { if (!FileUtil.checkFileExist(appendFile)) { return; } + List fileList = new ArrayList<>(); + fileList.add(srcFile); + fileList.add(appendFile); String concatFile = PATH + File.separator + "concat.mp3"; - commandLine = FFmpegUtil.concatAudio(srcFile, appendFile, concatFile); + commandLine = FFmpegUtil.concatAudio(fileList, concatFile); break; case R.id.btn_mix://混音 if (!FileUtil.checkFileExist(appendFile)) { 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 5ce3490..0abb14c 100644 --- a/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java +++ b/app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java @@ -5,6 +5,7 @@ import android.util.Log; import com.frank.ffmpeg.format.VideoLayout; +import java.util.List; import java.util.Locale; /** @@ -46,14 +47,23 @@ public class FFmpegUtil { /** * 使用ffmpeg命令行进行音频合并 * - * @param srcFile 源文件 - * @param appendFile 待追加的文件 + * @param fileList 合并列表 * @param targetFile 目标文件 * @return 合并后的文件 */ - public static String[] concatAudio(String srcFile, String appendFile, String targetFile) { - String concatAudioCmd = "ffmpeg -i concat:%s|%s -acodec copy %s"; - concatAudioCmd = String.format(concatAudioCmd, srcFile, appendFile, targetFile); + public static String[] concatAudio(List fileList, String targetFile) { +// ffmpeg -i concat:%s|%s -acodec copy %s + if (fileList == null || fileList.size() == 0) { + return null; + } + StringBuilder concatBuilder = new StringBuilder(); + concatBuilder.append("concat:"); + for (String file : fileList) { + concatBuilder.append(file).append("|"); + } + String concatStr = concatBuilder.substring(0, concatBuilder.length() - 1); + String concatAudioCmd = "ffmpeg -i %s -acodec libmp3lame -ab 128k -ac 2 -ar 44100 %s"; + concatAudioCmd = String.format(concatAudioCmd, concatStr, targetFile); return concatAudioCmd.split(" ");//以空格分割为字符串数组 } @@ -66,6 +76,7 @@ public class FFmpegUtil { * @return 混合后的文件 */ public static String[] mixAudio(String srcFile, String mixFile, String targetFile) { + //调节音量:使用-vol 50, 其中vol为0-100 String mixAudioCmd = "ffmpeg -i %s -i %s -filter_complex amix=inputs=2:duration=first -strict -2 %s"; mixAudioCmd = String.format(mixAudioCmd, srcFile, mixFile, targetFile); return mixAudioCmd.split(" ");//以空格分割为字符串数组