F:\ffmpegDemo\ffmpeg\bin\ffmpeg.exe -i F://ffmpegDemo//test.mp4
PS:解压在windows本地的ffmpeg程序F:\ffmpegDemo\ffmpeg\bin\ffmpeg.exe以及存放在windows本地视频:F://ffmpegDemo//test.mp4
实行效果,如下:
3.3 Java解析信息并返回时长
通过FFmpeg实行命令行,获取返回的视频信息,通过java筛选到视频信息的目标数据,进行返回即可。

public static void main(String[] args) { String timeLength = getVideoTime("F://ffmpegDemo//test.mp4","F:\\ffmpegDemo\\ffmpeg\\bin\\ffmpeg.exe"); if(timeLength.length()>0){//字符串截取 timeLength =timeLength.substring(0,timeLength.indexOf(".")); } System.out.println("视频时长:"+timeLength); }
干系学习资料推举,点击下方链接免费报名,先码住不迷路~】
音视频免费学习地址:FFmpeg/WebRTC/RTMP/NDK/Android音视频流媒体高等开拓
【免费分享】音视频学习资料包、大厂口试题、技能视频和学习路线图,资料包括(C/C++,Linux,FFmpeg webRTC rtmp hls rtsp ffplay srs 等等)有须要的可以点击788280672加群免费领取~
4. 贴出完成Demo
public class ExecWindowCMD { public static void main(String[] args) { String timeLength = getVideoTime("F://ffmpegDemo//test.mp4","F:\\ffmpegDemo\\ffmpeg\\bin\\ffmpeg.exe"); if(timeLength.length()>0){//字符串截取 timeLength =timeLength.substring(0,timeLength.indexOf(".")); } System.out.println("视频时长:"+timeLength); } / 获取视频韶光 @param video_path 视频路径 @param ffmpeg_path ffmpeg安装路径 @return / public static String getVideoTime(String video_path, String ffmpeg_path) { List<String> commands = new java.util.ArrayList<String>(); commands.add(ffmpeg_path); commands.add("-i"); commands.add(video_path); System.out.println("命令行:"+ffmpeg_path+" -i "+video_path); try { ProcessBuilder builder = new ProcessBuilder(); builder.command(commands); final Process p = builder.start(); //从输入流中读取视频信息 BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream())); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); //从视频信息中解析时长 String regexDuration = "Duration: (.?), start: (.?), bitrate: (\\d) kb\\/s"; Pattern pattern = Pattern.compile(regexDuration); Matcher m = pattern.matcher(sb.toString()); if (m.find()) { //System.out.println(video_path+",视频时长:"+m.group(1)+", 开始韶光:"+m.group(2)+",比特率:"+m.group(3)+"kb/s"); return m.group(1); } } catch (Exception e) { e.printStackTrace(); } return ""; }}
实行效果如下:
视频源文件:
原文 FFmpeg框架系列:获取视频时长_ffmpeg视频合成时长代码_瓜神仙的博客-CSDN博客