使用java将flv文件格式视频转换为mp4文件格式,使用的工具为ffmpeg。
/**
* 转换视频格式时需要的自定义线程
*/
public 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);
System.out.print("");
else break;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public class ConvertVideoUtils(){
/**
* flv转换为mp4
* @param ffmpegPath ffmpeg安装路径
* @param inputPath 需要转换为文件(flv)
* @param outputPath 转换后得文件(mp4)
* @return
*/
private boolean processMp4(String ffmpegPath,String inputPath,String outputPath) {
List<String> command = new ArrayList<String>();
command.add(ffmpegPath + "ffmpeg");
command.add("-i");
command.add(inputPath);
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);
try {
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;
}
}
}