一、产生的原因
前端Video标签的局限性,限制视频播放,在界面显示时出现有声音无画面。如下所示:
Video支持视频格式如下:
二、解决方案
1、上传视频前,利用格式工厂编码video对应的格式。
2、上传视频时,Java后台处理视频编码格式。
现在针对Java后台处理过程进行讲解:
首先引入maven包:
<!-- 用于视频转码-->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>2.7.3</version>
</dependency>
相关代码:
import ws.schild.jave.*;
import java.io.File;
/**
* @author allen小哥 2020/1/21 13:55
*
* 用于视频转码,更好支持前端video标签播放视频
*
*/
public class VideoEncoderUtil {
public static void main(String[] args) {
String source = "D:\\material\\VIDEO\\视频2.mp4";
File file = new File(source);
String target ="D:\\个人素材\\视频\\89898.mp4";
try {
transform(file,target);
}catch (Exception e){
e.printStackTrace();
}
}
/**
*
* @param source 源文件
* @param destPath 目标文件
* @return
*/
public static File transform(File source , String destPath) {
File target = new File(destPath);
EncodingAttributes attrs = getEncodingAttributes("aac","libx264","mp4");
transferEncoder(source, target, attrs);
return target;
}
/**
*
* @param source 源文件
* @param destPath 目标文件
* @param audioCodec 音频格式
* @param videoCodec 视频编码格式
* @param format 格式
* @return
*/
public static File transform(File source , String destPath,String audioCodec,String videoCodec,String format) {
File target = new File(destPath);
EncodingAttributes attrs = getEncodingAttributes(audioCodec,videoCodec,format);
transferEncoder(source, target, attrs);
return target;
}
/**
*
* @param source 源文件
* @param target 目标文件
* @param attrs 相关配置信息
*/
private static void transferEncoder(File source, File target, EncodingAttributes attrs) {
Encoder encoder = new Encoder();
try {
encoder.encode(new MultimediaObject(source), target, attrs);
} catch (Exception e) {
System.out.println("视频转码失败:" + e);
}
}
/**
* 转码相关配置信息
* @param audioCodec 音频格式
* @param videoCodec 视频编码格式
* @param format 格式
* @return
*/
private static EncodingAttributes getEncodingAttributes(String audioCodec,String videoCodec,String format) {
AudioAttributes audio = new AudioAttributes();
//audio.setCodec("libmp3lame"); // mp3
audio.setCodec(audioCodec);
audio.setBitRate(new Integer(36000));
audio.setChannels(new Integer(2)); //1 mono 单声道 2 stereo 立体声
audio.setSamplingRate(new Integer(44100));
VideoAttributes video = new VideoAttributes();
video.setCodec(videoCodec);
video.setBitRate(new Integer(160000));
video.setFrameRate(new Integer(15));
video.setSize(new VideoSize(400, 300));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat(format);
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
return attrs;
}
}
三、相关文档
https://blog.csdn.net/qq_34806812/article/details/81361817
https://blog.csdn.net/luohai859/article/details/52496054
https://segmentfault.com/q/1010000014656374
https://github.com/a-schild/jave2
http://www.sauronsoftware.it/projects/jave/manual.php
四、相关问题
A、Unknown encoder 'libmp3lame4'
audio.setCodec("libmp3lame4"); // 指没有这个音频编码器
调整:audio.setCodec("aac");