使用到了软件 ffmpeg
package com.hisign.apm.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.concurrent.*;
/**
* @description: 视频转音频
* @author: cyy
* @create: 2021-08-04 11:18
*/
@Slf4j
@Component
public class VideoToAudio {
private ExecutorService pool = Executors.newCachedThreadPool();
// 要输出的音频格式
private static String outputFormat;
// ffmpeg 安装路径
private static String toolPath;
@Value("${ffmpeg.outputFormat}")
public void setOutputFormat(String outputFormat) {
VideoToAudio.outputFormat = outputFormat;
}
@Value("${ffmpeg.toolPath}")
public void setToolPath(String toolPath) {
VideoToAudio.toolPath = toolPath;
}
// private String toolPath = "/Users/cuiyueyang/Documents/tool/ffmpeg-4.4";
/**
* 获得转化后的文件名
*
* @param sourceFilePath : 源视频文件路径
* @return
*/
public String getNewFileName(String sourceFilePath) {
File source = new File(sourceFilePath);
String fileName = source.getName().substring(0, source.getName().lastIndexOf("."));
return fileName + "." + outputFormat;
}
public String getNewFilePath(String sourceFilePath) {
return sourceFilePath.substring(0, sourceFilePath.lastIndexOf("."))
+ "." + outputFormat;
}
public String transformForOneFolder(String sourcePath) {
String targetPath = getNewFilePath(sourcePath);
transform(sourcePath, targetPath);
return targetPath;
}
/**
* 转化音频格式
*
* @param sourcePath : 源视频文件路径
* @param targetPath : 目标音频文件路径
* @return
*/
public void transform(String sourcePath, String targetPath) {
try {
long start = System.currentTimeMillis();
//转换
String command = toolPath + "/ffmpeg -i " + sourcePath + " -acodec " + outputFormat + " -vn " + targetPath;
log.info("转换命令:{}", command);
int n = execCommand(command, 100000);
if (n == 0) {
long end = System.currentTimeMillis();
log.info(sourcePath + " 转换成功, 耗时:" + (end - start) + "ms");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public int execCommand(String command, long timeout) {
Process process = null;
Future<Integer> executeFuture = null;
StringBuffer sb = new StringBuffer();
try {
process = Runtime.getRuntime().exec(command);
//获取进程的标准输入流
final InputStream is = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
final Process p = process;
Callable<Integer> call = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
p.waitFor();
return p.exitValue();
}
};
//使用线程池防止Process阻塞
executeFuture = pool.submit(call);
log.info("转换输出: {}", sb.toString());
return executeFuture.get(timeout, TimeUnit.MILLISECONDS);
} catch (Exception e) {
log.error("execCommand 执行命令失败,command:{},错误原因:{}", command,
e.getMessage());
return 1;
} finally {
log.info("转换结束");
if (executeFuture != null) {
try {
executeFuture.cancel(true);
} catch (Exception ignore) {
}
}
if (process != null) {
process.destroy();
}
}
}
/**
* 批量转化音频格式
*
* @param sourceFolderPath : 源视频文件夹路径
* @param targetFolderPath : 目标音乐文件夹路径
* @return
*/
public void batchTransform(String sourceFolderPath, String targetFolderPath) {
File sourceFolder = new File(sourceFolderPath);
if (sourceFolder.list().length != 0) {
Arrays.asList(sourceFolder.list()).forEach(e -> {
transform(sourceFolderPath + "/" + e, targetFolderPath + "/" + getNewFileName(e));
});
}
}
public static void main(String[] args) {
toolPath = "/Users/cuiyueyang/Documents/tool/ffmpeg-4.4";
outputFormat = "mp3";
VideoToAudio videoToAudio = new VideoToAudio();
String a = videoToAudio.transformForOneFolder("/Users/cuiyueyang/Downloads/temp/video/1.mp4");
System.out.println(a);
// videoToAudio.batchTransform("/Users/cuiyueyang/Downloads/temp/video", "/Users/cuiyueyang/Downloads/temp/audio");
}
}