PS: 建议大家在官网下载最新的资源
其他格式转FLV格式,可以用Java调用ffmpeg和memcoder实现
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:网易视频 比较清晰 50min -》170mb 的FLV格式
/** * 功能:将任意格式的视频转化为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;
}
/**
* 转换过程 :先检查文件类型,在决定调用 processFlv还是processAVI
*
* @param inputFile
* @param outputFile
* @return
*/
private void process(String inputFile, String outputFile) {
int type = checkContentType(inputFile);
if (type == 0) {
processFLV(inputFile, outputFile);// 直接将文件转为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)转换为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)转换为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进程 的输出流和错误流 因为有些本机平台仅针对标准输入和输出流提供有限的缓冲区大小,
* 如果读写子进程的输出流或输入流迅速出现失败,则可能导致子进程阻塞,甚至产生死锁。
*/
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;
}
}