1. 程式人生 > 程式設計 >使用開源專案JAVAE2 進行視訊格式轉換

使用開源專案JAVAE2 進行視訊格式轉換

使用開源專案JAVAE 進行視訊格式轉換

JAVAE簡介:

JAVE (Java音訊視訊編碼器)庫是ffmpeg專案的Java包裝器。開發人員可以利用JAVE2將音訊和視訊檔案從一種格式轉換為另一種格式。在示例可以轉換成一個AVI檔案MG,您可以更改一個DivX視訊(youtube) Flash FLV,您可以轉換WAV音訊檔案到MP3和Ogg Vorbis,您可以分離和轉換音訊和視訊跟蹤程式碼,您可以調整視訊,改變他們的大小和比例等。JAVE2還支援許多其他格式、容器和操作。

官網地址:https://www.mvnjar.com/ws.schild/jave-all-deps/2.4.2/detail.html

github地址:https://github.com/a-schild/jave2/tree/master/jave-example

maven地址:https://mvnrepository.com/artifact/ws.schild/jave-all-deps

快速上手

導包

<dependency>
 <groupId>ws.schild</groupId>
 <artifactId>jave-all-deps</artifactId>
 <version>2.4.2</version>
</dependency>

工具類

package com.example.javae2.util;


import cn.hutool.core.util.ObjectUtil;
import ws.schild.jave.*;

import java.io.File;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class VideoUtils {



 private static Map<String,Integer> sizeBitRateMap;

 static {
  sizeBitRateMap = new HashMap<>();
  sizeBitRateMap.put("1920*1080",4992);
  sizeBitRateMap.put("1280*720",2496);
  sizeBitRateMap.put("1024*576",1856);
  sizeBitRateMap.put("840*480",1216);
  sizeBitRateMap.put("768*432",1088);
  sizeBitRateMap.put("640*360",896);
  sizeBitRateMap.put("424*240",576);
 }


 public static void main(String[] args) {
  VideoUtils videoUtils = new VideoUtils();
  videoUtils.convertVideoToMP4(new File("C:\\temp\\javae2\\0001.嗶哩嗶哩-頸椎操[流暢版].flv"),"C:\\temp\\javae2\\0001.嗶哩嗶哩-頸椎操[流暢版].mp4");

  //videoUtils.getVideoInfoAndGenerateThumbnail(new File("C:\\\\temp\\\\javae2\\\\0001.嗶哩嗶哩-頸椎操[流暢版].mp4"),"C:\\\\temp\\\\javae2\\\\0001.嗶哩嗶哩-頸椎操[流暢版]..jpg");


 }

 /**
  * 擷取視訊的一針作為封面圖
  *
  * @param file   視訊檔案
  * @param thumbnailPath 擷取圖片儲存路徑
  * @return
  */
 public void getVideoInfoAndGenerateThumbnail(File file,String thumbnailPath) {
  MultimediaObject multimediaObject = new MultimediaObject(file);
  try {
   MultimediaInfo info = multimediaObject.getInfo();
   VideoInfo videoInfo = info.getVideo();
   logger.info("獲取視訊時長:{}",info.getDuration() / 1000);
   if (ObjectUtil.isNotNull(videoInfo)) {
    VideoSize size = videoInfo.getSize();
    int width = size.getWidth();
    int height = size.getHeight();
    logger.info("視訊寬:{} 視訊高{}",width,height);
    logger.info("位元率:{}",videoInfo.getBitRate() / 1000);
    ScreenExtractor screenExtractor = new ScreenExtractor();
    File target = new File(thumbnailPath);
    //擷取視訊作為圖片儲存
    /*
    *第一個引數 視訊原始檔資訊類
    * 第二個引數 擷取的寬度
    * 第三個引數 擷取的高度
    * 第四個引數 擷取的是那一幀
    * 第五個引數是 擷取的圖片質量 1-31 數字越小質量越高
    *
    **/
    screenExtractor.renderOneImage(multimediaObject,size.getWidth(),size.getHeight(),3000,target,31);

   }
  } catch (EncoderException e) {
   e.printStackTrace();
  }

 }


 /**
  * @param source  原始檔
  * @param targetPath 轉碼後的路徑
  */
 public void convertVideoToMP4(File source,String targetPath) {
  MultimediaObject multimediaObject = new MultimediaObject(source);
  try {
   MultimediaInfo info = multimediaObject.getInfo();
   VideoInfo videoInfo = info.getVideo();
   VideoSize size = videoInfo.getSize();
   System.out.println("原視訊寬:" + size.getWidth());
   System.out.println("原視訊高:" + size.getHeight());
   System.out.println("原視訊位元率:" + videoInfo.getBitRate() / 1000);
   System.out.println("原視訊編碼:" + videoInfo.getDecoder());

   Integer bitRate = sizeBitRateMap.get(size.getWidth() + "*" + size.getHeight());
   VideoAttributes video = new VideoAttributes();
   //設定視訊編碼
   video.setCodec("h264");

   if (ObjectUtil.isNotNull(bitRate)) {
    //設定位元率
    video.setBitRate(bitRate * 1000);
   }
   File target = new File(targetPath);
   AudioAttributes audio = new AudioAttributes();
   //設定編碼器名稱
   audio.setCodec("aac");
   EncodingAttributes attrs = new EncodingAttributes();
   //設定轉換後的格式
   attrs.setFormat("mp4");
   attrs.setAudioAttributes(audio);
   attrs.setVideoAttributes(video);
   Encoder encoder = new Encoder();
   encoder.encode(multimediaObject,attrs);
   //花費毫秒數

   MultimediaObject multimediaObjectOfter = new MultimediaObject(Paths.get(targetPath).toFile());
   MultimediaInfo info1 = multimediaObjectOfter.getInfo();
   VideoInfo video1 = info1.getVideo();
   VideoSize size1 = video1.getSize();

   System.out.println("轉換後視訊寬:" + size1.getWidth());
   System.out.println("轉換後視訊高:" + size1.getHeight());
   System.out.println("轉換後視訊位元率:" + video1.getBitRate() / 1000);
   System.out.println("轉換後視訊編碼:" + video1.getDecoder());

  } catch (EncoderException e) {
   e.printStackTrace();
  }
 }



}

效果 (flv 轉MP4)

使用開源專案JAVAE2 進行視訊格式轉換

以上就是使用開源專案JAVAE 進行視訊格式轉換的詳細內容,更多關於Java 視訊格式轉換的資料請關注我們其它相關文章!