最近公司有个项目要求使用将视频文件用H5展现出来,但是视频文件是avi格式的,而使用h5的viedo标签只能展现mp4格式同时编码方式是H264的,那么问题来了需要转码.
找了好多资料,有的是电脑上按照ffmpeg这个东西,然后写命令转码。但是后面我又从gitee上找到一个开源的Jave2(https://github.com/a-schild/jave2)版本的,之前找到的是jave1版本的,地址(http://www.sauronsoftware.it/projects/jave/manual.php).
使用jave2需要选择对应得环境,同时要把依赖加进去,我本地是windows64位的,加载依赖
各个环境要选择好,不然会报错在转换的时候,
下面是我写的将avi转换成mp4的Java
/**
* Project Name:springboot-salary
* File Name:VideoConvertMain.java
* Package Name:com.sundy
* Date:2019年7月4日上午8:02:56
* Copyright (c) 2019, [email protected] All Rights Reserved.
*
*/
package com.sundy;
import java.io.File;
import ws.schild.jave.AudioAttributes;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.EncodingAttributes;
import ws.schild.jave.InputFormatException;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.VideoAttributes;
import ws.schild.jave.VideoSize;
/**
* ClassName:VideoConvertMain
* Date: 2019年7月4日 上午8:02:56
* @作者 sundy.wangkui
* @描述
* @版本 1.0.0
*
*/
public class VideoConvertMain {
public static void main(String[] args) throws Exception, EncoderException, Exception {
String sourcePath="D:\\avifile\\20190614142215.avi";
String targetPath="D:\\avifile\\test0010.mp4";
String demoPath="D:\\avifile\\demo.mp4";
File source=new File(sourcePath);
File target=new File(targetPath);
File demoPathFile=new File(demoPath);
/*
MultimediaObject instance = new MultimediaObject(demoPathFile);
MultimediaInfo result = instance.getInfo();
System.out.println(result.getVideo().getDecoder());
Encoder encoder = new Encoder();
String[] encoderResult = encoder.getVideoEncoders();
if(encoderResult!=null && encoderResult.length>0) {
for(int i=0;i System.out.println(encoderResult[i]); } }*/ test01(source,target); } public static void test01(File source,File target) throws Exception, InputFormatException, EncoderException { AudioAttributes audio = new AudioAttributes(); audio.setCodec("aac"); audio.setBitRate(new Integer(64000)); audio.setChannels(new Integer(2)); audio.setSamplingRate(new Integer(22050)); VideoAttributes video = new VideoAttributes(); video.setCodec("libx264"); video.setBitRate(new Integer(160000)); video.setFrameRate(new Integer(15)); video.setSize(new VideoSize(800, 600)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mp4"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); encoder.encode(new MultimediaObject(source), target, attrs); } } ,然后在网页html里面使用viedo加载就可以了,但是服务端配置nginx要注意了, 要加上标红的是哪个参数配置,不然浏览器里面打开播放不了。