您的位置:首页 > 编程语言 > Java开发

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

2017-08-02 14:45 267 查看
由于html5 的video标签只支持 h264编码格式的MP4 所以需要对上传的MP4 做格式转换 所以简单的研究了下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/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 显示就正常了
现在就可以直接调用 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ffmpeg java 视频