ffmpeg获取视频信息

1.实体类VideoInfo


public class VideoInfoVo {
    /**
     * 码率 单位kb/s
     */
    private Integer bitrate;
    /**
     * 音频码率
     */
    private Integer aBitrate;
    /**
     * 视频码率
     */
    private Integer vBitrate;
    /**
     * 大小 单位字节
     */
    private Long size ;
    /**
     * 时长 秒
     */
    private Integer duration;
    /**
     * 视频编码
     */
    private String vCode;
    /**
     * 音频编码
     */
    private String aCode;
    /**
     * 压缩级别 main,high,baseline,extent
     * 对压缩视频特性的描述,profile越高,就说明采用了越高级的压缩特性。
     */
    private String vProfile;
    /**
     *
     */
    private String aProfile;
    /**
     * Level是对视频的描述,Level越高,视频的码率、分辨率、fps越高
     */
    private Integer level;
    /**
     * 分辨率
     */
    private String resolution;
    /**
     * 颜色制式
     */
    private String pixFmt;
    /**
     * 声道
     */
    private Integer channels;
    /**
     * 音频采样率
     */
    private Integer sampleRate;
    /**
     * b帧数量
     */
    private Integer hasBFrames;
    /**
     * 帧率 单位fps
     */
    private Integer frameRate;


2.cmd工具类

public class CmdUtils {
    /**
     * cmd
     * @param commend 命令
     * @return
     */
    public static String cmd(String commend ) {
        String bufs = "";
        List<String> commendList = new ArrayList<String>();
        ProcessBuilder builder = new ProcessBuilder();
        builder.command(commendList);
        builder.redirectErrorStream(true);

        String[] ss = Utils.stringToArray(commend, " ");
        for (int i = 0; i < ss.length; i++) {
            commendList.add(ss[i]);
        }
        String commend_desc = commendList.toString().replace(", ", " ");
        Utils.logger(VideoUtil.class,"cmd","命令:"+commend_desc);
        try {
            Process process1 = builder.start();
            Utils.logger(VideoUtil.class,"process1","process1:"+process1);
            InputStream is2 = process1.getInputStream();
            Utils.logger(VideoUtil.class,"is2","is2:"+is2);
            BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
            Utils.logger(VideoUtil.class,"br2","br2:"+br2);
            StringBuilder buf2 = new StringBuilder();


            String line2 = null;
            while ((line2 = br2.readLine()) != null) {
                buf2.append(line2 + "\r\n");
            }
            Utils.logger(VideoUtil.class,"buf2","buf2:"+buf2);
            bufs = buf2.toString();
            Utils.logger(VideoUtil.class,"bufs","bufs:"+bufs);
            br2.close();
            is2.close();
            process1.destroy();
        } catch (Exception e) {
            e.printStackTrace(System.out);
            System.out.println(e.getMessage() + "\r\n cmd:" + commend_desc);
        }


        return bufs;
    }
    }

3.获取视频信息

public static VideoInfoVo getVideoInfos(String videoUrl){
        //去掉了参数id、title
        VideoInfoVo videoInfo = new VideoInfoVo();
        //本地命令
        //String perfix = "D:\\Desktop\\ffmpeg-2022-05-04-git-0914e3a14a-full_build\\bin\\";
        //String cmd=perfix+"ffprobe -print_format json -show_format -show_streams -v quiet "+videoUrl;
        //线上命令
        String cmd="ffprobe -print_format json -show_format -show_streams -v quiet "+videoUrl;
        String info = CmdUtils.cmd(cmd);
        if("".equals(info)){
            return videoInfo;
        }
        JSONObject videoInfoJson = JSON.parseObject(info);
        JSONObject format = videoInfoJson.getJSONObject("format");
        JSONArray streams = videoInfoJson.getJSONArray("streams");
        if(Utils.notEmpty(format)){
            int bitrate = Utils.objectToInt(format.getString("bit_rate"))/1020;
            Double duration = Utils.objectToDouble(format.getString("duration"));
            long size = Utils.objectToLong(format.getString("size"));
            videoInfo.setBitrate(bitrate);
            videoInfo.setDuration(duration.intValue());
            videoInfo.setSize(size);
        }
        int length = streams.size();

        for (int i=0;i<length;i++){
            JSONObject streamObj = streams.getJSONObject(i);
            String codeType = streamObj.getString("codec_type");
            String codec = streamObj.getString("codec_name");
            String profile = streamObj.getString("profile");
            Integer height = streamObj.getIntValue("height");
            Integer width = streamObj.getIntValue("width");
            Integer hasBFrames = streamObj.getIntValue("has_b_frames");
            Integer level = streamObj.getIntValue("level");
            Integer bitrate = streamObj.getIntValue("bit_rate")/1000;
            Integer channels = streamObj.getIntValue("channels");
            String pix_fmt = streamObj.getString("pix_fmt");
            String fps = streamObj.getString("avg_frame_rate");
            int sampleRate = streamObj.getIntValue("sample_rate");
            String[] fpsVal= fps.split("/");
            Integer frameRate = Utils.notEmpty(fpsVal)?Utils.objectToInt(fpsVal[0]):0;
            if("video".equals(codeType)){
                videoInfo.setvBitrate(bitrate);
                videoInfo.setvCode(codec);
                videoInfo.setvProfile(profile);
                videoInfo.setLevel(level);
                videoInfo.setPixFmt(pix_fmt);
                videoInfo.setFrameRate(frameRate);
                videoInfo.setHasBFrames(hasBFrames);
                videoInfo.setResolution(width+"x"+height);

            }else if("audio".equals(codeType)){
                videoInfo.setaProfile(profile);
                videoInfo.setaBitrate(bitrate);
                videoInfo.setaCode(codec);
                videoInfo.setChannels(channels);
                videoInfo.setSampleRate(sampleRate);
            }
        }
        return videoInfo;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值