最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻

 更新時(shí)間:2018年12月15日 15:07:12   作者:zhengdesheng19930211  
這篇文章主要為大家詳細(xì)介紹了java調(diào)用ffmpeg實(shí)現(xiàn)轉(zhuǎn)換視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近由于項(xiàng)目需要把不同格式的視頻轉(zhuǎn)換為ts流,故研究了一下ffmpeg。在網(wǎng)上找了很多資料,主要參考了Java+Windows+ffmpeg實(shí)現(xiàn)視頻轉(zhuǎn)換功能。

期間也加了幾個(gè)qq群,咨詢了各大高手,其中在代碼中關(guān)于ffmpeg的命令就是來(lái)自其中一個(gè)qq群里面的大神。

下載相關(guān)文件

ffmpeg地址,我下載是windows 64位static版本。

xuggler下載地址

下面的代碼我上傳到了github,需要的可以下載下來(lái)看看。

步驟:

1.研究java如何調(diào)用外部程序
2.研究ffmpeg轉(zhuǎn)換視頻格式的命令
3.利用xuggle獲取ffmpeg解析的ts流的時(shí)長(zhǎng)、分辨率以及文件大小。

下面直接上代碼:

1.ffmpeg轉(zhuǎn)換實(shí)現(xiàn)

package vedio.ffmpeg;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
public class FfmpegUtil {
 
public static Boolean ffmpeg(StringffmpegPath, String inputPath, String outputPath) throwsFFmpegException{
 
if (!checkfile(inputPath)) {
throw newFFmpegException("文件格式不合法");
}
 
int type =checkContentType(inputPath);
List command = getFfmpegCommand(type,ffmpegPath, inputPath, outputPath);
if (null != command &&command.size() > 0) {
return process(command);
 
}
return false;
}
 
private static int checkContentType(StringinputPath) {
String type =inputPath.substring(inputPath.lastIndexOf(".") + 1,inputPath.length()).toLowerCase();
//ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if (type.equals("avi")) {
return 1;
} else if (type.equals("mpg")){
return 1;
} else if (type.equals("wmv")){
return 1;
} else if (type.equals("3gp")){
return 1;
} else if (type.equals("mov")){
return 1;
} else if (type.equals("mp4")){
return 1;
} else if(type.equals("mkv")){
return 1;
}else if (type.equals("asf")){
return 0;
} else if (type.equals("flv")){
return 0;
}else if (type.equals("rm")){
return 0;
} else if (type.equals("rmvb")){
return 1;
}
return 9;
}
 
private static boolean checkfile(Stringpath) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
 
private static boolean process(Listcommand) throws FFmpegException{
 
try {
 
if (null == command || command.size() ==0)
return false;
Process videoProcess = newProcessBuilder(command).redirectErrorStream(true).start();
 
newPrintStream(videoProcess.getErrorStream()).start();
 
newPrintStream(videoProcess.getInputStream()).start();
 
int exitcode =videoProcess.waitFor();
 
if (exitcode == 1) {
return false;
}
return true;
} catch (Exception e) {
throw new FFmpegException("file uploadfailed",e);
}
 
}
 
private static List getFfmpegCommand(inttype, String ffmpegPath, String oldfilepath, String outputPath)throws FFmpegException {
List command = newArrayList();
if (type == 1) {
command.add(ffmpegPath +"\\ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-c:v");
command.add("libx264");
command.add("-x264opts");
command.add("force-cfr=1");
command.add("-c:a");
command.add("mp2");
command.add("-b:a");
command.add("256k");
command.add("-vsync");
command.add("cfr");
command.add("-f");
command.add("mpegts");
command.add(outputPath);
} else if(type==0){
command.add(ffmpegPath +"\\ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-c:v");
command.add("libx264");
command.add("-x264opts");
command.add("force-cfr=1");
command.add("-vsync");
command.add("cfr");
command.add("-vf");
command.add("idet,yadif=deint=interlaced");
command.add("-filter_complex");
command.add("aresample=async=1000");
command.add("-c:a");
command.add("libmp3lame");
command.add("-b:a");
command.add("192k");
command.add("-pix_fmt");
command.add("yuv420p");
command.add("-f");
command.add("mpegts");
command.add(outputPath);
}else{
throw newFFmpegException("不支持當(dāng)前上傳的文件格式");
}
return command;
}
}
 
