-
引言
今天在开发过程中,突然遇到给前端flv格式视频还播放不了,flv在开发印象中就是跟mp4格式差不多,本地静态视频资源,怎么还就播放不了,为此只能特别做下视频转换。
-
How to do
1.提前引入包
<!--视频多媒体工具包 包含 FFmpeg、OpenCV-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.3</version>
</dependency>
2.逻辑实现
/**
* flv转mp4
*
* @param inputFile flv地址
* @param outputFile mp4地址
* @throws IOException
* @throws InterruptedException
*/
public static void flv2Mp4(String inputFile, String outputFile) throws IOException{
long start = System.currentTimeMillis();
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputFile);
grabber.start();
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputFile, grabber.getImageWidth(), grabber.getImageHeight());
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFrameRate(grabber.getFrameRate());
recorder.start();
Frame frame;
while ((frame = grabber.grabFrame()) != null) {
recorder.record(frame);
}
recorder.stop();
grabber.stop();
log.info("flv转mp4 占用时长(毫秒): {} ", (System.currentTimeMillis() - start));
}