|
@@ -1,7 +1,6 @@
|
|
|
package com.cooleshow.musicmerge.helper;
|
|
|
|
|
|
import android.text.TextUtils;
|
|
|
-import android.util.Log;
|
|
|
|
|
|
import com.cooleshow.base.data.net.RetrofitClientNoToken;
|
|
|
import com.cooleshow.base.utils.EncryptUtils;
|
|
@@ -44,8 +43,9 @@ public class MixHelper {
|
|
|
public static final String BASE_PATH = FileUtils.getCacheDir(Utils.getApp(), "musicmerge");
|
|
|
|
|
|
public static final String videoMp3Path = BASE_PATH + File.separator + "videoBgm.wav";
|
|
|
- public static final String accompanimentMp3Path = BASE_PATH + File.separator + "accompaniment_bgm.mp3";
|
|
|
+ public static final String accompanyAligningMp3Path = BASE_PATH + File.separator + "accompaniment_bgm.mp3";//对齐长度的伴奏文件
|
|
|
public static final String accompanimentWAVPath = BASE_PATH + File.separator + "accompaniment_bgm.wav";
|
|
|
+ public static final String accompanimentAligningWAVPath2 = BASE_PATH + File.separator + "accompaniment_bgm_cut.wav";//对齐长度的伴奏文件
|
|
|
public static final String wavToMpePath = BASE_PATH + File.separator + "accompaniment_bgm_result.mp3";
|
|
|
public static final String mergeMp3Path = BASE_PATH + File.separator + "merge.mp3";
|
|
|
public static final String mergeMp3PathForMP4 = BASE_PATH + File.separator + "merge.aac";//目前只能aac格式,mp3格式合成视频后,IOS原生播放器播放没有声音
|
|
@@ -415,6 +415,8 @@ public class MixHelper {
|
|
|
FileUtils.delete(onlyVideoPath);
|
|
|
FileUtils.delete(accompanimentWAVPath);
|
|
|
FileUtils.delete(handleVideoPath);
|
|
|
+ FileUtils.delete(accompanyAligningMp3Path);
|
|
|
+ FileUtils.delete(accompanimentAligningWAVPath2);
|
|
|
// boolean b = FileUtils.deleteAllInDir(BASE_PATH);
|
|
|
// LogUtils.i(TAG, "deleteAllTempFile:" + b);
|
|
|
}
|
|
@@ -430,7 +432,7 @@ public class MixHelper {
|
|
|
|
|
|
public int getMp3FromWav(String wavFilePath, String outPath) {
|
|
|
//ffmpeg -i video.mp4 -vn audio.mp3
|
|
|
- Log.i(TAG, "getMp3FromWav start wavFilePath:" + wavFilePath + "\noutPath:" + outPath);
|
|
|
+ LOG.i(TAG, "getMp3FromWav start wavFilePath:" + wavFilePath + "\noutPath:" + outPath);
|
|
|
// String[] commands = new String[5];
|
|
|
// commands[0] = "ffmpeg";
|
|
|
// commands[1] = "-i";
|
|
@@ -462,7 +464,7 @@ public class MixHelper {
|
|
|
String command = commands[i];
|
|
|
stringBuilder.append(command).append(" ");
|
|
|
}
|
|
|
- Log.i(TAG, "println:" + stringBuilder.toString());
|
|
|
+ LOG.i(TAG, "println:" + stringBuilder.toString());
|
|
|
}
|
|
|
|
|
|
|
|
@@ -496,7 +498,7 @@ public class MixHelper {
|
|
|
|
|
|
public int getVideoMp3_2(String videoPath, String outPath) {
|
|
|
//ffmpeg -i video.mp4 -vn audio.mp3
|
|
|
- Log.i(TAG, "getVideoMp3 start videoPath:" + videoPath + "\noutPath:" + outPath);
|
|
|
+ LOG.i(TAG, "getVideoMp3 start videoPath:" + videoPath + "\noutPath:" + outPath);
|
|
|
String[] commands = new String[5];
|
|
|
commands[0] = "ffmpeg";
|
|
|
commands[1] = "-i";
|
|
@@ -509,8 +511,18 @@ public class MixHelper {
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
+ public long getAudioDuration(String inputPath) {
|
|
|
+ String[] audioDuration = FFmpegUtil.getAudioDuration(inputPath);
|
|
|
+ if (audioDuration != null) {
|
|
|
+ println(audioDuration);
|
|
|
+ String s = FFmpegCmd.executeProbeSynchronize(audioDuration);
|
|
|
+ LOG.i(TAG, "getAudioDuration:" + s);
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
private int getOnlyVideo(String videoPath, String out) {
|
|
|
- Log.i(TAG, "extractVideo start");
|
|
|
+ LOG.i(TAG, "extractVideo start");
|
|
|
String[] commands1 = FFmpegUtil.extractVideo(videoPath, out);
|
|
|
if (commands1 != null) {
|
|
|
return FFmpegCmd.executeSync(commands1);
|
|
@@ -519,7 +531,7 @@ public class MixHelper {
|
|
|
}
|
|
|
|
|
|
private int megreMp3AndMp4(String videoPath, String mp3Path, String outPath) {
|
|
|
- Log.i(TAG, "mergeMp4 start");
|
|
|
+ LOG.i(TAG, "mergeMp4 start");
|
|
|
String[] commands = FFmpegUtil.mediaMux(videoPath, mp3Path, true, outPath);
|
|
|
println(commands);
|
|
|
if (commands != null) {
|
|
@@ -529,7 +541,7 @@ public class MixHelper {
|
|
|
}
|
|
|
|
|
|
private int megreMp3AndMp4V2(String videoPath, String mp3Path, String outPath, float videoDelay) {
|
|
|
- Log.i(TAG, "mergeMp4 start");
|
|
|
+ LOG.i(TAG, "mergeMp4 start");
|
|
|
String[] commands = FFmpegUtil.mediaMux2(videoPath, mp3Path, true, outPath, videoDelay);
|
|
|
println(commands);
|
|
|
if (commands != null) {
|
|
@@ -549,7 +561,7 @@ public class MixHelper {
|
|
|
return lastPath;
|
|
|
}
|
|
|
|
|
|
- public void startMixForMp4(String accompanimentMp3Path, String recordFilePath, int offsetValue, float volume1, float volume2, ResultCallback<String> resultCallback) {
|
|
|
+ public void startMixForMp4(String accompanimentMp3Path, String recordFilePath, long recordFileDuration, int offsetValue, float volume1, float volume2, ResultCallback<String> resultCallback) {
|
|
|
Observable.create(new ObservableOnSubscribe<String>() {
|
|
|
@Override
|
|
|
public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Throwable {
|
|
@@ -557,53 +569,62 @@ public class MixHelper {
|
|
|
int totalStep;
|
|
|
if (offsetValue > 0) {
|
|
|
//获取视频中的MP3 MP3转换为wav 获取纯视频 剪裁音频 裁剪视频 合并音频 合并最终音视频
|
|
|
- totalStep = 7;
|
|
|
+ totalStep = 8;
|
|
|
} else {
|
|
|
//获取视频中的MP3 MP3转换为wav 获取纯视频 补齐视频 合并音频 合并最终音视频
|
|
|
//获取视频中的MP3 MP3转换为wav 获取纯视频 合并音频 合并最终音视频
|
|
|
- totalStep = offsetValue != 0 ? 5 : 5;
|
|
|
+ totalStep = 6;
|
|
|
}
|
|
|
resetCondition(resultCallback, totalStep);
|
|
|
|
|
|
- Log.i(TAG, "转换开始:" + offsetValue);
|
|
|
+ LOG.i(TAG, "转换开始:" + offsetValue);
|
|
|
int getVideoMp3Result = getVideoMp3_2(recordFilePath, videoMp3Path);
|
|
|
+// long audioDuration = getAudioDuration(videoMp3Path);
|
|
|
+// LOG.i(TAG,"audioDuration:"+audioDuration);
|
|
|
+
|
|
|
completed_num++;
|
|
|
- Log.i(TAG, "getVideoMp3 complete:" + getVideoMp3Result);
|
|
|
+ LOG.i(TAG, "getVideoMp3 complete:" + getVideoMp3Result);
|
|
|
//mix原音和视频bgm mp3
|
|
|
// String accompanyPath = accompanimentMp3Path;
|
|
|
- Log.i(TAG, "mp3ToWav start");
|
|
|
+ LOG.i(TAG, "mp3ToWav start");
|
|
|
int i = mp3ToWav(accompanimentMp3Path, accompanimentWAVPath);
|
|
|
completed_num++;
|
|
|
- Log.i(TAG, "mp3ToWav complete:" + i);
|
|
|
+ float tartSecond = recordFileDuration * 1.0f / 1000;
|
|
|
+ LOG.i(TAG, "tartSecond :" + tartSecond);
|
|
|
+ //将伴奏文件裁剪成和演奏文件一样长度
|
|
|
+ cutAudio(accompanimentWAVPath, 0, tartSecond, accompanimentAligningWAVPath2);
|
|
|
+ completed_num++;
|
|
|
+ LOG.i(TAG, "mp3ToWav complete:" + i);
|
|
|
//获取纯视频
|
|
|
- Log.i(TAG, "getOnlyVideo start");
|
|
|
+ LOG.i(TAG, "getOnlyVideo start");
|
|
|
int getOnlyVideoResult = getOnlyVideo(recordFilePath, onlyVideoPath);
|
|
|
completed_num++;
|
|
|
- Log.i(TAG, "getOnlyVideo complete:" + getOnlyVideoResult);
|
|
|
+ LOG.i(TAG, "getOnlyVideo complete:" + getOnlyVideoResult);
|
|
|
String recordLastPath = videoMp3Path;
|
|
|
String recordVideoLastPath = onlyVideoPath;
|
|
|
int recordFileOffset = 0;
|
|
|
int accompanimentFileOffset = 0;
|
|
|
if (offsetValue > 0) {
|
|
|
float cutM = Math.abs(offsetValue) * 1.0f / 1000;
|
|
|
- Log.i(TAG, "cutAudio start");
|
|
|
+ LOG.i(TAG, "cutAudio start");
|
|
|
int cutAudioResult = cutAudio(recordLastPath, cutM, 5000000, cutPath);
|
|
|
completed_num++;
|
|
|
- Log.i(TAG, "cutAudio end:" + cutAudioResult);
|
|
|
+ LOG.i(TAG, "cutAudio end:" + cutAudioResult);
|
|
|
|
|
|
- Log.i(TAG, "cutVideo start");
|
|
|
+ LOG.i(TAG, "cutVideo start");
|
|
|
int cutVideoResult = cutVideo(onlyVideoPath, cutM, 5000000, handleVideoPath);
|
|
|
completed_num++;
|
|
|
- Log.i(TAG, "cutVideo end:" + cutVideoResult);
|
|
|
+ LOG.i(TAG, "cutVideo end:" + cutVideoResult);
|
|
|
recordLastPath = cutPath;
|
|
|
recordVideoLastPath = handleVideoPath;
|
|
|
} else {
|
|
|
recordFileOffset = Math.abs(offsetValue);
|
|
|
}
|
|
|
- Log.i(TAG, "mixMp3 start");
|
|
|
- int mixMp3Result = mixMp3(recordLastPath, accompanimentWAVPath, mergeMp3PathForMP4, recordFileOffset, accompanimentFileOffset, volume1, volume2);
|
|
|
+ LOG.i(TAG, "mixMp3 start");
|
|
|
+ //伴奏在前,录制在后
|
|
|
+ int mixMp3Result = mixMp3(accompanimentAligningWAVPath2,recordLastPath, mergeMp3PathForMP4, accompanimentFileOffset,recordFileOffset, volume2,volume1);
|
|
|
completed_num++;
|
|
|
- Log.i(TAG, "mixMp3 complete:" + mixMp3Result);
|
|
|
+ LOG.i(TAG, "mixMp3 complete:" + mixMp3Result);
|
|
|
String lastResultPath = getLastResultPath();
|
|
|
int mergeResult;
|
|
|
if (offsetValue < 0) {
|
|
@@ -612,13 +633,13 @@ public class MixHelper {
|
|
|
} else {
|
|
|
mergeResult = megreMp3AndMp4(recordVideoLastPath, mergeMp3PathForMP4, lastResultPath);
|
|
|
}
|
|
|
- Log.i(TAG, "mergeMp4 complete:" + mergeResult);
|
|
|
- Log.i(TAG, "转换完成");
|
|
|
+ LOG.i(TAG, "mergeMp4 complete:" + mergeResult);
|
|
|
+ LOG.i(TAG, "转换完成");
|
|
|
resetCount();
|
|
|
|
|
|
if (emitter != null) {
|
|
|
boolean isSuccess = getVideoMp3Result == 0 && mixMp3Result == 0 && getOnlyVideoResult == 0 && mergeResult == 0;
|
|
|
- Log.i(TAG, "转换是否全部isSuccess:" + isSuccess);
|
|
|
+ LOG.i(TAG, "转换是否全部isSuccess:" + isSuccess);
|
|
|
if (isSuccess) {
|
|
|
emitter.onNext(lastResultPath);
|
|
|
} else {
|
|
@@ -664,41 +685,48 @@ public class MixHelper {
|
|
|
FFmpegCmd.setOnProgressCallback(mEventListener);
|
|
|
}
|
|
|
|
|
|
- public void startMix(String accompanimentMp3Path, String recordFilePath, int offsetValue, float volume1, float volume2, ResultCallback<String> resultCallback) {
|
|
|
+ public void startMix(String accompanyMp3Path, String recordFilePath, long recordFileDuration, int offsetValue, float volume1, float volume2, ResultCallback<String> resultCallback) {
|
|
|
Observable.create(new ObservableOnSubscribe<String>() {
|
|
|
@Override
|
|
|
public void subscribe(@NonNull ObservableEmitter<String> emitter) throws Throwable {
|
|
|
- Log.i(TAG, "转换开始");
|
|
|
+ LOG.i(TAG, "转换开始");
|
|
|
// int getVideoMp3Result = getVideoMp3_2(videoPath, videoMp3Path);
|
|
|
-// Log.i(TAG, "getVideoMp3 complete:" + getVideoMp3Result);
|
|
|
-// Log.i(TAG, "getMp3FromWav start");
|
|
|
+// LOG.i(TAG, "getVideoMp3 complete:" + getVideoMp3Result);
|
|
|
+// LOG.i(TAG, "getMp3FromWav start");
|
|
|
deleteAllTempFile();
|
|
|
- resetCondition(resultCallback, offsetValue > 0 ? 3 : 2);
|
|
|
+ resetCondition(resultCallback, offsetValue > 0 ? 4 : 3);
|
|
|
int mp3FromWav = getMp3FromWav(recordFilePath, recordFileMp3Path);
|
|
|
completed_num++;
|
|
|
- Log.i(TAG, "getMp3FromWav complete:" + mp3FromWav);
|
|
|
+ LOG.i(TAG, "getMp3FromWav complete:" + mp3FromWav);
|
|
|
String recordLastPath = recordFileMp3Path;
|
|
|
int recordFileOffset = 0;
|
|
|
int accompanimentFileOffset = 0;
|
|
|
if (offsetValue > 0) {
|
|
|
float cutM = Math.abs(offsetValue) * 1.0f / 1000;
|
|
|
- Log.i(TAG, "cutAudio start");
|
|
|
+ LOG.i(TAG, "cutAudio start");
|
|
|
int cutAudioResult = cutAudio(recordLastPath, cutM, 5000000, cutPath);
|
|
|
completed_num++;
|
|
|
- Log.i(TAG, "cutAudio end:" + cutAudioResult);
|
|
|
+ LOG.i(TAG, "cutAudio end:" + cutAudioResult);
|
|
|
recordLastPath = cutPath;
|
|
|
} else {
|
|
|
recordFileOffset = Math.abs(offsetValue);
|
|
|
}
|
|
|
+ float tartSecond = recordFileDuration * 1.0f / 1000;
|
|
|
+ LOG.i(TAG, "tartSecond :" + tartSecond);
|
|
|
+ //将伴奏文件裁剪成和演奏文件一样长度
|
|
|
+ int aligningAccompanyResult = cutAudio(accompanyMp3Path, 0, tartSecond, accompanyAligningMp3Path);
|
|
|
+ completed_num++;
|
|
|
+
|
|
|
//mix原音和视频bgm mp3
|
|
|
- Log.i(TAG, "mixMp3 start");
|
|
|
- int mixMp3Result = mixMp3(recordLastPath, accompanimentMp3Path, mergeMp3Path, recordFileOffset, accompanimentFileOffset, volume1, volume2);
|
|
|
- Log.i(TAG, "mixMp3 complete:" + mixMp3Result);
|
|
|
+ LOG.i(TAG, "mixMp3 start");
|
|
|
+ //伴奏在前,录制在后
|
|
|
+ int mixMp3Result = mixMp3(accompanyAligningMp3Path,recordLastPath, mergeMp3Path, accompanimentFileOffset,recordFileOffset, volume2,volume1);
|
|
|
+ LOG.i(TAG, "mixMp3 complete:" + mixMp3Result);
|
|
|
resetCount();
|
|
|
|
|
|
if (emitter != null) {
|
|
|
- boolean isSuccess = mp3FromWav == 0 && mixMp3Result == 0;
|
|
|
- Log.i(TAG, "转换是否全部isSuccess:" + isSuccess);
|
|
|
+ boolean isSuccess = mp3FromWav == 0 && aligningAccompanyResult == 0 && mixMp3Result == 0;
|
|
|
+ LOG.i(TAG, "转换是否全部isSuccess:" + isSuccess);
|
|
|
if (isSuccess) {
|
|
|
emitter.onNext(mergeMp3Path);
|
|
|
} else {
|
|
@@ -746,7 +774,7 @@ public class MixHelper {
|
|
|
}
|
|
|
|
|
|
public int downloadM3U8FileToMp4(String url, String encryption_key, String encryption_kid, String outPath) {
|
|
|
- Log.i(TAG, "downloadM3U8FileToMp4 start url:" + url + "\noutPath:" + outPath);
|
|
|
+ LOG.i(TAG, "downloadM3U8FileToMp4 start url:" + url + "\noutPath:" + outPath);
|
|
|
String[] commands = new String[6];
|
|
|
commands[0] = "ffmpeg";
|
|
|
commands[1] = "-i";
|
|
@@ -789,7 +817,7 @@ public class MixHelper {
|
|
|
}
|
|
|
|
|
|
public int decryptionMp4(String input, String key, String output) {
|
|
|
- Log.i(TAG, "encryptionMp4 start input:" + input + "\noutPath:" + output);
|
|
|
+ LOG.i(TAG, "encryptionMp4 start input:" + input + "\noutPath:" + output);
|
|
|
String[] commands = new String[6];
|
|
|
commands[0] = "ffmpeg";
|
|
|
commands[1] = "-i";
|