FFmpeg簡介:FFmpeg是一個自由軟件,可以運行音頻和視頻多種格式的錄影、轉換、流功能,包含了libavcodec ─這是一個用于多個項目中音頻和視頻的解碼器庫,以及l(fā)ibavformat——一個音頻與視頻格式轉換庫。
FFmpeg命令:
? ? 一、mp3截取
????????????ffmpeg?-y?-i?test.mp3?-ss?00:00:00?-t?00:00:03?-acodec?copy?output_mp3.mp3
? ? ? 參數(shù)說明
? ? ? ? ? ? ? -y :? ? ?允許覆蓋
? ? ? ? ? ? ? -i test.mp3:? 源文件
? ? ? ? ? ? ?-ss 00:00:00:? ?開始時間
? ? ? ? ? ? ?-t 00:00:03:? ?截取音頻時間長度
? ? ? ? ? ? ?-acodec copy:編碼格式復制
? ?二、合并兩個mp3文件為一個
? ? ? ? ??ffmpeg -i? "contat:file1.mp3|file2.mp3"??-acodec copy? merge.mp3
? ? ? ? ? ? ?file1.mp3:源文件1
?????????????file2.mp3:源文件2
? ? ? ? ? ? ?merge.mp3:合并后的目標文件
? ?二、mp3轉pcm
? ?? ??????ffmpeg -y -i?test.mp3?-acodec pcm_s16le -f s16le -ac?2?-ar?16000?16k.pcm
? ? ?參數(shù)說明
? ? ? ? ? ?-y:? 允許覆蓋
? ? ? ? ? ?-i test.mp3:? 源文件
? ? ? ? ? -acodec pcm_s16le: 編碼器
? ? ? ? ? -f s16le:? 強制文件格式
? ? ? ? ?-ac 2: 雙聲道
? ? ? ? ?-ar 16000: 采樣率
? 三、pcm轉mp3
? ? ??? ??ffmpeg -y -f s16be -ac?2?-ar?16000?-acodec pcm_s16le -i?16k.pcm?new_mp3.mp3
????????參數(shù)說明
????????????-y: 允許覆蓋
????????????-f s16le: 強制文件格式
????????????-ac 2: 雙聲道
????????????-ar 16000: 采樣率
????????????-acodec pcm_s16le: 編碼器
????????????-i test.mp3: 源文件
? ? 四、上代碼
? ??public class Mp3ToPcm {
????????????public static StringchangeMp3ToPcm(String sourcePath){
????????????????????String webroot ="E:\\ffmpeg\\bin";
? ? ? ????????????? String targetPath ="E:\\pcm\\test.pcm";
? ? ? ? ????????????Runtime run =null;
? ? ? ????????????? try {
????????????????????????????run = Runtime.getRuntime();
? ? ? ? ? ? ???????????????? long start=System.currentTimeMillis();
? ? ? ? ? ? ???????????????? System.out.println(new File(webroot).getAbsolutePath());
? ? ? ? ? ? //mp3轉pcm
? ? ? ? ? ? Process p=run.exec(new File(webroot).getAbsolutePath() +"/ffmpeg -y -i "+sourcePath+" -acodec pcm_s16le -f s16le -ac 2 -ar 16000 "+targetPath);
? ? ? ? ? ? //釋放進程
? ? ? ? ? ? p.getOutputStream().close();
? ? ? ? ? ? p.getInputStream().close();
? ? ? ? ? ? p.getErrorStream().close();
? ? ? ? ? ? p.waitFor();
? ? ? ? ? ? long end=System.currentTimeMillis();
? ? ? ? ? ? System.out.println(sourcePath+" convert success, costs:"+(end-start)+"ms");
? ? ? ? }catch (Exception e) {
e.printStackTrace();
? ? ? ? }finally{
//run調用lame解碼器最后釋放內存
? ? ? ? run.freeMemory();
? ? ? ? }
return null;
? ? }
}