java 获取视频的时长、大小、格式等信息

1、最近有一个小的视频处理需求,根据传入视频的url获取视频的时长、大小、格式等信息。下面将记录一下:

package Void;

/**

* @Author:psw

* @Description:获取视频宽高大小时间工具类

*/

import it.sauronsoftware.jave.Encoder;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.nio.channels.FileChannel;

import java.util.HashMap;

import java.util.Map;

public class VoidUtils {

public static void main(String[] args){

File source = new File("http://flv4.people.com.cn/videofile3//CCTV1/2018/1/14/CCTV1_1500000_20181114_30322514_0_113_android_l.mp4");

Encoder encoder = new Encoder();

FileChannel fc= null;

String size = "";

try {

it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(source);

long ls = m.getDuration();

System.out.println("此视频时长为:"+ls/60000+"分"+(ls)/1000+"秒!");

//视频帧宽高

System.out.println("此视频高度为:"+m.getVideo().getSize().getHeight());

System.out.println("此视频宽度为:"+m.getVideo().getSize().getWidth());

System.out.println("此视频格式为:"+m.getFormat());

FileInputStream fis = new FileInputStream(source);

fc= fis.getChannel();

BigDecimal fileSize = new BigDecimal(fc.size());

size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";

System.out.println("此视频大小为"+size);

}catch (Exception e) {

e.printStackTrace();

}finally {

if (null!=fc){

try {

fc.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* @param path

* @return Map

*/

public static Map<String, Object> getVoideMsg(String path){

Map<String, Object> map = new HashMap<String, Object>();

File file = new File(path);

Encoder encoder = new Encoder();

FileChannel fc= null;

String size = "";

if(file != null){

try {

it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(file);

long ls = m.getDuration();

FileInputStream fis = new FileInputStream(file);

fc= fis.getChannel();

BigDecimal fileSize = new BigDecimal(fc.size());

size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";

map.put("height", m.getVideo().getSize().getHeight());

map.put("width", m.getVideo().getSize().getWidth());

map.put("format", m.getFormat());

}catch (Exception e) {

e.printStackTrace();

}finally {

if (null!=fc){

try {

fc.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

return map;

}

}

该工具类需要的依赖:

<dependency>

<groupId>it.sauronsoftware</groupId>

<artifactId>jave</artifactId>

<version>1.0.2</version>

<scope>system</scope>

<systemPath>${project.basedir}/src/main/resources/lib/jave-1.0.2.jar</systemPath>

</dependency>

<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->

<dependency>

<groupId>commons-codec</groupId>

<artifactId>commons-codec</artifactId>

<version>1.10</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

<version>4.3.1</version>

</dependency>

其中jave-1.0.2.jar 这个maven中没有 ,需要自己下载:

下载后在resource下创建lib放在里面

此时因为是外部包 不是maven自带的需要scope内system

${project.basedir}/src/main/resources/lib/jave-1.0.2.jar为项目中jar的路径

<dependency>

<groupId>it.sauronsoftware</groupId>

<artifactId>jave</artifactId>

<version>1.0.2</version>

<scope>system</scope>

<systemPath>${project.basedir}/src/main/resources/lib/jave-1.0.2.jar</systemPath>

</dependency>

当scope为system的时候打包不会自动打包进去的,所以要添加一个参数才能打包进去的

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration>

<includeSystemScope>true</includeSystemScope>

</configuration>

</plugin>

</plugins>

</build>

————————————————

原文链接:https://blog.csdn.net/qq_34288630/article/details/85836152

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容