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>
  • 13
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 26
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值