java读取视频文件信息的两种方式(jave、ffmpeg)

2023-10-22 18:59

本文主要是介绍java读取视频文件信息的两种方式(jave、ffmpeg),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、通过Jave的方式读取文件信息
  1. 需要一个jar包
<!-- 获取视频时长等信息 --><dependency><groupId>jave</groupId><artifactId>jave</artifactId><version>1.0.2</version><scope>system</scope><systemPath>${project.basedir}/src/main/resources/libs/jave-1.0.2.jar</systemPath></dependency>

在这里插入图片描述
2. java代码实现

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;private void getVideoInfo(String filePath){File source = new File(filePath);Encoder encoder = new Encoder();try{MultimediaInfo mi = encoder.getInfo(source);System.out.println(mi.getVideo()); //视频信息System.out.println(mi.getAudio());  //音频信息String duration = LxTimeUtil.msecToTime(mi.getDuration());int width = mi.getVideo().getSize().getWidth();int height = mi.getVideo().getSize().getHeight();String format = mi.getFormat();int audioChannels = mi.getAudio().getChannels();String audioDecoder = mi.getAudio().getDecoder();int audioSamplingRate = mi.getAudio().getSamplingRate();String videoDecoder = mi.getVideo().getDecoder();float videoFrameRate = mi.getVideo().getFrameRate();System.out.println("★★★★★★★★★【"+source+"】★★★★★★★★★");System.out.println("格式:" + format);System.out.println("时长:" + duration);System.out.println("尺寸:" + width + "×" + height);System.out.println("音频编码:"+ audioDecoder);System.out.println("音频轨道:" + audioChannels);System.out.println("音频采样率:" + audioSamplingRate);System.out.println("视频编码:" + videoDecoder);System.out.println("视频帧率:" + videoFrameRate);//获取视频大小FileInputStream fis = new FileInputStream(source);FileChannel fc= null;fc= fis.getChannel();BigDecimal fileSize = new BigDecimal(fc.size());}catch (Exception e) {e.printStackTrace();} finally {if (null != fc) {try {fc.close();} catch (IOException e) {e.printStackTrace();}}}
}
二、通过ffmpeg的方式读取文件信息(项目中的webm视频格式通过jave解析不了,最终换成ffmpeg)
  1. 首先本地要下载ffmpeg
    http://www.ffmpeg.org/download.html
    在这里插入图片描述
  2. 随后在环境变量中配置ffmpeg
    在这里插入图片描述
    测试是否成功读取文件信息
    在这里插入图片描述
  3. java代码实现
    首先引入pom文件
<dependency><groupId>oro</groupId><artifactId>oro</artifactId><version>2.0.8</version></dependency><dependency><groupId>com.alibaba.druid</groupId><artifactId>druid-wrapper</artifactId><version>0.2.9</version></dependency>
import org.apache.commons.lang3.StringUtils;
import org.apache.oro.text.regex.*;import java.io.*;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public static Map getEncodingFormat(String filePath) throws IOException {String cut = "ffmpeg -i "+ filePath;String command = "" + cut;System.out.println(command);Process process = Runtime.getRuntime().exec(new String[]{"sh", "-c", command});InputStream in = process.getErrorStream();BufferedReader br = new BufferedReader(new InputStreamReader(in));String line;StringBuffer sb = new StringBuffer();String fps = null;FileChannel fc = null;while ((line = br.readLine()) != null) {sb.append(line);if (line.contains("fps")) {String[] split = line.split(",");for (String d : split) {if (d.contains("fps")) {String[] split1 = d.split(" fps"); //提取视频文件的fpsfps = split1[0].trim();}}}continue;}String processFLVResult = sb.toString();Map retMap = new HashMap();retMap.put("fps", fps);if (org.apache.commons.lang3.StringUtils.isNotBlank(processFLVResult)) {PatternCompiler compiler = new Perl5Compiler();try {File source = new File(filePath);FileInputStream fis = new FileInputStream(source);fc = fis.getChannel();retMap.put("size", fc.size());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));String[] split = re.group(1).split(":");String s = split[0];Integer a = Integer.parseInt(s) * 60;String s1 = split[1];Integer a1 = Integer.parseInt(s1) * 60;Integer a2 = Integer.valueOf(split[2].split("\\.")[0]);Integer w = a + a1 + a2;retMap.put("duration", w);retMap.put("开始时间", re.group(2));retMap.put("bitrate", 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("codec", re.group(1).split("\\(")[0].trim());retMap.put("format", re.group(2).split("\\(")[0]);retMap.put("width", re.group(3).split("x")[0]);retMap.put("height", re.group(3).split("x")[1]);}} catch (MalformedPatternException e) {e.printStackTrace();}finally {if (null!=fc){try {fc.close();} catch (IOException e) {e.printStackTrace();}}}}return retMap;}//linux方式下的读取方式 (因为项目是用docker部署的,所以读取的视频文件是在容器内部,所以得用这种方式读取)public static String processFLV(String filePath) {String cut1 = "ffmpeg -i "+ filePath;try {String command = "" + cut1;System.out.println(command);Process process = Runtime.getRuntime().exec(new String[]{"sh","-c",command});InputStream in = process.getErrorStream();BufferedReader br = new BufferedReader(new InputStreamReader(in));String line ;StringBuffer sb1 = new StringBuffer();while ((line = br.readLine()) != null) {sb1.append(line);if(line.contains("fps")){String[] split = line.split(",");for (String d : split) {if(d.contains("fps")){String[] split1 = d.split(" fps");String fps1 = split1[0];}}}continue;}System.out.println(sb1.toString());return sb1.toString();}catch (IOException e) {e.printStackTrace();return null;}}//windows环境下读取方式 public static String processFLVWin(String filePath) {//注意要保留单词之间有空格List commend = new java.util.ArrayList();commend.add("D:\\xbb\\ffmpeg-N-104863-g6cf55b9da2-win64-gpl-shared\\ffmpeg-N-104863-g6cf55b9da2-win64-gpl-shared\\bin\\ffmpeg.exe");//可以设置环境变量从而省去这行commend.add("ffmpeg");commend.add("-i");commend.add(filePath);try {ProcessBuilder builder = new ProcessBuilder();builder.command(commend);builder.redirectErrorStream(true);Process p = builder.start();BufferedReader buf = null;String line = null;buf = new BufferedReader(new InputStreamReader(p.getInputStream()));StringBuffer sb = new StringBuffer();while ((line = buf.readLine()) != null) {System.out.println(line);sb.append(line);if(line.contains("fps")){String[] split = line.split(",");for (String d : split) {if(d.contains("fps")){String[] split1 = d.split(" fps");String fps = split1[0];}}}continue;}int ret = p.waitFor();return sb.toString();} catch (IOException | InterruptedException e) {e.printStackTrace();return null;}}

参考博客:https://www.cnblogs.com/xhy-shine/p/11820341.html

这篇关于java读取视频文件信息的两种方式(jave、ffmpeg)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/263038

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

linux打包解压命令方式

《linux打包解压命令方式》文章介绍了Linux系统中常用的打包和解压命令,包括tar和zip,使用tar命令可以创建和解压tar格式的归档文件,使用zip命令可以创建和解压zip格式的压缩文件,每... 目录Lijavascriptnux 打包和解压命令打包命令解压命令总结linux 打包和解压命令打

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

Python中常用的四种取整方式分享

《Python中常用的四种取整方式分享》在数据处理和数值计算中,取整操作是非常常见的需求,Python提供了多种取整方式,本文为大家整理了四种常用的方法,希望对大家有所帮助... 目录引言向零取整(Truncate)向下取整(Floor)向上取整(Ceil)四舍五入(Round)四种取整方式的对比综合示例应

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式