class PrintStream extends Thread{
java.io.InputStream __is =null;
 
public PrintStream(java.io.InputStream is){
__is = is;
}
 
public void run() {
try {
while (this != null) {
int _ch = __is.read();
if (_ch == -1) {
break;
} else {
System.out.print((char) _ch);
}
 
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

2.調(diào)用測(cè)試類(lèi)

package vedio.ffmpeg;
 
public class ConvertVedio {
public static void convertVedio(StringinputPath){
String ffmpegPath =getFfmpegPath();
String outputPath =getOutputPath(inputPath);
try {
FfmpegUtil.ffmpeg(ffmpegPath, inputPath,outputPath);
} catch (FFmpegException e) {
e.printStackTrace();
}
 
}
 
private static String getFfmpegPath(){
return "ffmpeg";
}
 
private static String getOutputPath(StringinputPath) {
return inputPath.substring(0,inputPath.lastIndexOf(".")).toLowerCase() + ".ts";
}
}

3.自定義的異常類(lèi)

package vedio.ffmpeg;
 
public class FFmpegException extendsException {
 
private static final long serialVersionUID= 1L;
 
public FFmpegException() {
super();
}
 
public FFmpegException(String message){
super(message);
}
 
public FFmpegException(Throwable cause){
super(cause);
}
 
public FFmpegException(String message,Throwable cause) {
super(message, cause);
}
}

4.獲取ts流的時(shí)長(zhǎng)、大小以及分辨率(用到了Xuggle,需要下載對(duì)應(yīng)jar包)

importcom.xuggle.xuggler.ICodec;
importcom.xuggle.xuggler.IContainer;
importcom.xuggle.xuggler.IStream;
importcom.xuggle.xuggler.IStreamCoder;
 
*/
 public static void getVedioInfo(String filename){
 
 
   // first we create a Xuggler containerobject
   IContainer container =IContainer.make();
 
   // we attempt to open up thecontainer
   int result = container.open(filename,IContainer.Type.READ, null);
 
   // check if the operation wassuccessful
   if (result<0)
    return;
   
   // query how many streams the call to openfound
   int numStreams =container.getNumStreams();
   // query for the total duration
   long duration =container.getDuration();
   // query for the file size
   long fileSize =container.getFileSize();
   long secondDuration =duration/1000000;
   
   System.out.println("時(shí)長(zhǎng):"+secondDuration+"秒");
   System.out.println("文件大小:"+fileSize+"M");
  
  
   for (int i=0; i
    IStreamstream = container.getStream(i);
    IStreamCoder coder = stream.getStreamCoder();
    if(coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO){
    System.out.println("視頻寬度:"+coder.getWidth());
     System.out.println("視頻高度:"+coder.getHeight());
    }
   }
 
 }

以上就是在開(kāi)發(fā)過(guò)程中做的全部,希望大家多多學(xué)習(xí),交流!

相關(guān)文章

  • apache commons工具集代碼詳解

    apache commons工具集代碼詳解

    這篇文章主要介紹了apache commons工具集代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • Java計(jì)算器核心算法代碼實(shí)現(xiàn)

    Java計(jì)算器核心算法代碼實(shí)現(xiàn)

    今天小編就為大家分享一篇關(guān)于Java計(jì)算器核心算法代碼實(shí)現(xiàn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 淺談Thread.sleep(0)到底有什么用

    淺談Thread.sleep(0)到底有什么用

    為什么要用sleep,主要是為了暫停當(dāng)前線程,把cpu片段讓出給其他線程,減緩當(dāng)前線程的執(zhí)行,本文主要介紹了Thread.sleep(0)到底有什么用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java 8 中 Map 騷操作之 merge() 的使用方法

    Java 8 中 Map 騷操作之 merge() 的使用方法

    本文簡(jiǎn)單介紹了一下Map.merge()的方法,除此之外,Java 8 中的HashMap實(shí)現(xiàn)方法使用了TreeNode和 紅黑樹(shù),原理很相似,今天通過(guò)本文給大家介紹Java 8 中 Map 騷操作之 merge() 的用法 ,需要的朋友參考下吧
    2021-07-07
  • Maven配置阿里云倉(cāng)庫(kù)/國(guó)內(nèi)鏡像的詳細(xì)步驟

    Maven配置阿里云倉(cāng)庫(kù)/國(guó)內(nèi)鏡像的詳細(xì)步驟

    在國(guó)內(nèi)使用Maven時(shí),很多時(shí)候會(huì)遇到下載依賴較慢的問(wèn)題,主要是因?yàn)镸aven的默認(rèn)中央倉(cāng)庫(kù)位于國(guó)外,網(wǎng)絡(luò)延遲較高,為了解決這個(gè)問(wèn)題,我們可以配置國(guó)內(nèi)的Maven鏡像源,如阿里云提供的鏡像,在這篇博客中,我們將詳細(xì)介紹如何配置Maven使用阿里云倉(cāng)庫(kù),需要的朋友可以參考下
    2025-04-04
  • java圖片對(duì)比度調(diào)整示例代碼

    java圖片對(duì)比度調(diào)整示例代碼

    這篇文章主要給大家介紹了關(guān)于java圖片對(duì)比度調(diào)整的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Java中的TreeSet集合詳解

    Java中的TreeSet集合詳解

    這篇文章主要介紹了Java中的TreeSet集合詳解,TreeSet 是一個(gè) 有序集合,它擴(kuò)展了 AbstractSet 類(lèi)并實(shí)現(xiàn)了 NavigableSet 接口,作為自平衡二叉搜索樹(shù),二叉樹(shù)的每個(gè)節(jié)點(diǎn)包括一個(gè)額外的位,用于識(shí)別紅色或黑色的節(jié)點(diǎn)的顏色,需要的朋友可以參考下
    2023-09-09
  • 跨域解決方案Jsonp原理解析

    跨域解決方案Jsonp原理解析

    這篇文章主要介紹了跨域解決方案Jsonp原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • eclipse下搭建hibernate5.0環(huán)境的步驟(圖文)

    eclipse下搭建hibernate5.0環(huán)境的步驟(圖文)

    這篇文章主要介紹了eclipse下搭建hibernate5.0環(huán)境的步驟(圖文),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Java實(shí)現(xiàn)推箱子游戲

    Java實(shí)現(xiàn)推箱子游戲

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)推箱子游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05

最新評(píng)論

页游| 东台市| 银川市| 舞钢市| 靖西县| 西宁市| 武清区| 堆龙德庆县| 伽师县| 中阳县| 沙雅县| 库尔勒市| 南康市| 孟州市| 夏邑县| 永新县| 芦溪县| 烟台市| 福州市| 石家庄市| 临西县| 陇西县| 文水县| 江津市| 鹰潭市| 大理市| 木里| 清远市| 樟树市| 安顺市| 昌图县| 新蔡县| 石屏县| 清远市| 化隆| 安泽县| 桂林市| 阿勒泰市| 偃师市| 怀化市| 宜州市|