使用JAVE转换视频格式以及获取视频某帧作为图片
程序员文章站
2022-07-07 21:12:33
...
官网下载jar包:http://www.sauronsoftware.it/projects/jave/manual.php
我下载版本是:jave-1.0.2.zip
转flv:
转MP4:
获取某一帧作为png图片:
从官网下载的帮助文档中可以看到有很多编码格式,以此类推,相信可以转成你想要的格式
我下载版本是:jave-1.0.2.zip
转flv:
public static void main(String[] args) { File source = new File("E:\\test\\source.mp4"); File target = new File("E:\\test\\target.flv");// 转flv AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(64000)); audio.setChannels(new Integer(1)); audio.setSamplingRate(new Integer(22050)); VideoAttributes video = new VideoAttributes(); video.setCodec("flv");// 转flv video.setBitRate(new Integer(180000));// 180kb/s比特率 video.setFrameRate(new Integer(1));// 1f/s帧频,1是目前测试比较清楚的,越大越模糊 EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("flv");// 转flv attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); long beginTime = System.currentTimeMillis(); try { // 获取时长 MultimediaInfo m = encoder.getInfo(source); long sTime = m.getDuration(); long minute = sTime / 60000; long second = (sTime % 60000) / 1000; System.out.println("视频时长:" + minute + ":" + (second < 10 ? "0" + second : second)); System.out.println("获取时长花费时间是:" + (System.currentTimeMillis() - beginTime)); beginTime = System.currentTimeMillis(); encoder.encode(source, target, attrs); System.out.println("视频转码花费时间是:" + (System.currentTimeMillis() - beginTime)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InputFormatException e) { e.printStackTrace(); } catch (EncoderException e) { e.printStackTrace(); } }
转MP4:
public static void main(String[] args) { File source = new File("E:\\test\\source.avi"); File target = new File("E:\\test\\target.mp4");// 转MP4 AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(64000)); audio.setChannels(new Integer(1)); audio.setSamplingRate(new Integer(22050)); VideoAttributes video = new VideoAttributes(); video.setCodec("libxvid");// 转MP4 video.setBitRate(new Integer(180000));// 180kb/s比特率 video.setFrameRate(new Integer(1));// 1f/s帧频,1是目前测试比较清楚的,越大越模糊 EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mp4");// 转MP4 attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); long beginTime = System.currentTimeMillis(); try { // 获取时长 MultimediaInfo m = encoder.getInfo(source); System.out.println(m.getDuration()); System.out.println("获取时长花费时间是:" + (System.currentTimeMillis() - beginTime)); beginTime = System.currentTimeMillis(); encoder.encode(source, target, attrs); System.out.println("视频转码花费时间是:" + (System.currentTimeMillis() - beginTime)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InputFormatException e) { e.printStackTrace(); } catch (EncoderException e) { e.printStackTrace(); } }
获取某一帧作为png图片:
public static void main(String[] args) { File source = new File("E:\\test\\flex.avi"); File target = new File("E:\\test\\target.png");//转图片 VideoAttributes video = new VideoAttributes(); video.setCodec("png");//转图片 video.setSize(new VideoSize(600, 500)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("image2");//转图片 attrs.setOffset(3f);//设置偏移位置,即开始转码位置(3秒) attrs.setDuration(0.01f);//设置转码持续时间(1秒) attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); long beginTime = System.currentTimeMillis(); try { //获取时长 MultimediaInfo m = encoder.getInfo(source); System.out.println(m.getDuration()); System.out.println("获取时长花费时间是:" + (System.currentTimeMillis() - beginTime)); beginTime = System.currentTimeMillis(); encoder.encode(source, target, attrs); System.out.println("视频转码花费时间是:" + (System.currentTimeMillis() - beginTime)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InputFormatException e) { e.printStackTrace(); } catch (EncoderException e) { e.printStackTrace(); } }
从官网下载的帮助文档中可以看到有很多编码格式,以此类推,相信可以转成你想要的格式