日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java针对不同视频格式进行转码

發布時間:2023/12/31 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java针对不同视频格式进行转码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、產生的原因

? 前端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"); // mp3audio.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");

總結

以上是生活随笔為你收集整理的Java针对不同视频格式进行转码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。