使用Java调用linux命令ffmpeg

时间:2022-09-02 19:46:06

http://lenj.blog.163.com/blog/static/127420388201411112545226/

使用Java调用linux命令ffmpeg  

2014-12-11 14:09:11|  分类: J2EE|举报|字号 订阅

下载LOFTER客户端
//win下获取ffmpeg路径  String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/ffmpeg.exe");// linux路径 String path="/usr/local/bin/ffmpeg";
String raw2flvCmd = "/usr/local/ffmpeg/bin/ffmpeg -i \"某视频文件下载URL\" -f flv /usr/userfile/ffmpeg/tempfile/1.flv";  
Runtime.getRuntime().exec(new String[]{"sh","-c",raw2flvCmd}); 
package com.nvvideo.util.cmd;    import java.io.BufferedReader;  import java.io.InputStreamReader;  import java.util.List;      /**  * CmdExecuter  * <p>Title: 命令执行器</p>  * <p>Description: 封装对操作系统命令行发送指令相关操作</p>  */  public class CmdExecuter {            /**      * 执行指令      * @param cmd 执行指令      * @param getter 指令返回处理接口,若为null则不处理输出      */      static public void exec( List<String> cmd, IStringGetter getter ){          try {              ProcessBuilder builder = new ProcessBuilder();                builder.command(cmd);              builder.redirectErrorStream(true);              Process proc = builder.start();              BufferedReader stdout = new BufferedReader(                      new InputStreamReader(proc.getInputStream()));              String line;              while ((line = stdout.readLine()) != null) {                  if( getter != null )                      getter.dealString(line);              }              proc.waitFor();                 stdout.close();          } catch (Exception e) {              e.printStackTrace();          }      }  
/**  * IStringGetter  * <p>Title: 字符串接收者接口</p>  * <p>Description: </p> */  public interface IStringGetter {      /**      * 处理字符串      * @param str      */      public void dealString( String str );  }
import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;
import com.nvvideo.util.cmd.CmdExecuter;import com.nvvideo.util.cmd.IStringGetter;

public class FFMpegUtil implements IStringGetter {
private long runtime = 0;private String ffmpegUri;// ffmpeg地址private String originFileUri; // 视频源文件地址
private enum FFMpegUtilStatus {Empty, CheckingFile, GettingRuntime};
private FFMpegUtilStatus status = FFMpegUtilStatus.Empty;
/** * 构造函数 *  * @param ffmpegUri *            ffmpeg的全路径 如e:/ffmpeg/ffmpeg.exe 或 /usr/local/bin/ffmpeg * @param originFileUri *            所操作视频文件的全路径 如e:/upload/temp/test.wmv */public FFMpegUtil(String ffmpegUri, String originFileUri) {this.ffmpegUri = ffmpegUri;this.originFileUri = originFileUri;}
/** * 获取视频时长 *  * @return */public long getRuntime() {runtime = 0;status = FFMpegUtilStatus.GettingRuntime;System.out.println(status);cmd.clear();cmd.add(ffmpegUri);cmd.add("-i");cmd.add(originFileUri);CmdExecuter.exec(cmd, this);return runtime;}
/** * 检测文件是否是支持的格式 将检测视频文件本身,而不是扩展名 *  * @return */public boolean isSupported() {isSupported = false;status = FFMpegUtilStatus.CheckingFile;cmd.clear();cmd.add(ffmpegUri);cmd.add("-i");cmd.add(originFileUri);cmd.add("Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s");CmdExecuter.exec(cmd, this);return isSupported;}
private boolean isSupported;
/** * 生成视频截图 *  * @param imageSavePath *            截图文件保存全路径 * @param screenSize *            截图大小 如640x480 */public void makeScreenCut(String imageSavePath, String screenSize) {cmd.clear();cmd.add(ffmpegUri);cmd.add("-i");cmd.add(originFileUri);cmd.add("-y");cmd.add("-f");cmd.add("image2");cmd.add("-ss");cmd.add("2");cmd.add("-t");cmd.add("0.001");cmd.add("-s");cmd.add(screenSize);cmd.add(imageSavePath);CmdExecuter.exec(cmd, this);}
private List<String> cmd = new ArrayList<String>();

public void dealString(String str) {System.out.println("==dealString==="+str);
switch (status) {case Empty:break;case CheckingFile: {if (-1 != str.indexOf("Metadata:"))this.isSupported = true;break;}case GettingRuntime: {String strs="";if(str.contains("Duration")){System.out.println(str.substring(str.indexOf(":")+1,str.indexOf(",")));strs=str.substring(str.indexOf(":")+1,str.indexOf(","));if(strs!=null){runtime = TimeUtils.Test(strs);}}// Matcher m = Pattern.compile("Duration").matcher(str);// System.out.println("====msgm======="+m);// while (m.find()) {// String msg = m.group();// msg = msg.replace("Duration: ","");// System.out.println("====msg======="+msg);//runtime = TimeUtils.Test(strs);// }break;}}// switch}}

public class TimeUtils {public static long Test(String str){long t=Integer.parseInt(str.substring(1,3))*60*60+Integer.parseInt(str.substring(4,6))*60+Integer.parseInt(str.substring(7,9));System.out.println(t);return t;}}
阅读(1558)| 评论(1)