最近遇到一个mp4视屏在浏览器页面播放无画面问题,经过多次研究发现视屏编码在浏览器格式不支持,因此需要对视屏的编码进行转换,兼容性最好的是AVC(H264)格式
下面给大家介绍一个main方法对视频编码转换的工具类
jar包:
ws.schild jave-core:2.4.5
ws.schild jave-native-win64:2.4.5
可以在maven库搜索下载
1、pom中加入这两个依赖:
<!-- https://mvnrepository.com/artifact/ws.schild/jave-core --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-core</artifactId> <version>2.4.5</version> </dependency> <!-- https://mvnrepository.com/artifact/ws.schild/jave-native-win64 --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-native-win64</artifactId> <version>2.4.5</version> </dependency>
2、代码如下:
package org.springeos.modules.quartz.m3u8.main; import ws.schild.jave.*; import java.io.File; import java.io.IOException; public class test { public static void main(String[] args) throws IOException { File source = new File("source.avi"); File target = new File("target.avi"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame");//音频编码格式 //audio.setBitRate(new Integer(56000));//设置比特率,比特率越大,转出来的音频越大(默认是128000,最好默认就行,有特殊要求再设置) audio.setChannels(new Integer(1)); audio.setSamplingRate(new Integer(22050)); VideoAttributes video = new VideoAttributes(); video.setCodec("libx264");//视屏编码格式 //video.setBitRate(new Integer(56000));//设置比特率,比特率越大,转出来的视频越大(默认是128000,最好默认就行,有特殊要求再设置) video.setFrameRate(new Integer(15));//数值设置小了,视屏会卡顿 EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mp4"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); MultimediaObject multimediaObject=new MultimediaObject(source); try { encoder.encode(multimediaObject,target,attrs); }catch (IllegalArgumentException e){ e.printStackTrace(); }catch (InputFormatException e){ e.printStackTrace(); }catch (EncoderException e){ e.printStackTrace(); } } }
下面这个链接是 JAVE 视音频转码链接,可以看下里面的一些属性设置;
https://blog.csdn.net/qllinhongyu/article/details/29817297