在閱讀這篇文章之前如果你的項(xiàng)目還未配置好FFmpeg的話蠢涝,請(qǐng)先閱讀AndroidStudio中配置FFmpeg
- 單獨(dú)提取音頻
public static String[] extractAudio(String videoUrl, String outUrl) {
String[] commands = new String[8];
commands[0] = "ffmpeg";
commands[1] = "-i";
commands[2] = videoUrl;
commands[3] = "-acodec";
commands[4] = "copy";
commands[5] = "-vn";
commands[6] = "-y";
commands[7] = outUrl;
return commands;
}
videoUrl參數(shù)為視頻的路徑
outUrl參數(shù)為存儲(chǔ)路徑
- 單獨(dú)提取視頻块饺。沒(méi)有聲音
public static String[] extractVideo(String videoUrl, String outUrl) {
String[] commands = new String[8];
commands[0] = "ffmpeg";
commands[1] = "-i";
commands[2] = videoUrl;
commands[3] = "-vcodec";
commands[4] = "copy";
commands[5] = "-an";
commands[6] = "-y";
commands[7] = outUrl;
return commands;
}
videoUrl參數(shù)為視頻的路徑
outUrl參數(shù)為存儲(chǔ)路徑
- 裁剪音頻
public static String[] cutIntoMusic(String musicUrl, long second, String outUrl) {
String[] commands = new String[10];
commands[0] = "ffmpeg";
commands[1] = "-i";
commands[2] = musicUrl;
commands[3] = "-ss";
commands[4] = "00:00:10";
commands[5] = "-t";
commands[6] = String.valueOf(second);
commands[7] = "-acodec";
commands[8] = "copy";
commands[9] = outUrl;
return commands;
}
musicUrl參數(shù)為音頻的路徑
outUrl參數(shù)為裁剪后的音頻路徑
- 原聲和背景音樂(lè)的合成
public static String[] composeAudio(String audio1, String audio2, String outputUrl) {
String[] commands = new String[10];
commands[0] = "ffmpeg";
//輸入
commands[1] = "-i";
commands[2] = audio1;
//音樂(lè)
commands[3] = "-i";
commands[4] = audio2;
//覆蓋輸出
commands[5] = "-filter_complex";
commands[6] = "amix=inputs=2:duration=first:dropout_transition=2";
commands[7] = "-strict";
commands[8] = "-2";
//輸出文件
commands[9] = outputUrl;
return commands;
}
audio1參數(shù)為原聲的路徑
audio2參數(shù)為背景音樂(lè)的路徑
outputUrl參數(shù)為合成后的路徑
- 修改音頻文件的音量
public static String[] changeAudioOrMusicVol(String audioOrMusicUrl, int vol, String outUrl)
String[] commands = new String[8];
commands[0] = "ffmpeg";
commands[1] = "-i";
commands[2] = audioOrMusicUrl;
commands[3] = "-vol";
commands[4] = String.valueOf(vol);
commands[5] = "-acodec";
commands[6] = "copy";
commands[7] = outUrl;
return commands;
}
audioOrMusicUrl參數(shù)為音頻路徑
vol參數(shù)為音頻的音量
outUrl參數(shù)為修改后音頻的路徑
- 音頻和視頻的合成
public static String[] composeVideo(String videoUrl, String musicOrAudio, String outputUrl, long second) {
String[] commands = new String[14];
commands[0] = "ffmpeg";
//輸入
commands[1] = "-i";
commands[2] = videoUrl;
//音樂(lè)
commands[3] = "-i";
commands[4] = musicOrAudio;
commands[5] = "-ss";
commands[6] = "00:00:00";
commands[7] = "-t";
commands[8] = String.valueOf(second);
//覆蓋輸出
commands[9] = "-vcodec";
commands[10] = "copy";
commands[11] = "-acodec";
commands[12] = "copy";
//輸出文件
commands[13] = outputUrl;
return commands;
}
videoUrl參數(shù)為視頻路徑
musicOrAudio參數(shù)為音頻路徑
outputUrl參數(shù)為合成后的路徑
second參數(shù)為時(shí)間
非常感謝大家的閱讀俺叭,這些代碼基本就可以滿足簡(jiǎn)單的視頻和音頻的處理窘行,有些人會(huì)有疑問(wèn)該如何使用上面編寫的這些代碼呢?不著急鬼吵,后續(xù)會(huì)出如何使用的文章,請(qǐng)需要的人關(guān)注留意鸽凶。