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 getVoideMsg(String path){
Map map = new HashMap();
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;
}
}
该工具类需要的依赖:
it.sauronsoftware
jave
1.0.2
system
${project.basedir}/src/main/resources/lib/jave-1.0.2.jar
commons-codec
commons-codec
1.10
org.apache.httpcomponents
httpclient
4.3.1
其中jave-1.0.2.jar 这个maven中没有 ,需要自己下载:
下载后在resource下创建lib放在里面
此时因为是外部包 不是maven自带的需要scope内system
${project.basedir}/src/main/resources/lib/jave-1.0.2.jar为项目中jar的路径
it.sauronsoftware
jave
1.0.2
system
${project.basedir}/src/main/resources/lib/jave-1.0.2.jar
当scope为system的时候打包不会自动打包进去的,所以要添加一个参数才能打包进去的
org.springframework.boot
spring-boot-maven-plugin
true