liunx 安装FFmpeg并集成x264,简单java调用

由于html5 的video标签只支持 h264编码格式的MP4 所以需要对上传的MP4 做格式转换 所以简单的研究了下FFmpeg

对于windows下的FFmpeg 使用就很简单了,下载绿色版解压就可以直接 使用了

而liunx就必要麻烦一些.因为我的liunx无法访问外网所以只能先下载好后上传到服务器上,这里就不给出 liunx下的下载命令,自行搜索下网上有好多的

  1. 需要安装yasm 解压安装即可
    tar xzvf yasm-1.3.0.tar.gz
    cd yasm-1.3.0
    ./configure --enable-shared --disable-asm
    make
    make install
  2. 安装x264
    cd x264-snapshot-20170801-2245
    ./configure --enable-shared --disable-asm
    make
    make install

  3. 安装ffmpeg
    ./configure --enable-shared --disable-yasm --enable-libx264 --enable-encoder=libx264 --enable-gpl --prefix=/usr/local/ffmpe
    make
    make install
    
    
    --prefix=/usr/local/ffmpeg 为指定安装的目录 
    make时间会比较长 耐心等待

    安装完成之后可以 进入 /usr/local/ffmpeg 查看目录
    执行
    /usr/local/ffmpeg/bin/ffmpeg -version
    查看版本信息

    如果报错 libavdevice.so.57: cannot open shared object file: No such file or directory
    原因是lib目录未加载到链接到系统库中,系统ld目录列表在/etc/ld.so.conf中,打开文件会发现,
    里面引用了/etc/ld.so.conf.d/下面所有的.conf文件,
    比如mariadb-x86_64.conf我们只需要创建一个文件并写入lib路径即可,
    执行命令:
    vim /etc/ld.so.conf.d/ffmpeg.conf 
    然后添加一行内容:
    /usr/local/ffmpeg/lib 
    (实际就是你解压ffmpeg的目录)之后保存并退出,
    然后执行 ldconfig 使配置生效,现在再次执行 ./ffmpeg -version 显示就正常了
  4. 现在就可以直接调用 ffmpeg看看了
    /usr/local/ffmpeg/bin/ffmpeg -y -i 321.mp4 -f h264  3456.mp4

  5. 在看看java调用ffmpeg 其实就是模拟系统命令,借鉴下往上的代码吧
    /**
         * ffmpeg将其他格式转格式文件(未指定其他任何参数)
         * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
         * 
         * @param ffmpegPath
         *            ffmpeg.exe路径
         * @param srcVideoPath
         *            视频文件(原)
         * @param tarVideoPath
         *            视频文件(新)
         * @return
         */
        public static boolean processFfmpegOther(String ffmpegPath, String srcVideoPath, String tarVideoPath) {
            if (!checkfile(srcVideoPath)) {
                System.out.println("【" + srcVideoPath + "】  不存在 !");
                return false;
            }
            long l = System.currentTimeMillis();
            List<String> commend = new java.util.ArrayList<String>();
    
    //        String type = tarVideoPath.substring(tarVideoPath.lastIndexOf(".") + 1, tarVideoPath.length());
    
            commend.add(ffmpegPath);
    
            commend.add("-y");
    
            commend.add("-i");
    
            commend.add(srcVideoPath);
    
    //         if(type.toUpperCase().equals("MP4")){  
    //           commend.add( " -f h264 ");    
    //         }else{  
    //             
    //         }  
    
            commend.add(tarVideoPath);
    
            try {
                ProcessBuilder builder = new ProcessBuilder();
                builder.command(commend);
                Process process = builder.start();
                doWaitFor(process);
                process.destroy();
                if (!checkfile(tarVideoPath)) {
                    System.out.println(tarVideoPath + " is not exit! processFfmpegOther 转换不成功 !");
                    return false;
                }
                System.out.println("视频["+srcVideoPath+"] 转换成功!耗时:"+(System.currentTimeMillis() - l));
                return true;
            } catch (Exception e) {
                System.out.println("【" + srcVideoPath + "】processFfmpegOther 转换不成功 !");
                return false;
            }
        }
    
        private static boolean checkfile(String filepath) {
            return new File(filepath).exists();
        }
    
        /**
         * 等待进程处理
         * 
         * @param p
         * @return
         */
        @SuppressWarnings("unused")
        public static int doWaitFor(Process p) {
            InputStream in = null;
            InputStream err = null;
            int exitValue = -1; // returned to caller when p is finished
            try {
                in = p.getInputStream();
                err = p.getErrorStream();
                boolean finished = false; // Set to true when p is finished
                while (!finished) {
                    try {
                        while (in.available() > 0) {
                            Character c = new Character((char) in.read());
                        }
                        while (err.available() > 0) {
                            Character c = new Character((char) err.read());
                        }
                        exitValue = p.exitValue();
                        finished = true;
                    } catch (IllegalThreadStateException e) {
                        Thread.currentThread();
                        Thread.sleep(500);
                    }
                }
    
            } catch (Exception e) {
                System.out.println("doWaitFor();: unexpected exception - " + e.getMessage());
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (IOException e) {
                    System.out.println("等待进程处理错误");
                }
                if (err != null) {
                    try {
                        err.close();
                    } catch (IOException e) {
                        System.out.println("等待进程处理错误");
                    }
                }
            }
            return exitValue;
        }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

love13135816

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值