java 调用ffmpeg获取视频时长
安装ffmpge查看该文
目的:
对文件夹的中的mp4获取时长
使用ffmpge获取视频详情,从中解析出 duration的信息
public class VadioTest { public void getVideoDuration(String path, StringBuffer bf) throws IOException, InterruptedException { File[] files = new File(path).listFiles();for (int i = 0; i < files.length; i++) { if (!files[i].isDirectory() && files[i].toString().endsWith(".mp4")) { List<String> commend = new ArrayList<String>(); commend.add("ffmpeg"); commend.add("-i"); commend.add(files[i].getPath()); ProcessBuilder builder = new ProcessBuilder(commend); builder.command(commend); builder.redirectErrorStream(true); // 进程信息输出到控制台 Process p = builder.start(); BufferedReader br = new BufferedReader(new InputStreamReader( p.getInputStream())); String line = null; while ((line = br.readLine()) != null) { // System.out.println(line); int durationIndex = line.indexOf("Duration:"); if (durationIndex > -1) { System.out.println(line.substring(durationIndex + 10, durationIndex + 20)); String time = line.substring(durationIndex + 10, durationIndex + 20); System.out.println(time.split("\\.")[0]); String[] timeArr = time.split("\\.")[0].split(":"); int time2 = Integer.valueOf(timeArr[0]) * 60 + Integer.valueOf(timeArr[1]); System.out.println(time2); bf.append("update xx_table set duration = " + time2 + " where vadio_url like %" + files[i].getName() + "%;"); } } p.waitFor();// 直到上面的命令执行完,才向下执行 } else if (files[i].isDirectory()) { getVideoDuration(files[i].getPath(), bf); } } } public static void main(String[] args) { String filePath = "/Users/xx/Desktop/test"; FileWriter fw = null; try { StringBuffer sf = new StringBuffer(""); VadioTest videoDuration = new VadioTest(); videoDuration.getVideoDuration(filePath, sf); File fileOut = new File(filePath + "/1.txt"); System.out.println(sf); fw = new FileWriter(fileOut); fw.write(sf.toString()); fw.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fw != null){ try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
}
标题:java 调用ffmpeg获取视频时长
作者:hugh0524
地址:https://blog.uproject.cn/articles/2017/03/28/1490692725651.html
0 0