ffmpeg java 实现视频的抽帧和转码

安装文件

- libmp3lame MP3编码器不需要的可以不安装
- nasm-2.13 
	- 地址:https://www.nasm.us/pub/nasm/releasebuilds/2.13.03/
- x264 chrome 可以识别的MP4格式
	- git clone http://git.videolan.org/git/x264.git
- ffmpeg
- 

安装流程

  1. 安装lame

    • chmod -R 755 .
    • ./configure --enable-shared --enable-static
    • make
    • make install
  2. 安装nasm

    • ./configure
    • make
    • make install
  3. 安装x264

    • chmod -R 755 .
    • ./configure --enable-shared --enable-static
    • make
    • make install
  4. 安装ffmpeg

    • ./configure --enable-shared --enable-libx264 --enable-libmp3lame --enable-gpl
    • 注意x264是gpl协议的 不可以用于商用
  5. 指定依赖库的环境变量

    • export LD_LIBRARY_PATH=/usr/local/lib/

java代码

  1. 获取视频的基本信息
  // 根据视频路径获取基本信息
   public int getTime(String sourcePath) {
       int time = 0;
       Runtime run = null;
       try {
           run = Runtime.getRuntime();
           long start=System.currentTimeMillis();
           // 抽帧哈  ffmpeg -i a.mp4
           String snapshotCmd = "ffmpeg -i " + sourcePath;
           Process snapshotProcess = run.exec(snapshotCmd);
           //从输入流中读取视频信息  ffmpeg的信息在错误输出流里面
           BufferedReader br = new BufferedReader(new InputStreamReader(snapshotProcess.getErrorStream()));
           StringBuilder stringBuilder = new StringBuilder();
           String line = "";
           while ((line = br.readLine()) != null) {
               stringBuilder.append(line);
           }
           br.close();
           String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
           Pattern pattern = Pattern.compile(regexDuration);
           Matcher m = pattern.matcher(stringBuilder.toString());
           if (m.find()) {
               time = getTimelen(m.group(1));
               log.info("视频时长:" + time + "s , 开始时间:" + m.group(2) + ", 比特率:" + m.group(3) + "kb/s");
           }
           String regexVideo = "Video: (.*?), (.*?), (.*?)[,\\s]";
           pattern = Pattern.compile(regexVideo);
           m = pattern.matcher(stringBuilder.toString());
           if (m.find()) {
               log.info("编码格式:" + m.group(1) + ", 视频格式:" + m.group(2) + ", 分辨率:" + m.group(3) + "kb/s");
           }
           return time;
       } catch (Exception e) {
           log.info("获取视频信息失败");
           return 0;
       }
   }
  1. 转码
public void transcodeMp4(String sourcePath , String targetPath) {
    Runtime run = null;
    try {
        run = Runtime.getRuntime();
        long start=System.currentTimeMillis();
        String cmd = "ffmpge -y -i " + sourcePath + " " + targetPath;
        log.info(cmd);
        Process p = run.exec(cmd);
        //释放进程
        printFfmpegStream(p.getInputStream());
        printFfmpegStream(p.getErrorStream());
        p.waitFor();
        long end=System.currentTimeMillis();
        long costTime = end - start;
        log.info(sourcePath + " convert success, costs:" + costTime + "ms-------------------------------------------------");
        ResultModel<Boolean> booleanResultModel = mediaCmsServiceClient.localTranscodeVideo(request);
        log.info("回调成功 转码成功 \n{} \n{}", JSON.toJSONString(request), JSON.toJSONString(booleanResultModel));
    } catch (Exception e) {

        log.info("回调成功 转码失败 \n{}", JSON.toJSONString(request));
    }finally{
        run.freeMemory();
    }
}
  1. 截图
    public void snapshotMp4(String sourcePath, String targetFolderPath,Integer id) {
        Runtime run = null;
        try {
            run = Runtime.getRuntime();
            long start=System.currentTimeMillis();
            int time = getTime(sourcePath);
            float v = 10f / time;
            // 抽帧哈  ffmpeg -i 870101971ad54167a8ff9cf05edfa9b5.mp4 image/image-%05d.jpeg
            String snapshotCmd = "ffmpge -y -i " + sourcePath + " -r " + new DecimalFormat("0.####").format(Math.max(v,0.00001)) + " " + targetFolderPath + "image-%05d.jpg";
            log.info(snapshotCmd);
            Process snapshotProcess = run.exec(snapshotCmd);
            //释放进程
            printFfmpegStream(snapshotProcess.getInputStream());
            snapshotProcess.getOutputStream().close();
            snapshotProcess.waitFor();
            long end=System.currentTimeMillis();
            long costTime = end - start;
            log.info(sourcePath + " snapshot success, costs:" + costTime + "ms-------------------------------------------------");
        } catch (Exception e) {
            log.error("抽帧失败", e);
        }finally{
            //run调用lame解码器最后释放内存
            run.freeMemory();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值