liunx 安装FFmpeg并集成x264,简单java调用
2017-08-02 14:45
267 查看
由于html5 的video标签只支持 h264编码格式的MP4 所以需要对上传的MP4 做格式转换 所以简单的研究了下FFmpeg
对于windows下的FFmpeg 使用就很简单了,下载绿色版解压就可以直接 使用了
而liunx就必要麻烦一些.因为我的liunx无法访问外网所以只能先下载好后上传到服务器上,这里就不给出 liunx下的下载命令,自行搜索下网上有好多的
需要安装yasm 解压安装即可
安装x264
安装ffmpeg
对于windows下的FFmpeg 使用就很简单了,下载绿色版解压就可以直接 使用了
而liunx就必要麻烦一些.因为我的liunx无法访问外网所以只能先下载好后上传到服务器上,这里就不给出 liunx下的下载命令,自行搜索下网上有好多的
需要安装yasm 解压安装即可
tar xzvf yasm-1.3.0.tar.gz cd yasm-1.3.0 ./configure --enable-shared --disable-asm make make install
安装x264
cd x264-snapshot-20170801-2245 ./configure --enable-shared --disable-asm make make install
安装ffmpeg
./configure --enable-shared --disable-yasm --enable-libx264 --enable-encoder=libx264 --enable-gpl --prefix=/usr/local/ffmpemake 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 显示就正常了
现在就可以直接调用 ffmpeg看看了/usr/local/ffmpeg/bin/ffmpeg -y -i 321.mp4 -f h264 3456.mp4
在看看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; }
相关文章推荐
- kafka介绍,安装以及简单的java调用kafka代码
- ffmpeg linux 安装和java调用
- Android 集成 FFmpeg (一) 基础知识及简单调用
- FFmpeg在Linux下的简单安装并与Java结合获取视频缩略图功能实现
- Java调用linux命令ffmpeg视频转码
- java调用ffmpeg执行视频转换-------调试成功
- *JAVA调用DLL简单小例子*
- 使用jsp生成验证码使用简单在java或jsp页面调用简单
- ICTCLAS分词系统Java调用接口在Eclipse中的安装 http://blog.csdn.net/CloneIQ/archive/2006/07/20/945909.aspx
- freebsd安装JAVA的最简单方法
- Windows下Memcached安装及Java客户端调用
- dwr例子2调用有简单参数的java方法
- 学习Java的第一步是安装好JDK,写一个Hello World, 其实JDK的学习没有那么简单,关于JDK有两个问题是很容易一直困扰Java程序员的地方:一个是CLASSPATH的问题,其实从原理上来说,是要搞清楚JRE的ClassLoader是如何加
- .NET作为客户端调用Java和.NET WebService 简单示例
- Eclipse安装CXF插件开发java web service 集成Spring
- 用java程序调用ffmpeg执行视频文件格式转换flv
- 使用开放源代码框架的 Java 应用程序的 Web 服务集成模式,第 1 部分: 实现调用模式
- 用java程序调用ffmpeg执行视频文件格式转换flv
- 在java中实现调用简单的c程序
- JAVA学习提高之---- Tomcat集成到Eclipse的方法(插件安装及配置)