目录
视频转音频
1. maven依赖
<!--视频&音频处理-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5</version>
</dependency>
2. 实例
/**
* 视频(mp4|avi)转音频(MP3|WAV)
* WAV要求-
* 1.音频编码:pcm_s16le
* 2.单声道
* 3.音频采样率: 16000hz
* 4.比特率 256kbps
* @param videoPath
* @param audioPath
* @return
*/
public static boolean convertVideoToAudio(String videoPath, String audioPath){
boolean flag = false;
String format = "wav"; // 转写的格式
FFmpegFrameGrabber ffg = new FFmpegFrameGrabber(videoPath);
Frame frame = null;
FFmpegFrameRecorder ffr = null;
try{
ffg.start();
ffr = new FFmpegFrameRecorder(audioPath, ffg.getAudioChannels());
ffr.setFormat(format); // 音频编码格式
// ffr.setSampleRate(ffg.getSampleRate()); // 音频采样率
ffr.setSampleRate(16000); // 音频采样率
ffr.setTimestamp(ffg.getTimestamp()); // 音频时长
ffr.setAudioChannels(1); // 单声道
ffr.setVideoBitrate(256); // 比特率
ffr.setAudioQuality(0); // 音频质量
ffr.start();
int index = 0;
while (true){
frame = ffg.grab();
if (frame == null){
break;
}
if (frame.samples != null){
ffr.recordSamples(frame.sampleRate, frame.audioChannels, frame.samples);
}
index ++;
}
ffr.stop();
ffr.release();
ffg.stop();
flag = true;
}catch (Exception e){
e.printStackTrace();
log.error("视频转音频({}->{})异常:{}", videoPath, audioPath, String.valueOf(e));
}
log.info("视频转音频({}->{}):{}", videoPath, audioPath, flag);
return flag;
}
public static void main(String[] args) {
convertVideoToAudio("F:\\test\\video\\tmp\\10.mp4", "F:\\test\\video\\mp3\\10.wav");
}
标签:视频,String,音频,ffr,ffg,audioPath,frame From: https://www.cnblogs.com/fsh19991001/p/17823466.html