一、下载ffmpeg

http://www.ffmpeg.org/download.html

主要需要bin目录下的ffmpeg可执行文件

二、java代码实现

package com.aw.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.oro.text.regex.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @author hy
* @date 2019/11/7 14:05
*/
public class FileParseUtil { public static final Logger LOGGER = LoggerFactory.getLogger(FileParseUtil.class); /**
* 提取音频、视频编码等信息
*
* @param filePath
* @return
*/
public static Map<String, String> getEncodingFormat(String filePath) {
String processFLVResult = processFLV(filePath);
Map retMap = new HashMap();
if (StringUtils.isNotBlank(processFLVResult)) {
PatternCompiler compiler = new Perl5Compiler();
try {
String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
String regexVideo = "Video: (.*?), (.*?), (.*?)[,\\s]";
String regexAudio = "Audio: (\\w*), (\\d*) Hz"; Pattern patternDuration = compiler.compile(regexDuration, Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcherDuration = new Perl5Matcher();
if (matcherDuration.contains(processFLVResult, patternDuration)) {
MatchResult re = matcherDuration.getMatch();
retMap.put("提取出播放时间", re.group(1));
retMap.put("开始时间", re.group(2));
retMap.put("bitrate 码率 单位 kb", re.group(3));
System.out.println("提取出播放时间 ===" + re.group(1));
System.out.println("开始时间 =====" + re.group(2));
System.out.println("bitrate 码率 单位 kb==" + re.group(3));
} Pattern patternVideo = compiler.compile(regexVideo, Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcherVideo = new Perl5Matcher(); if (matcherVideo.contains(processFLVResult, patternVideo)) {
MatchResult re = matcherVideo.getMatch();
retMap.put("编码格式", re.group(1));
retMap.put("视频格式", re.group(2));
retMap.put("分辨率", re.group(3));
System.out.println("编码格式 ===" + re.group(1));
System.out.println("视频格式 ===" + re.group(2));
System.out.println(" 分辨率 == =" + re.group(3));
} Pattern patternAudio = compiler.compile(regexAudio, Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcherAudio = new Perl5Matcher(); if (matcherAudio.contains(processFLVResult, patternAudio)) {
MatchResult re = matcherAudio.getMatch();
retMap.put("音频编码", re.group(1));
retMap.put("音频采样频率", re.group(2));
System.out.println("音频编码 ===" + re.group(1));
System.out.println("音频采样频率 ===" + re.group(2));
}
} catch (MalformedPatternException e) {
e.printStackTrace();
}
}
return retMap; } // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static String processFLV(String inputPath) {
List<String> commend = new java.util.ArrayList<String>(); commend.add("D:\\aisino\\ffmpeg-20191105-c54268c-win64-static\\bin\\ffmpeg");//可以设置环境变量从而省去这行
commend.add("ffmpeg");
commend.add("-i");
commend.add(inputPath); try { ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.redirectErrorStream(true);
Process p = builder.start(); //1. start
BufferedReader buf = null; // 保存ffmpeg的输出结果流
String line = null;
//read the standard output buf = new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuffer sb = new StringBuffer();
while ((line = buf.readLine()) != null) {
System.out.println(line);
sb.append(line);
continue;
}
int ret = p.waitFor();//这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行
//1. end
return sb.toString();
} catch (Exception e) {
LOGGER.error("-- processFLV error, message is {}", e);
return null;
}
}
}

原文:https://www.iteye.com/blog/gaozzsoft-1483576

java调用ffmpeg获取视频文件信息的一些参数的更多相关文章

  1. Java使用FFmpeg处理视频文件指南

    Java使用FFmpeg处理视频文件指南 本文主要讲述如何使用Java + FFmpeg实现对视频文件的信息提取.码率压缩.分辨率转换等功能: 之前在网上浏览了一大圈Java使用FFmpeg处理音视频 ...

  2. Java使用FFmpeg处理视频文件的方法教程

    这篇文章主要给大家介绍了关于Java使用FFmpeg处理视频文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧 前言 本文主要 ...

  3. Java调用FFmpeg进行视频处理及Builder设计模式的应用

    1.FFmpeg是什么 FFmpeg(https://www.ffmpeg.org)是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它用来干吗呢?视频采集.视频格式转化.视频 ...

  4. JAVA通过URL链接获取视频文件信息(无需下载文件)

    最近项目碰到一个大坑:APP上需要在获取视频列表时就获取视频的时长,但早期上传的时候数据库都没有保存这个数据,所以前段时间添加一个时长字段,在上传时手动输入视频时长,但是之前库中有上万条数据没这个信息 ...

  5. centos8平台用ffprobe获取视频文件信息(ffmpeg4.2.2)

    一,ffprobe的作用 ffprobe是强大的视频分析工具, 用于从多媒体流中获取相关信息或查看文件格式信息, 并以可读的方式打印 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https:// ...

  6. java 实现视频转换通用工具类:获取视频元数据信息(一)

    java 做视频转换主要用到开源的ffmpeg或者mencoder,还要有MP4Box. 注:由于平时都没有时间写博客,所以思路我就不写了,有问题问我,不一定马上回复. 详细介绍: ffmpeg:ht ...

  7. JAVA调用FFMpeg进行转码等操作

    直接上代码: public abstract class FFmpegUtils { FFmpegUtils ffmpegUtils; ; String timeLength = "&quo ...

  8. android 中获取视频文件的缩略图(非原创)

    在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 2. android 2.2以后使用ThumbnailUtils类获取 3.调用jni文件,实现MediaMetadataRet ...

  9. Java调用ffmepg+mencoder视频格式转换(*)

    PS: 建议大家在官网下载最新的资源 其他格式转FLV格式,可以用Java调用ffmpeg和memcoder实现 ffmepg: D:\ffmpeg\bin\ffmpeg.exe -i E:\1.mp ...

随机推荐

  1. c语言数组在作为参数传递时注意事项

    1.不能在[]给定大小 2.不要在被调用函数里使用sizeof(a)/sizeof(a[0])形式传递数组大小,直接给出数组长度,或者在 主调函数中使用sizeof(a)/sizeof(a[0])传递 ...

  2. TOMCAT web.xml 整理说明

    JavaWeb项目中web.xml有关servlet的基本配置: 我们注意到,tomcat下的conf中也有一个web.xml文件,没错的,所有的JavaWeb项目中web.xml都继承自服务器下的w ...

  3. DOTS学习资源

    以下是一些面向数据的资源,可以是Unity或我们已经验证过的外部资源.我们将包括外部资源,我们认为这些外部资源能够很好地理解面向数据的设计并包含高质量的信息(在贡献时). 注意:由于Unity Dat ...

  4. 【VS开发】程序员对内存的理解

    程序员对内存的理解 在C和C++语言开发中,指针.内存一直是学习的重点.因为C语言作为一种偏底层的中低级语言,提供了大量的内存直接操作的方法,这一方面使程序的灵活度最大化,同时也为bug埋下很多隐患. ...

  5. 使用sequelize-auto生成sequelize的Models

    一.全局安装sequelize-auto npm install -g sequelize-auto 二.全局安装对应数据库的驱动,此处使用的是mysql npm install -g mysql 三 ...

  6. SpringCloud学习(六)分布式配置中心(Spring Cloud Config)(Finchley版本)

    在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理, ...

  7. python报错及处理 -- 不断总结

    ModuleNotFoundError: No module named 'PIL' 解决方法: 运行命令:pip install Pillow IndentationError: expected ...

  8. 桥接模式下访问虚拟机中的Django项目

    首先需要保证主机和虚拟机能相互Ping通,如果Ping不通,请参考我上篇文章,这里演示的是桥接模式下的方法,如果是NAT模式连接,请参考别处. 1. 虚拟机Linux系统内的Django项目 sett ...

  9. 写着玩的bash脚本

    #!/bin/bash function strealingManu { pwdfolder=`pwd` for var in "$@" do man 1 $var > $p ...

  10. 斜率优化DP(转载)

    转载自:https://www.cnblogs.com/ka200812/archive/2012/08/03/2621345.html 我们知道,有些DP方程可以转化成DP[i]=f[j]+x[i] ...