java获取视频_java获取视频播第一帧

import java.io.*;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/*** 获取视频的信息

* FFMPEG homepagehttp://ffmpeg.org/about.html

*/

public classVideoInfo

{//视频路径

privateString ffmpegApp;//视频时

private inthours;//视频分

private intminutes;//视频秒

private floatseconds;//视频width

private intwidth;//视频height

private intheigt;publicVideoInfo() {}publicVideoInfo(String ffmpegApp)

{this.ffmpegApp =ffmpegApp;

}publicString toString()

{return "time: " + hours + ":" + minutes + ":" + seconds + ", width = " + width + ", height= " +heigt;

}public void getInfo(String videoFilename) throwsIOException,

InterruptedException

{

String tmpFile= videoFilename + ".tmp.png";

ProcessBuilder processBuilder= new ProcessBuilder(ffmpegApp, "-y","-i", videoFilename, "-vframes", "1", "-ss", "0:0:0", "-an","-vcodec", "png", "-f", "rawvideo", "-s", "100*100", tmpFile);

Process process=processBuilder.start();

InputStream stderr=process.getErrorStream();

InputStreamReader isr= newInputStreamReader(stderr);

BufferedReader br= newBufferedReader(isr);

String line;//打印 sb,获取更多信息。 如 bitrate、width、heigt

StringBuffer sb = newStringBuffer();while ((line = br.readLine()) != null)

{

sb.append(line);

}newFile(tmpFile).delete();

System.out.println("video info:\n" +sb);

Pattern pattern= Pattern.compile("Duration: (.*?),");

Matcher matcher=pattern.matcher(sb);if(matcher.find())

{

String time= matcher.group(1);

calcTime(time);

}

pattern= Pattern.compile("w:\\d+ h:\\d+");

matcher=pattern.matcher(sb);if(matcher.find())

{

String wh=matcher.group();//w:100 h:100

String[] strs = wh.split("\\s+");if(strs != null && strs.length == 2)

{

width= Integer.parseInt(strs[0].split(":")[1]);

heigt= Integer.parseInt(strs[1].split(":")[1]);

}

}

process.waitFor();if(br != null)

br.close();if(isr != null)

isr.close();if(stderr != null)

stderr.close();

}private voidcalcTime(String timeStr)

{

String[] parts= timeStr.split(":");

hours= Integer.parseInt(parts[0]);

minutes= Integer.parseInt(parts[1]);

seconds= Float.parseFloat(parts[2]);

}publicString getFfmpegApp()

{returnffmpegApp;

}public voidsetFfmpegApp(String ffmpegApp)

{this.ffmpegApp =ffmpegApp;

}public intgetHours()

{returnhours;

}public void setHours(inthours)

{this.hours =hours;

}public intgetMinutes()

{returnminutes;

}public void setMinutes(intminutes)

{this.minutes =minutes;

}public floatgetSeconds()

{returnseconds;

}public void setSeconds(floatseconds)

{this.seconds =seconds;

}public intgetWidth()

{returnwidth;

}public void setWidth(intwidth)

{this.width =width;

}public intgetHeigt()

{returnheigt;

}public void setHeigt(intheigt)

{this.heigt =heigt;

}public static voidmain(String[] args)

{

VideoInfo videoInfo= new VideoInfo("D:\\ffmpeg\\bin\\ffmpeg.exe");try{

videoInfo.getInfo("f:/reyo/test.mkv");

System.out.println(videoInfo);

}catch(Exception e)

{

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值