java通过url获取视频时长(无需下载文件)

 1、导入架包

        <!-- jave 核心依赖 --><dependency><groupId>ws.schild</groupId><artifactId>jave-core</artifactId><version>2.4.6</version></dependency><!-- 根据不同操作系统引入不同FFmpeg包 --><!-- window32位 FFmpeg --><dependency><groupId>ws.schild</groupId><artifactId>jave-native-win32</artifactId><version>2.4.6</version></dependency><!-- window64位 FFmpeg --><dependency><groupId>ws.schild</groupId><artifactId>jave-native-win64</artifactId><version>2.4.6</version></dependency><!-- linux64位 FFmpeg --><dependency><groupId>ws.schild</groupId><artifactId>jave-native-linux64</artifactId><version>2.4.6</version></dependency><!-- macos64位 FFmpeg --><dependency><groupId>ws.schild</groupId><artifactId>jave-native-osx64</artifactId><version>2.4.6</version></dependency>

2、创建FFmpegFileInfo类(类的位置ws.schild.jave)

 

package ws.schild.jave;import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;public class FFmpegFileInfo {private static final Log LOG = LogFactory.getLog(MultimediaObject.class);private static final Pattern SIZE_PATTERN = Pattern.compile("(\\d+)x(\\d+)", 2);private static final Pattern FRAME_RATE_PATTERN = Pattern.compile("([\\d.]+)\\s+(?:fps|tbr)", 2);private static final Pattern BIT_RATE_PATTERN = Pattern.compile("(\\d+)\\s+kb/s", 2);private static final Pattern SAMPLING_RATE_PATTERN = Pattern.compile("(\\d+)\\s+Hz", 2);private static final Pattern CHANNELS_PATTERN = Pattern.compile("(mono|stereo|quad)", 2);private final FFMPEGLocator locator;private File inputFile;public FFmpegFileInfo(File input) {this.locator = new DefaultFFMPEGLocator();this.inputFile = input;}public File getFile() {return this.inputFile;}public void setFile(File file) {this.inputFile = file;}public FFmpegFileInfo(File input, FFMPEGLocator locator) {this.locator = locator;this.inputFile = input;}public MultimediaInfo getInfo(String url) throws InputFormatException, EncoderException {FFMPEGExecutor ffmpeg = this.locator.createExecutor();ffmpeg.addArgument("-i");ffmpeg.addArgument(url);try {ffmpeg.execute();} catch (IOException var9) {throw new EncoderException(var9);}MultimediaInfo var4;try {RBufferedReader reader = new RBufferedReader(new InputStreamReader(ffmpeg.getErrorStream()));var4 = this.parseMultimediaInfo(this.inputFile, reader);} finally {ffmpeg.destroy();}return var4;}private MultimediaInfo parseMultimediaInfo(File source, RBufferedReader reader) throws InputFormatException, EncoderException {Pattern p1 = Pattern.compile("^\\s*Input #0, (\\w+).+$\\s*", 2);Pattern p2 = Pattern.compile("^\\s*Duration: (\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d\\d).*$", 2);Pattern p3 = Pattern.compile("^\\s*Stream #\\S+: ((?:Audio)|(?:Video)|(?:Data)): (.*)\\s*$", 2);Pattern p4 = Pattern.compile("^\\s*Metadata:", 2);MultimediaInfo info = null;try {int step = 0;while(true) {String line = reader.readLine();LOG.debug("Output line: " + line);if (line == null) {break;}Matcher m;String type;switch(step) {case 0:String token = source.getAbsolutePath() + ": ";if (line.startsWith(token)) {String message = line.substring(token.length());throw new InputFormatException(message);}Matcher m = p1.matcher(line);if (m.matches()) {type = m.group(1);info = new MultimediaInfo();info.setFormat(type);++step;}break;case 1:m = p2.matcher(line);if (m.matches()) {long hours = (long)Integer.parseInt(m.group(1));long minutes = (long)Integer.parseInt(m.group(2));long seconds = (long)Integer.parseInt(m.group(3));long dec = (long)Integer.parseInt(m.group(4));long duration = dec * 10L + seconds * 1000L + minutes * 60L * 1000L + hours * 60L * 60L * 1000L;info.setDuration(duration);++step;}break;case 2:m = p3.matcher(line);p4.matcher(line);if (m.matches()) {type = m.group(1);String specs = m.group(2);StringTokenizer st;int i;String token;boolean parsed;Matcher m2;int bitRate;if ("Video".equalsIgnoreCase(type)) {VideoInfo video = new VideoInfo();st = new StringTokenizer(specs, ",");for(i = 0; st.hasMoreTokens(); ++i) {token = st.nextToken().trim();if (i == 0) {video.setDecoder(token);} else {parsed = false;m2 = SIZE_PATTERN.matcher(token);if (!parsed && m2.find()) {bitRate = Integer.parseInt(m2.group(1));int height = Integer.parseInt(m2.group(2));video.setSize(new VideoSize(bitRate, height));parsed = true;}m2 = FRAME_RATE_PATTERN.matcher(token);if (!parsed && m2.find()) {try {float frameRate = Float.parseFloat(m2.group(1));video.setFrameRate(frameRate);} catch (NumberFormatException var22) {LOG.info("Invalid frame rate value: " + m2.group(1), var22);}parsed = true;}m2 = BIT_RATE_PATTERN.matcher(token);if (!parsed && m2.find()) {bitRate = Integer.parseInt(m2.group(1));video.setBitRate(bitRate * 1000);parsed = true;}}}info.setVideo(video);} else if ("Audio".equalsIgnoreCase(type)) {AudioInfo audio = new AudioInfo();st = new StringTokenizer(specs, ",");for(i = 0; st.hasMoreTokens(); ++i) {token = st.nextToken().trim();if (i == 0) {audio.setDecoder(token);} else {parsed = false;m2 = SAMPLING_RATE_PATTERN.matcher(token);if (!parsed && m2.find()) {bitRate = Integer.parseInt(m2.group(1));audio.setSamplingRate(bitRate);parsed = true;}m2 = CHANNELS_PATTERN.matcher(token);if (!parsed && m2.find()) {String ms = m2.group(1);if ("mono".equalsIgnoreCase(ms)) {audio.setChannels(1);} else if ("stereo".equalsIgnoreCase(ms)) {audio.setChannels(2);} else if ("quad".equalsIgnoreCase(ms)) {audio.setChannels(4);}parsed = true;}m2 = BIT_RATE_PATTERN.matcher(token);if (!parsed && m2.find()) {bitRate = Integer.parseInt(m2.group(1));audio.setBitRate(bitRate * 1000);parsed = true;}}}info.setAudio(audio);}}}if (line.startsWith("frame=")) {reader.reinsertLine(line);break;}}} catch (IOException var23) {throw new EncoderException(var23);}if (info == null) {throw new InputFormatException();} else {return info;}}
}

