java调用ffmpeg获取转码进度
Posted 向天再借500年V
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java调用ffmpeg获取转码进度相关的知识,希望对你有一定的参考价值。
java调用ffmpeg获取转码进度
package com.asia.tip;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaFFmpeg extends Thread
private boolean stoped = false;
public boolean isStoped()
return stoped;
public void setStoped(boolean stoped)
this.stoped = stoped;
public void run()
stoped = false;
openFFmepgExe();
stoped = true;
private void openFFmepgExe()
Runtime rn = Runtime.getRuntime();
Process p = null;
try
p = rn.exec(
"ffmpeg -progress D:/tmp/tmp.txt -i D:/tmp/少年.mp4 -threads 1 -vcodec libx264 -acodec aac -y D:/tmp/少年2.mp4");
BufferedInputStream in = new BufferedInputStream(p.getErrorStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;
System.out.println("开始转码");
while ((lineStr = inBr.readLine()) != null)
if (p.waitFor() != 0)
if (p.exitValue() == 1)
System.out.println("命令执行失败!");
catch (Exception e)
e.printStackTrace();
public static void main(String[] args)
System.out.println("hello.....");
JavaFFmpeg objTC = new JavaFFmpeg();
objTC.start();
String strFileContent;
while (!objTC.isStoped())
try
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();
strFileContent = readToString("D:/tmp/tmp.txt");
int nIdxFrameStart = strFileContent.lastIndexOf("frame=");
if (nIdxFrameStart >= 0)
int nIdxFrameEnd = strFileContent.indexOf((char) 0x0A, nIdxFrameStart);
if (nIdxFrameEnd > 0)
String strFrames = strFileContent.substring(nIdxFrameStart, nIdxFrameEnd);
System.out.println(strFrames);
// out_time=00:00:01.950476
int nIdxOutTimeStart = strFileContent.indexOf("out_time=", nIdxFrameEnd);
if (nIdxOutTimeStart > 0)
int nIdxOutTimeEnd = strFileContent.indexOf((char) 0x0A, nIdxOutTimeStart);
if (nIdxOutTimeEnd > 0)
String strOutTime = strFileContent.substring(nIdxOutTimeStart, nIdxOutTimeEnd);
System.out.println(strOutTime);
private static String readToString(String fileName)
String encoding = "UTF-8";
File file = new File(fileName);
Long fileLength = file.length();
byte[] fileContent = new byte[fileLength.intValue()];
try
FileInputStream in = new FileInputStream(file);
in.read(fileContent);
in.close();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
try
return new String(fileContent, encoding);
catch (Exception e)
System.out.println("The OS does not support " + encoding);
e.printStackTrace();
return null;
升级版-直接查错误流获取进度
package com.asia.tip;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class JavaFFmpegThread extends Thread
private String theFFmpegCmdLine = "";
public String getTheFFmpegCmdLine()
return theFFmpegCmdLine;
public void setTheFFmpegCmdLine(String theFFmpegCmdLine)
this.theFFmpegCmdLine = theFFmpegCmdLine;
public void run()
openFFmepgExe();
private void openFFmepgExe()
if (null == theFFmpegCmdLine || theFFmpegCmdLine.equals(""))
return;
Runtime rn = Runtime.getRuntime();
Process p = null;
try
p = rn.exec(theFFmpegCmdLine);
BufferedInputStream in = new BufferedInputStream(p.getErrorStream());
BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
String lineStr;
System.out.println("开始转码");
while ((lineStr = inBr.readLine()) != null)
int nTimeStartIndex = lineStr.indexOf("time=");
int nTimeEndIndex = lineStr.indexOf(" bitrate=");
if (nTimeStartIndex > 0 && nTimeEndIndex > 0)
System.out.println(lineStr.substring(nTimeStartIndex + 5, nTimeEndIndex));
if (p.waitFor() != 0)
if (p.exitValue() == 1)
System.out.println("命令执行失败!");
catch (Exception e)
e.printStackTrace();
public static void main(String[] args)
System.out.println("hello.....");
JavaFFmpegThread objTC = new JavaFFmpegThread();
objTC.setTheFFmpegCmdLine("ffmpeg -i D:/tmp/101.mp4 -threads 1 -vcodec libx264 -acodec aac -y D:/tmp/少年2.mp4");
objTC.start();
以上是关于java调用ffmpeg获取转码进度的主要内容,如果未能解决你的问题,请参考以下文章
Android 集成 FFmpeg 获取 FFmpeg 执行进度