ffmpeg jave 截取视频封面 获取视频大小 获取音频时长

因为项目需要有空研究了一下 ffmpeg ,安装过程就不贴了,我的开发环境是linux ,大家自行百度就好!

安装传送门

废话不多说 直接上代码(ps:有错误的地方,望大神指正)

截取视频第一帧:(ffmpeg)

截取帧数是可调控的,大小也是可以调控的,在参数里都写了。自己看!

/**
     * 获取视频第一帧
     * @param video_path
     * @return
     */
    private static String processImg(String video_path) {

        File file = new File(video_path);
        String path = "";
        if (!file.exists()) {

            System.err.println("路径[" + video_path + "]对应的视频文件不存在!");

            return path;

        }


        List<String> commands = new java.util.ArrayList<String>();

        commands.add(PropertyUtil.getProperty("ffmpeg_path"));

        commands.add("-i");

        commands.add(video_path);

        commands.add("-y");

        commands.add("-f");

        commands.add("image2");

        commands.add("-ss");

        commands.add("1");//这个参数是设置截取视频多少秒时的画面

        commands.add("-t");

        commands.add("0.001");

        //commands.add("-s");

        //commands.add("1920x1080");//宽X高
        path = video_path.substring(0, video_path.lastIndexOf(".")).replaceFirst("vedio", "file") + ".jpg";

        commands.add(path);

        try {

            ProcessBuilder builder = new ProcessBuilder();

            builder.command(commands);

            builder.start();

            System.out.println("截取成功");
            //休眠两秒,要不然上传图片的时候会找不到图片
            Thread.sleep(2000);
            String id = "";
            File img = new File(path);
            //清理文件
            if (img.exists()) {
                //上传截图
                System.out.println("~~~~~~~~~~~~~~~~~ 上传视频第一帧, 删除图片 ~~~~~~~~~~~~~~~~~");
                id = FileUtilByRest.uploadFile(img,UUIDUtil.createUUID() + ".jpg");
                img.delete();
            }

            return id;

        } catch (Exception e) {

            e.printStackTrace();

            return "";

        }

    }

获取视频时长:(jave  jar包自行百度)

音频也通用

/**
     * 	获取视频文件时长
     * @param music
     * @return
     * @throws BitstreamException
     */
    private String getVideoTime(File music)throws IOException, BitstreamException {
        System.out.println("~~~~~~~~~~~~~~~~ 开始获取视频时长 ~~~~~~~~~~~~~~~~~");
        Encoder encoder = new Encoder();
        try {
            MultimediaInfo m = encoder.getInfo(music);
            long ls = m.getDuration();
            int time = (int) ls / 1000;
            int ss = time % 60;
            int mm = time / 60 % 60;
            int hh = time / 60 / 60 % 24;

            String result = "";
            if(ss != 0){
                String HH = hh == 0 ? "" : getTime(hh) + ":";
                result = HH + getTime(mm) + ":" + getTime(ss);
            }

            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "00:00";
    }

/**
     * 把时间转换成字符串
     * @param time
     * @return
     */
    private String getTime(int time) {
        if(time == 0){
            return "00";
        }else{
            if(time <= 9 ){
                return "0" + time;
            }else{
                return time + "";
            }
        }
    }

 获取视频大小:(nio)

  /**
     * 获取视频大小
     * @param source
     * @return
     */
    private Long readVideoSize(File source) {
        FileChannel fc= null;
        Long size = 0L;
        try {
            @SuppressWarnings("resource")
            FileInputStream fis = new FileInputStream(source);
            fc= fis.getChannel();
            BigDecimal fileSize = new BigDecimal(fc.size());
            size = Long.parseLong(fileSize.toString());
            if (size > 0) {
                size = Long.parseLong(fileSize.toString());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null!=fc){
                try{
                    fc.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
        return size;
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取视频,可以使用FFmpeg.AutoGen 库中的AVFormatContext和AVCodecContext结构体中的duration成员变量。具体实现步骤如下: 1. 使用avformat_open_input函数打开视频文件,得到AVFormatContext结构体。 2. 使用avformat_find_stream_info函数获取视频流信息,填充AVFormatContext结构体。 3. 遍历AVFormatContext结构体的streams数组,找到视频流对应的AVCodecContext结构体。 4. 从AVCodecContext结构体的duration成员变量获取视频。 以下是示例代码: ```csharp using FFmpeg.AutoGen; // 打开视频文件 AVFormatContext* formatContext = null; int ret = ffmpeg.avformat_open_input(&formatContext, filename, null, null); if (ret < 0) { // 打开失败,处理错误 } // 获取流信息 ret = ffmpeg.avformat_find_stream_info(formatContext, null); if (ret < 0) { // 获取失败,处理错误 } // 查找视频流 AVCodecContext* codecContext = null; int videoStreamIndex = -1; for (int i = 0; i < formatContext->nb_streams; i++) { AVStream* stream = formatContext->streams[i]; if (stream->codecpar->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; codecContext = ffmpeg.avcodec_alloc_context3(null); if (codecContext == null) { // 分配失败,处理错误 } ret = ffmpeg.avcodec_parameters_to_context(codecContext, stream->codecpar); if (ret < 0) { // 转换失败,处理错误 } break; } } // 获取视频 long duration = (long)codecContext->duration * 1000 / ffmpeg.AV_TIME_BASE; ``` 注意:以上代码仅供参考,实际使用应该根据具体情况进行调整和完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值