配音方案
后臺以zip壓縮包的方式提供兩個視屏頻文件,和一個lrc文件 ,一個視頻是帶背景音和人聲,另一個視屏只帶背景音。
lrc 里面提供具體要配音的 時間段和字幕信息。
android 使用ffmpeg 去實(shí)現(xiàn) https://github.com/tanersener/mobile-ffmpeg
大致思路是 先把所有的 自己錄制的配音音頻 定點(diǎn)拼接起來 再和帶背景音的視頻 混音 生成一個新的視頻
大致分為兩步
1,配音定點(diǎn)插入 生成 一條完整的配音音頻
ffmpeg -y -i /storage/emulated/0/1/audio/test1.mp3 -i /storage/emulated/0/1/audio/test2.mp3 -i /storage/emulated/0/1/audio/test3.mp3 -i /storage/emulated/0/1/audio/test4.mp3 -filter_complex "[1]adelay=10000|10000[1_]; [2]adelay=20000|20000[2_]; [3]adelay=30000|30000[3_]; [0][1_][2_][3_]amix=4" /storage/emulated/0/1/audio/apptend.mp3
這條命令的意思是 test1.mp3 和 前面延遲了10s 的test2.mp3 和 前面延遲了 20s的 test3.mp3 和 前面延遲了30s的 test4.mp3 混音 生成 apptend.mp3
使用 -filter_complex adelay 和 -filter_complex amix 成功實(shí)現(xiàn)了多條音頻定點(diǎn)插入 生成一條新的音頻,相當(dāng)于第一段配音從起始點(diǎn)插入 ,第二段配音從 第一段要配音的視屏 末尾點(diǎn) 插入 以此內(nèi)推,生成一段新的配音視頻
以為為java 封裝獲取命令
/**
* key 對應(yīng)某段配音 的本地地址
* value 配音對應(yīng)在原視頻的 截止時間
*
outputAudioPath 輸出地址
*/
public static String getmixAudiocommandLine(LinkedHashMap<String,Integer> para, String outputAudioPath){
String preCommand = "-y";
String lastCommand=" -filter_complex \"";
int index=0;
String tip="_";
for (Map.Entry<String, Integer> entry : para.entrySet()) {
index++;
String audioAddress = entry.getKey();
preCommand+=" -i "+audioAddress;
int audioLength = entry.getValue();
if (index==para.entrySet().size()){
for (int j=0;j<para.entrySet().size();j++){
if (j==0){
lastCommand+="[0]";
}else if (j==para.entrySet().size()-1){
lastCommand+="["+j+tip+"]"+"amix="+para.entrySet().size()+"\" ";
}else {
lastCommand+="["+j+tip+"]";
}
}
}else {
lastCommand+="["+index+"]adelay="+audioLength+"|"+audioLength+"["+index+tip+"]; ";
}
}
String commandLine = preCommand+lastCommand+outputAudioPath;
Log.e("commandLine---",commandLine);
return commandLine;
}
2, 用生成完整的配音音頻和zip包里的 帶背景音的 視屏混音 生成一個新的視頻
ffmpeg -i /storage/emulated/0/1/video/1.mp4 -i /storage/emulated/0/1/audio/apptend.mp3 -vcodec copy -filter_complex amix=inputs=2:duration=first -strict -2 /storage/emulated/0/1/output/mix.mp4
如下為 java 封裝
/**
* 使用ffmpeg命令行進(jìn)行音頻混合
* @param srcFile 源文件
* @param mixFile 待混合文件
* @param targetFile 目標(biāo)文件
* @return 混合后的文件
*/
public static String mixAudio(String srcFile, String mixFile, String targetFile){
String mixAudioCmd = "-y -i %s -i %s -vcodec copy -filter_complex amix=inputs=2:duration=first -strict -2 %s";
mixAudioCmd = String.format(mixAudioCmd, srcFile, mixFile, targetFile);
return mixAudioCmd;
}
性能
華為p20 手機(jī) 4段 音頻 和 一個 5M多的1分鐘17秒的視屏 合成 總共花費(fèi) 4s ,支持mp3 ,wav aac m4a 格式音頻 MP4視頻
弊端
使用的都是開源庫,底層都是c實(shí)現(xiàn),出問題了基本無法修改,各種機(jī)型的兼容性 也無法得知。