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

2020-12-23  本文已影响0人  WilburLi

原文: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; 
    }
}

该工具类需要的依赖:

<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>
上一篇下一篇

猜你喜欢

热点阅读