Java调用ffmepg+mencoder视频格式转换(*)
生活随笔
收集整理的這篇文章主要介紹了
Java调用ffmepg+mencoder视频格式转换(*)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
PS: 建議大家在官網(wǎng)下載最新的資源
?
其他格式轉(zhuǎn)FLV格式,可以用Java調(diào)用ffmpeg和memcoder實(shí)現(xiàn)
ffmepg:
D:\ffmpeg\bin\ffmpeg.exe -i E:\1.mp4 -ab 64 -acodec mp3 -ac 2 -ar 22050 -b 230 -r 24 -y E:\111.flv
?
Mencoder:
D:\WisMencoder\mencoder.exe E:\1.rmvb -oac mp3lame -lameopts preset=64 -ovc xvid -xvidencopts bitrate=600 -of avi -o E:\1.avi
?
ps:網(wǎng)易視頻 比較清晰 50min -》170mb 的FLV格式
?
/*** 功能:將任意格式的視頻轉(zhuǎn)化為flv格式,有利于在線視頻播放* ps: ffmpeg 能解析的格式:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等* mencoder 解析剩下的格式:wmv9,rm,rmvb * time:2013.12.11*/ package softflag.core.util;import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import softflag.index.IndexService;
public class VideoConverter {
?? ?public synchronized void convert(String inputFile, String outputFile) {
?? ??? ?process(inputFile, outputFile);
?? ?}
?? ?private static String getAviFilePath(String filePath) {
?? ??? ?String path = filePath.substring(0, filePath.lastIndexOf(".")) + ".avi";
?? ??? ?return path;
?? ?}
?? ?/**
?? ? * 轉(zhuǎn)換過程 :先檢查文件類型,在決定調(diào)用 processFlv還是processAVI
?? ? *
?? ? * @param inputFile
?? ? * @param outputFile
?? ? * @return
?? ? */
?? ?private void process(String inputFile, String outputFile) {
?? ??? ?int type = checkContentType(inputFile);
?? ??? ?if (type == 0) {
?? ??? ??? ?processFLV(inputFile, outputFile);// 直接將文件轉(zhuǎn)為flv文件
?? ??? ?} else if (type == 1) {
?? ??? ??? ?processAVI(type, inputFile,outputFile);
?? ??? ?}
?? ?}
?? ?/**
?? ? * 檢查視頻類型
?? ? *
?? ? * @param inputFile
?? ? * @return ffmpeg 能解析返回0,不能解析返回1
?? ? */
?? ?private static int checkContentType(String inputFile) {
?? ??? ?String type = inputFile.substring(inputFile.lastIndexOf(".") + 1,
?? ??? ??? ??? ?inputFile.length()).toLowerCase();
?? ??? ?// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
?? ??? ?if (type.equals("avi")) {
?? ??? ??? ?return 0;
?? ??? ?} else if (type.equals("mpg")) {
?? ??? ??? ?return 0;
?? ??? ?} else if (type.equals("wmv")) {
?? ??? ??? ?return 0;
?? ??? ?} else if (type.equals("3gp")) {
?? ??? ??? ?return 0;
?? ??? ?} else if (type.equals("mov")) {
?? ??? ??? ?return 0;
?? ??? ?} else if (type.equals("mp4")) {
?? ??? ??? ?return 0;
?? ??? ?} else if (type.equals("asf")) {
?? ??? ??? ?return 0;
?? ??? ?} else if (type.equals("asx")) {
?? ??? ??? ?return 0;
?? ??? ?} else if (type.equals("flv")) {
?? ??? ??? ?return 0;
?? ??? ?}
?? ??? ?// 對ffmpeg無法解析的文件格式(wmv9,rm,rmvb等),
?? ??? ?// 可以先用別的工具(mencoder)轉(zhuǎn)換為avi(ffmpeg能解析的)格式.
?? ??? ?else if (type.equals("wmv9")) {
?? ??? ??? ?return 1;
?? ??? ?} else if (type.equals("rm")) {
?? ??? ??? ?return 1;
?? ??? ?} else if (type.equals("rmvb")) {
?? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?return 9;
?? ?}
?? ?/**
?? ? * ffmepg: 能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
?? ? *
?? ? * @param inputFile
?? ? * @param outputFile
?? ? * @return
?? ? */
?? ?private void processFLV(String inputFile, String outputFile) {
?? ??? ?try {
?? ??? ??? ?try {
?? ??? ??? ??? ?List<String> commend = new java.util.ArrayList<String>();
?? ??? ??? ??? ?// 低精度
?? ??? ??? ??? ?commend.add(IndexService.getConfigValue("check_multi_ffmpegPath"));
?? ??? ??? ??? ?commend.add("-i");
?? ??? ??? ??? ?commend.add(inputFile);
?? ??? ??? ??? ?commend.add("-ab");
?? ??? ??? ??? ?commend.add("64");
?? ??? ??? ??? ?commend.add("-acodec");
?? ??? ??? ??? ?commend.add("mp3");
?? ??? ??? ??? ?commend.add("-ac");
?? ??? ??? ??? ?commend.add("2");
?? ??? ??? ??? ?commend.add("-ar");
?? ??? ??? ??? ?commend.add("22050");
?? ??? ??? ??? ?commend.add("-b");
?? ??? ??? ??? ?commend.add("230");
?? ??? ??? ??? ?commend.add("-r");
?? ??? ??? ??? ?commend.add("24");
?? ??? ??? ??? ?commend.add("-y");
?? ??? ??? ??? ?commend.add(outputFile);
?? ??? ??? ??? ?StringBuffer test = new StringBuffer();
?? ??? ??? ??? ?for (int i = 0; i < commend.size(); i++)
?? ??? ??? ??? ??? ?test.append(commend.get(i) + " ");
//?? ??? ??? ??? ??? ??? ??? ?System.out.println(test);
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?ProcessBuilder builder = new ProcessBuilder();
?? ??? ??? ??? ??? ?builder.command(commend);
?? ??? ??? ??? ??? ?Process p = builder.start();
?? ??? ??? ??? ??? ?doWaitFor(p);
?? ??? ??? ??? ??? ?p.destroy();
?? ??? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (Exception e) {
?? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
?? ?/**
?? ? * Mencoder: 對ffmpeg無法解析的文件格式(wmv9,rm,rmvb等),
?? ? * 可以先用別的工具(mencoder)轉(zhuǎn)換為avi(ffmpeg能解析的)格式.
?? ? *
?? ? * @param type
?? ? * @param inputFile
?? ? * @return
?? ? */
?? ?private void processAVI(int type, String inputFile,String outputFile) {
?? ??? ?String aviPath = getAviFilePath(inputFile);
?? ??? ?String outputFilePath = outputFile;
?? ??? ?File file = new File(aviPath);
?? ??? ?if (file.exists())
?? ??? ??? ?file.delete();
?? ??? ?List<String> commend = new java.util.ArrayList<String>();
?? ??? ?commend.add(IndexService.getConfigValue("check_multi_mencoderPath"));
?? ??? ?commend.add(inputFile);
?? ??? ?commend.add("-oac");
?? ??? ?commend.add("mp3lame");
?? ??? ?commend.add("-lameopts");
?? ??? ?commend.add("preset=64");
?? ??? ?commend.add("-ovc");
?? ??? ?commend.add("xvid");
?? ??? ?commend.add("-xvidencopts");
?? ??? ?commend.add("bitrate=600");
?? ??? ?commend.add("-of");
?? ??? ?commend.add("avi");
?? ??? ?commend.add("-o");
?? ??? ?commend.add(aviPath);
?? ??? ?StringBuffer test = new StringBuffer();
?? ??? ?for (int i = 0; i < commend.size(); i++)
?? ??? ??? ?test.append(commend.get(i) + " ");
//?? ??? ??? ?System.out.println(test);
?? ??? ?try {
?? ??? ??? ?ProcessBuilder builder = new ProcessBuilder();
?? ??? ??? ?builder.command(commend);
?? ??? ??? ?Process p = builder.start();
?? ??? ??? ?/**
?? ??? ??? ? * 清空Mencoder進(jìn)程 的輸出流和錯(cuò)誤流 因?yàn)橛行┍緳C(jī)平臺僅針對標(biāo)準(zhǔn)輸入和輸出流提供有限的緩沖區(qū)大小,
?? ??? ??? ? * 如果讀寫子進(jìn)程的輸出流或輸入流迅速出現(xiàn)失敗,則可能導(dǎo)致子進(jìn)程阻塞,甚至產(chǎn)生死鎖。
?? ??? ??? ? */
?? ??? ??? ?final InputStream is1 = p.getInputStream();
?? ??? ??? ?final InputStream is2 = p.getErrorStream();
?? ??? ??? ?new Thread() {
?? ??? ??? ??? ?public void run() {
?? ??? ??? ??? ??? ?BufferedReader br = new BufferedReader(
?? ??? ??? ??? ??? ??? ??? ?new InputStreamReader(is1));
?? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ?String lineB = null;
?? ??? ??? ??? ??? ??? ?while ((lineB = br.readLine()) != null) {
?? ??? ??? ??? ??? ??? ??? ?if (lineB != null)
?? ??? ??? ??? ??? ??? ??? ??? ?System.out.println(lineB);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?is1.close();
?? ??? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}.start();
?? ??? ??? ?new Thread() {
?? ??? ??? ??? ?public void run() {
?? ??? ??? ??? ??? ?BufferedReader br2 = new BufferedReader(
?? ??? ??? ??? ??? ??? ??? ?new InputStreamReader(is2));
?? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ?String lineC = null;
?? ??? ??? ??? ??? ??? ?while ((lineC = br2.readLine()) != null) {
?? ??? ??? ??? ??? ??? ??? ?if (lineC != null)
?? ??? ??? ??? ??? ??? ??? ??? ?System.out.println(lineC);
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?br2.close();
?? ??? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}.start();
?? ??? ??? ?doWaitFor(p);
?? ??? ??? ?p.destroy();
?? ??? ??? ?processFLV(aviPath,outputFilePath);
?? ??? ?} catch (Exception e) {
?? ??? ??? ?System.err.println(e);
?? ??? ?}
?? ?}
?? ?
?? ?public int doWaitFor(Process p) {
?? ??? ?InputStream in = null;
?? ??? ?InputStream err = null;
?? ??? ?int exitValue = -1; // returned to caller when p is finished
?? ??? ?try {
//?? ??? ??? ?System.out.println("comeing");
?? ??? ??? ?in = p.getInputStream();
?? ??? ??? ?err = p.getErrorStream();
?? ??? ??? ?boolean finished = false; // Set to true when p is finished
?? ??? ??? ?while (!finished) {
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?while (in.available() > 0) {
?? ??? ??? ??? ??? ??? ?Character c = new Character((char) in.read());
?? ??? ??? ??? ??? ??? ?System.out.print(c);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?while (err.available() > 0) {
?? ??? ??? ??? ??? ??? ?Character c = new Character((char) err.read());
?? ??? ??? ??? ??? ??? ?System.out.print(c);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?exitValue = p.exitValue();
?? ??? ??? ??? ??? ?finished = true;
?? ??? ??? ??? ?} catch (IllegalThreadStateException e) {
?? ??? ??? ??? ??? ?Thread.currentThread().sleep(500);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?} catch (Exception e) {
?? ??? ??? ?System.err.println("doWaitFor();: unexpected exception - "
?? ??? ??? ??? ??? ?+ e.getMessage());
?? ??? ?} finally {
?? ??? ??? ?try {
?? ??? ??? ??? ?if (in != null) {
?? ??? ??? ??? ??? ?in.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?System.out.println(e.getMessage());
?? ??? ??? ?}
?? ??? ??? ?if (err != null) {
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?err.close();
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?System.out.println(e.getMessage());
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return exitValue;
?? ?}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/coprince/p/3461664.html
總結(jié)
以上是生活随笔為你收集整理的Java调用ffmepg+mencoder视频格式转换(*)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UDT 最新源码分析(五) -- 网络数
- 下一篇: Mac 上Grapher基础入门教程