前端播放视频有声音没有画面
MP4编码格式主要有三种,分别是mpg4(xdiv),mpg4(xvid),avc(h264),只有h264才是公认的MP4标准编码,所以视频有声音没画面需要将视频转码成h264编码的标准格式才能实现视频播放
转码代码
package com.huang.controller.autoVideoModule;import ws.schild.jave.*;import java.io.File;/*** @Description 视频转码工具类* @Author huangyang* @date 2021年06月22日 16:02*/public class changeCode {public static void changeCodeUtil(String sourcepath,String targetpath){File source = new File(sourcepath);File target = new File(targetpath);AudioAttributes audio = new AudioAttributes();audio.setCodec("libmp3lame");audio.setBitRate(new Integer(800000));audio.setChannels(new Integer(1));VideoAttributes video=new VideoAttributes();video.setCodec("libx264");video.setBitRate(new Integer(3200000));video.setFrameRate(new Integer(15));EncodingAttributes attr=new EncodingAttributes();attr.setFormat("mp4");attr.setAudioAttributes(audio);attr.setVideoAttributes(video);Encoder encoder=new Encoder();MultimediaObject multimediaObject = new MultimediaObject(source);try{encoder.encode(multimediaObject, target, attr);}catch (IllegalArgumentException e){e.printStackTrace();}catch (InputFormatException e){e.printStackTrace();}catch (EncoderException e){e.printStackTrace();}}}
**注意:**在写代码前首先要在pom.xml中导入以下依赖
<dependency><groupId>ws.schild</groupId><artifactId>jave-core</artifactId><version>2.4.5</version></dependency><dependency><groupId>ws.schild</groupId><artifactId>jave-native-win64</artifactId><version>2.4.5</version></dependency>