3、打包把类打成class文件放到本地的Maven仓库(如果在测试类中使用跳过此步)

 4、测试

String url="https://xxx.mp4";File mediaFile = new File(url);FFmpegFileInfo ffmpegFileInfo = new FFmpegFileInfo(mediaFile);MultimediaInfo info = null;try {info = ffmpegFileInfo.getInfo(url);long duration = info.getDuration();System.out.println("============="+duration/1000);} catch (EncoderException e) {e.printStackTrace();}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/22335.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据库应用:CentOS 7离线安装MySQL与Nginx

目录 一、理论 1.安装依赖 二、实验 1.离线安装MySQL与Nginx 2.离线安装Nginx 三、问题 1.执行nginx -v命令报错 四、总结 一、理论 1.安装依赖 &#xff08;1&#xff09;概念 安装依赖是指在软件开发中&#xff0c;为了运行或者编译一个程序或者库&#xff0c;在计…

css:横向滚动布局

效果&#xff1a; 实现代码&#xff1a; <template><div class"index_div"><div class"container"><div class"flexBox"><div class"flex_item" v-for"item in topMenu" :key"item.id&quo…

JVM回收算法(标记-清除算法, 复制算法, 标记-整理算法)

1.标记-清除算法 最基础的算法&#xff0c;分为两个阶段&#xff0c;“标记”和“清除” 原理&#xff1a; - 标记阶段&#xff1a;collector从mutator根对象开始进行遍历&#xff0c;对从mutator根对象可以访问到的对象都打上一个标识&#xff0c;一般是在对象的header中&am…

