1、JAVA操作音频
package com.day.util;
import java.io.*;
import java.util.*;
public class AudioConvert {
public static void main(String args[]) throws Exception {
/**电脑上需要提前安装ffmpeg windows版*/
//1.合并音频
String tempPath=System.getProperty("user.dir").replaceAll("\\\\","/");
String rootPath=tempPath+"/src/main/resources/static/server/audioMerge/";
String cmd="ffmpeg -i \"concat:";
List<File> files = MyFileOperate.filesGetByFolder(rootPath);
for(int i=0;i<files.size();i++){
File tempFile=files.get(i);
cmd=cmd+tempFile.getName()+"|";
}
cmd=cmd+"\" -c copy "+rootPath+new Date().getTime()+"All.mp3";
System.out.println(cmd);
//拼接命令完毕后调用合并方法
audioMerge(cmd,rootPath);
String tempFolder="src/main/resources/static/server/audioConvert/";
//2.实现音频格式批量转换
List<File> fileList=MyFileOperate.filesGetByFolder(tempFolder);
for(int i=0;i<fileList.size();i++){
File tempFile=fileList.get(i);
String targetFileName=tempFile.getName().substring(0,tempFile.getName().length()-4);
changeWavTo16KPcm(tempFile.getName(),
tempFolder,
targetFileName+".pcm");
}
//3.切割PCM音频
String tempFolderAnother="src/main/resources/static/server/audioConvert/";
List<File> fileListAnother=MyFileOperate.filesGetByFolder(tempFolderAnother);
for(int i=0;i<fileListAnother.size();i++){
File tempFile=fileListAnother.get(i);
String targetFileName=tempFile.getName().substring(0,tempFile.getName().length()-4);
cutPCM("00:00:00",
"00:00:03",
tempFile.getName(),
tempFolderAnother,
targetFileName+"MyCut.pcm");
}
}
/**1.合并音频*/
public static void audioMerge(String cmd,String rootPath) {
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start = System.currentTimeMillis();
//在指定目录下执行cmd命令,需要管理员权限才能执行
Process process = runTime.exec("cmd /c "+cmd,null,new File(rootPath));
//释放进程
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
try {
int mark=process.waitFor();
System.out.println("返回的Mark值:"+mark);
if(mark==0){
long end = System.currentTimeMillis();
System.out.println("合并成功, 耗时:" + (end - start) + "ms");
}else{
System.out.println(rootPath+"合并失败, mark值:" +mark);
}
} catch (InterruptedException e) {
System.out.println("发生错误异常"+e);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
runTime.freeMemory();
}
}
/**2.Mp4转换为Mp3*/
public static void changeMp4ToMp3(String srcFileName,String rootPath,String targetFileName) throws Exception {
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//在指定目录下执行cmd命令
System.out.println("cmd /c ffmpeg -y -i "+srcFileName+" "+targetFileName);
Process process=runTime.exec("cmd /c ffmpeg -y -i "+srcFileName+" "+targetFileName,null,new File(rootPath));
//释放进程
//System.err.println("ffmpeg -y -i "+srcFileName+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetFileName);
//System.err.println(rootPath);
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("转换成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
/**3.Wav转换为16K的PCM*/
public static void changeWavTo16KPcm(String srcFileName,String rootPath,String targetFileName) throws Exception {
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//在指定目录下执行cmd命令
Process process=runTime.exec("cmd /c ffmpeg -y -i "+srcFileName+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetFileName,null,new File(rootPath));
//释放进程
//System.err.println("ffmpeg -y -i "+srcFileName+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetFileName);
//System.err.println(rootPath);
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("转换成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
/**4.Mp3转换为16K的PCM*/
public static void changeMp3To16KPcm(String srcFileName,String rootPath,String targetFileName)throws Exception {
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//在指定目录下执行cmd命令
Process process=runTime.exec("cmd /c ffmpeg -y -i "+srcFileName+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetFileName,null,new File(rootPath));
//释放进程
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("转换成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
/**5.PCM转换为16K的Wav*/
public static void changePcmTo16KWav(String srcFileName,String rootPath,String targetFileName)throws Exception {
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//System.out.println("cmd /c ffmpeg -y -f s16be -ac 1 -ar 16000 -acodec pcm_s16le -i "+srcFileName+" "+targetFileName);
//在指定目录下执行cmd命令
Process process=runTime.exec("cmd /c ffmpeg -y -f s16be -ac 1 -ar 16000 -acodec pcm_s16le -i "+srcFileName+" "+targetFileName,null,new File(rootPath));
//释放进程
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("转换成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
/**6.PCM转换为16K的Mp3*/
public static void changePcmTo16KMp3(String srcFileName,String rootPath,String targetFileName)throws Exception {
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//ffmpeg -y -f s16be -ac 1 -ar 16000 -acodec pcm_s16le -i 英文1分钟.pcm 1.mp3
//System.out.println("cmd /c ffmpeg -y -f s16be -ac 1 -ar 16000 -acodec pcm_s16le -i "+srcFileName+" "+targetFileName);
//在指定目录下执行cmd命令
Process process=runTime.exec("cmd /c ffmpeg -y -f s16be -ac 1 -ar 16000 -acodec pcm_s16le -i "+srcFileName+" "+targetFileName,null,new File(rootPath));
//释放进程
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("转换成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
/**7.PCM转换为16K的PCM*/
public static void changePcmTo16KPcm(String ar,String srcFileName,String rootPath,String targetFileName)throws Exception{
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//在指定目录下执行cmd命令
Process process=runTime.exec("cmd /c ffmpeg -y -f s16le -ar "+ar+" -ac 1 -i "+srcFileName+" -acodec pcm_s16le -f s16le -ac 1 -ar 16000 "+targetFileName,null,new File(rootPath));
//释放进程
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("转换成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
/**8.Wav转换为16K的Wav*/
public static void changeWavTo16KWav(String srcFileName,String rootPath,String targetFileName)throws Exception{
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//在指定目录下执行cmd命令
Process process=runTime.exec("cmd /c ffmpeg -y -i "+srcFileName+" -acodec pcm_s16le -b 16000 -ac 1 -ar 16000 "+targetFileName,null,new File(rootPath));
//释放进程
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("转换成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
/**9.PCM转换为16K的Wav*/
public static void change16KPcmTo16KWav(String srcFileName,String rootPath,String targetFileName)throws Exception{
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//在指定目录下执行cmd命令
Process process=runTime.exec("cmd /c ffmpeg -f s16le -ar 16000 -ac 1 -i "+srcFileName+" "+targetFileName,null,new File(rootPath));
//释放进程
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("转换成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
long end=System.currentTimeMillis();
System.out.println("转换成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
/**10.Mp4视频的切割*/
public static void cutMp4(String startTime,String endTime,
String srcFileName,String rootPath,String targetFileName)throws Exception{
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//在指定目录下执行cmd命令
//ffmpeg -ss 00:00:00 -t 00:00:03 -i source.mp4 -vcodec copy -acodec copy myCut.mp4
Process process=runTime.exec("cmd /c ffmpeg -ss " +startTime
+" -t "+endTime+" -i "
+srcFileName+" -vcodec copy -acodec copy "
+targetFileName,null,new File(rootPath));
//释放进程
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("分割成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
/**11.PCM音频的切割*/
public static void cutPCM(String startTime,String endTime,
String srcFileName,String rootPath,String targetFileName)throws Exception{
Runtime runTime = null;
try {
runTime = Runtime.getRuntime();
long start=System.currentTimeMillis();
//在指定目录下执行cmd命令
//ffmpeg -f s16le -ar 16000 -acodec pcm_s16le -i source.pcm -ss 00:00:00 -t 00:00:03 -f s16le -ar 16000 -ac 1 myCut.pcm
Process process=runTime.exec("cmd /c ffmpeg -f s16le -ar 16000 -acodec pcm_s16le -i "
+srcFileName
+" -ss "
+startTime
+" -t "+endTime+" -f s16le -ar 16000 -ac 1 "
+targetFileName,null,new File(rootPath));
//释放进程
process.getOutputStream().close();
process.getInputStream().close();
process.getErrorStream().close();
int mark=process.waitFor();
if(mark==0){
long end=System.currentTimeMillis();
System.out.println("分割成功, 耗时:"+(end-start)+"ms");
System.out.println("路径保存在: "+rootPath);
}else{
System.out.println(srcFileName+"操作失败,mark值:"+mark);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//run调用lame解码器最后释放内存
runTime.freeMemory();
}
}
}
2、把FLV转成MP3
ffmpeg -i 1.flv -b:a 320k -f MP3 1.mp3 #FLV转成MP3