日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java flv转mp3_用java程序调用ffmpeg执行视频文件格式转换flv

發(fā)布時間:2024/3/26 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java flv转mp3_用java程序调用ffmpeg执行视频文件格式转换flv 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

用java小例題說明更直觀:(可以直接編譯運行)

環(huán)境我在windows平臺下測試的。。。

需要在e:/下有ffmpeg.exe;mencoder.exe;drv43260.dll;pncrt.dll共4個文件。

如何得到這4個文件參考文章http://blog.sina.com.cn/u/4a424eca010005kb

還要在e:/input下放各種文件名為a的以下各種視頻文件;還要e:/output;java程序執(zhí)行后能得到一個a.flv的已轉(zhuǎn)換的文件。

ffmpeg.exe能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)

對ffmpeg.exe無法解析的文件格式(wmv9,rm,rmvb等),

可以先用別的工具(mencoder)轉(zhuǎn)換為avi(ffmpeg能解析的)格式;

mencoder.exe;drv43260.dll;pncrt.dll這3個文件是為文件格式(wmv9,rm,rmvb等)

轉(zhuǎn)換為avi(ffmpeg能解析的)格式準(zhǔn)備的;再把轉(zhuǎn)換好的avi文件再用ffmpeg.exe轉(zhuǎn)換成flv格式的視頻文件。。。

package com.jh.test;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class ConvertVideo {

private static String inputPath = "";

private static String outputPath = "";

private static String ffmpegPath = "";

public static void main(String args[]) throws IOException {

getPath();

if (!checkfile(inputPath)) {

System.out.println(inputPath + " is not file");

return;

}

if (process()) {

System.out.println("ok");

}

}

public static void getPath(){

// 先獲取當(dāng)前項目路徑,在獲得源文件、目標(biāo)文件、轉(zhuǎn)換器的路徑

File diretory = new File("");

try {

String currPath = diretory.getAbsolutePath();

//視頻的地址

inputPath = "C:\\平行四邊形面積.flv";

//視頻轉(zhuǎn)完格式存放地址

outputPath = "E:\\mp4\\";

//轉(zhuǎn)換視頻的插件位置

ffmpegPath = "G:\\工具類\\ffmpeg-20171225-be2da4c-win64-static\\ffmpeg-20171225-be2da4c-win64-static\\bin\\";

System.out.println(currPath);

}

catch (Exception e) {

System.out.println("getPath出錯");

}

}

public static boolean process() {

int type = checkContentType();

boolean status = false;

System.out.println("直接轉(zhuǎn)成mp4格式");

status = processMp4(inputPath);// 直接轉(zhuǎn)成mp4格式

return status;

}

private static int checkContentType() {

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 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;

}

private static boolean checkfile(String path) {

File file = new File(path);

if (!file.isFile()) {

return false;

}

return true;

}

// 對ffmpeg無法解析的文件格式(wmv9,rm,rmvb等), 可以先用別的工具(mencoder)轉(zhuǎn)換為avi(ffmpeg能解析的)格式.

private static String processAVI(int type) {

Listcommend = new ArrayList();

commend.add(ffmpegPath + "mencoder");

commend.add(inputPath);

commend.add("-oac");

commend.add("lavc");

commend.add("-lavcopts");

commend.add("acodec=mp3:abitrate=64");

commend.add("-ovc");

commend.add("xvid");

commend.add("-xvidencopts");

commend.add("bitrate=600");

commend.add("-of");

commend.add("mp4");

commend.add("-o");

commend.add(outputPath + "a.AVI");

try {

ProcessBuilder builder = new ProcessBuilder();

Process process = builder.command(commend).redirectErrorStream(true).start();

new PrintStream(process.getInputStream());

new PrintStream(process.getErrorStream());

process.waitFor();

return outputPath + "a.AVI";

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)

private static boolean processFlv(String oldfilepath) {

if (!checkfile(inputPath)) {

System.out.println(oldfilepath + " is not file");

return false;

}

Listcommand = new ArrayList();

command.add(ffmpegPath + "ffmpeg");

command.add("-i");

command.add(oldfilepath);

command.add("-ab");

command.add("56");

command.add("-ar");

command.add("22050");

command.add("-qscale");

command.add("8");

command.add("-r");

command.add("15");

command.add("-s");

command.add("600x500");

command.add(outputPath + "a.flv");

try {

// 方案1

// Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath

// + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "

// + outputPath + "a.flv");

// 方案2

Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();

new PrintStream(videoProcess.getErrorStream()).start();

new PrintStream(videoProcess.getInputStream()).start();

videoProcess.waitFor();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

private static boolean processMp4(String oldfilepath) {

if (!checkfile(inputPath)) {

System.out.println(oldfilepath + " is not file");

return false;

}

Listcommand = new ArrayList();

command.add(ffmpegPath + "ffmpeg");

command.add("-i");

command.add(oldfilepath);

command.add("-c:v");

command.add("libx264");

command.add("-mbd");

command.add("0");

command.add("-c:a");

command.add("aac");

command.add("-strict");

command.add("-2");

command.add("-pix_fmt");

command.add("yuv420p");

command.add("-movflags");

command.add("faststart");

command.add(outputPath + "a.mp4");

try {

// 方案1

// Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath

// + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "

// + outputPath + "a.flv");

// 方案2

Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();

new PrintStream(videoProcess.getErrorStream()).start();

new PrintStream(videoProcess.getInputStream()).start();

videoProcess.waitFor();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

}

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)

System.out.print((char)_ch);

else break;

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

總結(jié)

以上是生活随笔為你收集整理的java flv转mp3_用java程序调用ffmpeg执行视频文件格式转换flv的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。