【单片机】MSP430F5529单片机的Flash读写控制,MSP430 flash 读写

文章目录 内存模型程序 内存模型 https://qq742971636.blog.csdn.net/article/details/108892807 单片机的Flash里面的区域不是全都能写的&#xff1a;https://blog.csdn.net/u014470361/article/details/79297601 找一下手册看看MSP430F5529单片机哪些地址区域能写&#xf…

uniapp下载和上传照片

利用uniapp开发的时候&#xff0c;需要下载和上传照片&#xff0c;在H5和微信小程序中的写法不一样。 H5环境下 浏览器中下载就是模拟超链接下载。也不需要获取什么权限&#xff0c;比较简单。 // #ifdef H5 this.isLoading true; let oA document.createElement("a&…

Spring Boot原理分析(二):IoC

文章目录 〇、IoC思想和IoC容器IoC体现了什么思想什么是IoC容器 一、Spring IoC容器的继承层次1.BeanFactory2.ListableBeanFactory3.HierarchicalBeanFactory4.ApplicationContext5.常用的ApplicationContext的实现类ClassPathXmlApplicationContext&#xff08;基于XML配置&a…

文件IO_打开和关闭文件(附Linux-5.15.10内核源码分析)

目录 1.打开文件 1.1 函数原型介绍 1.1.1 open函数 1.1.2 creat函数 1.1.2 openat函数 1.2 内核源码分析 1.3 函数原型区别 2.关闭文件 2.1 函数原型介绍 2.1.1 close函数 2.2 内核源码实现 1.打开文件 1.1 函数原型介绍 1.1.1 open函数 #include <sys/types.…

国产MCU-CW32F030开发学习-BH1750模块

国产MCU-CW32F030开发学习-BH1750模块 硬件平台 CW32_48F大学计划板CW32_IOT_EVA物联网开发评估套件BH1750数字型光照强度传感器 BH1750 BH1750是一款数字型光照强度传感器&#xff0c;能够获取周围环境的光照强度。其测量范围在0~65535 lx。lx勒克斯&#xff0c;是光照强…

前端漏洞xss

网络钓鱼、获取Cookie、强制弹窗获取流量、网站挂马(将恶意代码嵌入程序&#xff0c;用户浏览页面时计算机将被嵌入木马)、发送垃圾信息或广告、传播蠕虫病毒 漏洞原理 XSS(Cross Site Scripting),是一种跨站的脚本攻击&#xff0c;曾简称为CSS&#xff0c; 后改为XSS。 攻击…

git下载源码及环境搭建之数据库(二)

学习目标&#xff1a; 数据库 新项目使用 数据库文件 的配置 及相关属性的设置 步骤&#xff1a; 数据库 下图所示为开发时所用数据库 第一步&#xff1a;新建一个数据库 注意&#xff1a; 字符集与排序规则我们应该选择utf-8 相关 选中新创建的表&#xff0c;点击备份—还…

备战秋招009(20230714)

文章目录 前言一、Java内存区域1、JVM组成部分2、运行时数据区域01、基础02、程序计数器03、虚拟机栈04、本地方法栈05、堆06、方法区07、直接内存 3、HotSpot虚拟机对象01、对象的创建02、内存分配03、内存布局04、访问定位 二、垃圾回收1、堆空间01、空间结构02、GC 分类03、…

Unified Named Entity Recognition as Word-Word Relation Classification

原文链接&#xff1a;https://arxiv.org/pdf/2112.10070.pdf AAAI 2022 介绍 NER主要包括三种类型&#xff1a;flat、overlap和discontinuous。目前效果最好的模型主要是&#xff1a;span-based和seq2seq&#xff0c;但前者注重于边界的识别&#xff0c;后者可能存在exposure b…