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

原创
小哥 3年前 (2022-10-27) 阅读数 68 #大杂烩

指向本文的链接:https://blog.csdn.net/qq\_34288630/article/details/85836152
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;
}
}

此工具类所需的依赖项:

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 ———————————————— 版权声明:本文是CSDN博主“奈斯图米踢”的原文如下 CC 4.0 BY-SA 版权协议,转载请附上原始来源链接和本声明。 原始链接:https://blog.csdn.net/qq\_34288630/article/details/85836152
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

上一篇:VueJS总结 下一篇:Vue与ref属性与refs转载
热门