improve cmd of concating audio

improve cmd of concating audio
pull/166/head
xufulong 5 years ago
parent 8e64125f0b
commit a8a369210c
  1. 36
      app/src/main/java/com/frank/ffmpeg/activity/AudioHandleActivity.java
  2. 8
      app/src/main/java/com/frank/ffmpeg/util/FFmpegUtil.java

@ -31,7 +31,7 @@ import static com.frank.ffmpeg.handler.FFmpegHandler.MSG_FINISH;
public class AudioHandleActivity extends BaseActivity {
private final static String PATH = Environment.getExternalStorageDirectory().getPath();
private String appendFile = PATH + File.separator + "test.mp3";
private String appendFile = PATH + File.separator + "heart.m4a";
private ProgressBar progressAudio;
private LinearLayout layoutAudioHandle;
@ -142,12 +142,14 @@ public class AudioHandleActivity extends BaseActivity {
if (!FileUtil.checkFileExist(appendFile)) {
return;
}
List<String> fileList = new ArrayList<>();
fileList.add(srcFile);
fileList.add(appendFile);
String concatFile = PATH + File.separator + "concat.mp3";
commandLine = FFmpegUtil.concatAudio(fileList, concatFile);
break;
// List<String> fileList = new ArrayList<>();
// fileList.add(srcFile);
// fileList.add(appendFile);
// String concatFile = PATH + File.separator + "concat.mp3";
// commandLine = FFmpegUtil.concatAudio(fileList, concatFile);
// break;
concatAudio(srcFile);
return;
case R.id.btn_mix://mix audio
if (!FileUtil.checkFileExist(appendFile)) {
return;
@ -204,6 +206,26 @@ public class AudioHandleActivity extends BaseActivity {
}
}
private void concatAudio(String selectedPath) {
if (ffmpegHandler == null || selectedPath.isEmpty() || appendFile.isEmpty()) {
return;
}
String outputPath1 = PATH + File.separator + "output1.mp3";
String outputPath2 = PATH + File.separator + "output2.mp3";
String targetPath = PATH + File.separator + "concatAudio.mp3";
String[] transformCmd1 = FFmpegUtil.transformAudio(selectedPath, "libmp3lame", outputPath1);
String[] transformCmd2 = FFmpegUtil.transformAudio(appendFile, "libmp3lame", outputPath2);
List<String> fileList = new ArrayList<>();
fileList.add(outputPath1);
fileList.add(outputPath2);
String[] jointVideoCmd = FFmpegUtil.concatAudio(fileList, targetPath);
List<String[]> commandList = new ArrayList<>();
commandList.add(transformCmd1);
commandList.add(transformCmd2);
commandList.add(jointVideoCmd);
ffmpegHandler.executeFFmpegCmds(commandList);
}
@Override
protected void onDestroy() {
super.onDestroy();

@ -28,6 +28,12 @@ public class FFmpegUtil {
return transformAudioCmd.split(" ");
}
public static String[] transformAudio(String srcFile, String acodec, String targetFile) {
String transformAudioCmd = "ffmpeg -i %s -acodec %s -ac 2 -ar 44100 %s";
transformAudioCmd = String.format(transformAudioCmd, srcFile, acodec, targetFile);
return transformAudioCmd.split(" ");
}
/**
* cut audio, you could assign the startTime and duration which you want to
*
@ -62,7 +68,7 @@ public class FFmpegUtil {
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";
String concatAudioCmd = "ffmpeg -i %s -acodec copy %s";
concatAudioCmd = String.format(concatAudioCmd, concatStr, targetFile);
return concatAudioCmd.split(" ");
}

Loading…
Cancel
Save