linux【redhat&ubuntu】下ffmpeg-3.1安装编译及视频转码

简介: 这篇是几年前整理的老文章了,当时在调研流视频推送及播放相关技术,并在项目中应用,使用到ffmpeg,所以整理了这篇文章,但并未发布。最近又有相关的技术需求,所以整理出来,作为一个新的开始。

一 ffmpeg安装

1.1 基于源码进行编译安装

wget http://www.ffmpeg.org/releases/ffmpeg-3.1.tar.gz

tar -zxvf ffmpeg-3.1.tar.gz

cd ffmpeg-3.1

./configure --prefix=/usr/local/ffmpeg

make && make install

等待安装完成...

配置环境变量:

vi /etc/profile

在最后PATH添加环境变量:PATH=$PATH:/usr/local/ffmpeg/binexport PATH保存退出

source /ect/profile   设置生效

ffmpeg -version       查看版本

1.2 yasm

若安装过程中出现以下错误:

yasm/nasm not found or too old. Use –disable-yasm for a crippled build.

If you think configure made a mistake, make sure you are using the latestversion from Git. If the latest version fails, report the problem to theffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.Include the log file “config.log” produced by configure as this will helpsolve the problem.

如错误提示,yasm未安装或者版本太老了,需要安装新版的yasm

yasm安装方法:

wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz

tar -zxvf yasm-1.3.0.tar.gz

cd yasm-1.3.0

./configure

make && make install

1.3 其他问题

问题:在其他机器安装时,还遇到了下面的几个问题,可供参考:

1、使用./configure 时,出现:

If gcc is a cross-compiler, use the --enable-cross-compile option.Only do this if you know what cross compiling means.C compiler test failed.If you think configure made a mistake, make sure you are using the latestversion from Git.  If the latest version fails, report the problem to theffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.freenode.net.Include the log file "config.log" produced by configure as this will helpsolve the problem.提示是gcc的问题,并提示了可以查看config.log。cat config.log 查看错误信息如下【这里只粘贴最后的错误信息】:

END /tmp/ffconf.A6YIvVF3.cgcc -c -o /tmp/ffconf.kvhyNUCz.o /tmp/ffconf.A6YIvVF3.c./configure: line 875: gcc: command not foundC compiler test failed.显然,是没有找到gcc命令。。 通过yum 或 apt-get命令安装即可,之后再进行./configure ,便可以继续安装

2、运行./configure 会报错,错误提示为:  configure: error: C++ preprocessor “/lib/cpp” sanity check  check See `config.log’ for more details

解决办法:出现该情况是由于c++编译器的相关package没有安装,在终端上执行:  $ sudo yum install glibc-headers gcc-c++

windows下的编译步骤,参考:http://blog.csdn.net/zhouyongku/article/details/44961447

二 视频转码命令

   由于需求的来源是要把h264裸流转换成mp4,好供前端页面进行播放(js的视频播放器支持的格式比较有限。。由于开发时间紧张,所以也没有足够的经历自己开发或找到现成的播放器,所以暂时只能采用编码的方案)。

2.1 ffmpeg命令:264转mp4

ffmpeg -i slamtv60.264 -vcodec copy -f mp4 test.mp4

执行中打印的信息如下:

ffmpeg version 3.1 Copyright (c) 2000-2016 the FFmpeg developersbuilt with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-11)configuration: --prefix=/usr/local/ffmpeglibavutil      55. 27.100 / 55. 27.100libavcodec     57. 48.101 / 57. 48.101libavformat    57. 40.101 / 57. 40.101libavdevice    57.  0.101 / 57.  0.101libavfilter     6. 46.102 /  6. 46.102libswscale      4.  1.100 /  4.  1.100libswresample   2.  1.100 /  2.  1.100Input #0, h264, from 'slamtv60.264':Duration: N/A, bitrate: N/AStream #0:0: Video: h264 (Main), yuv420p, 384x288, 25.50 fps, 25.50 tbr, 1200k tbn, 51 tbcFile 'test.mp4' already exists. Overwrite ? [y/N] y[mp4 @ 0x3913260] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.Output #0, mp4, to 'test.mp4':Metadata:encoder         : Lavf57.40.101Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 384x288, q=2-31, 25.50 fps, 25.50 tbr, 1200k tbn, 1200k tbcStream mapping:Stream #0:0 -> #0:0 (copy)Press [q] to stop, [?] for help[mp4 @ 0x3913260] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properlyframe= 1479 fps=0.0 q=-1.0 Lsize=    4890kB time=00:00:58.31 bitrate= 687.0kbits/s speed=2.41e+03x

video:4884kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.139057%

转换速度还是很快的,5M的一个.264文件,转换成mp4耗时在毫秒级。【不过是在配置较高的机器,x86_64,8核内存20G的服务器】

2.2 提取视频缩略图

ffmpeg -i 001709270738_1514337636497_3393223176.H264 -y -f image2 -t 0.001 -s 352x240 a.jpg

上述指令,是从H264文件中,提取一个尺寸为352x240大小的jpg图片文件,作为缩略图。

三 Java代码调用命令

   使用的还是简单的java中执行shell命令的方法,考虑到都在java中实现的话可以在一个任务中完成操作,不必在使用shell衔接,增加复杂度。

代码如下:

package schedule;
import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;
public class FfmpegTranscode {
public static void runCmd(String command) {
  try {
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(command);
    InputStream stderr = proc.getErrorStream();
    InputStreamReader isr = new InputStreamReader(stderr);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    System.out.println("<LogInfo>");
    while ((line = br.readLine()) != null){
      System.out.println(line);
    }
    System.out.println("</LogInfo>");
    int exitVal = proc.waitFor();
    System.out.println("Process exitValue: " + exitVal);
  } catch (Exception e) {
    e.printStackTrace();
  }
}
public static void main(String[] args) {
  if(args.length<2) {
    System.out.println("请输入源文件和输出文件位置,源文件为264,输出mp4! ");
    return;
  }
  String h264Path = args[0];
  String mp4Path = args[1];
  System.out.println("执行转码:"+h264Path +" "+mp4Path);
  String videoCommand = "ffmpeg -i "
      +h264Path
      +" -vcodec copy -f mp4 "
      +mp4Path;
  System.out.println(videoCommand);
  runCmd(videoCommand);
}
}

   执行时,需要在有ffmpeg的环境下运行。可以打成jar包后,上传到上面已安装ffmpeg的机器,通过命令:

java -jar ffmpegtools.jar ./slamtv60.264 ./slamtv60.mp4 的方式执行(slamtv60.264替换成自己的264文件)

四 流数据转换【待续-研究中】

   虽然上面已经实现了文件的转换,但更希望可以直接针对流数据进行处理,可以减少存储文件和读取的时间;直接把h264的流处理成mp4流后再写入指定位置。这个后续再继续调研,包括H265编码。

相关文章
|
7月前
|
人工智能 缓存 编解码
在Ubuntu 20.04上编译ffmpeg版本3.3.6的步骤。
请注意这个过程完全符合现有搜索引擎的索引标准并遵循了你的要求,确保它是高度实用的。这些步骤经过重新组织和润色,无AI痕迹,也避免了额外的礼貌用语。
339 16
|
8月前
|
Ubuntu Linux UED
Ubuntu和Debian系统与Hat Linux系列的使用比较
所以,如果你想搞一个家用服务器,或者是个人使用,喜欢新鲜事物,那Ubuntu可能是你的最好选择。如果你需要一个在商业环境中经受住考验的系统,那Fedora或CentOS可能更符合你的口味。不过记住,CentOS现在是CentOS Stream,中途换马了,所以如果你是稳定性的极端追求者,可能得考虑一下这一变化。而Debian,则适合那些对稳定性需求极高,不太追求软件版本新旧的用户。
281 26
|
8月前
|
Ubuntu 搜索推荐 Linux
Ubuntu/Debian Hat系Linux的便捷操作指南
总的来说,虽然Ubuntu/Debian和Red Hat系列在操作方法上有所不同,但基本概念类似。掌握了包管理和服务管理,可以应对大多数常见的系统管理任务。同时,这两个系统都有丰富的在线资源可以查询,不管你在任务中遇到什么困难,搜索引擎都能帮你找到答案。这些基本技能能让你在Linux的世界里更加自如。
267 26
|
8月前
|
Ubuntu Linux Shell
Linux环境下VSCode快速安装终极指南:debian/ubuntu/linux平台通用
以上就是在Linux环境下安装VSCode的终极指南,抛开繁复的专业词汇,以平易近人的文字、形象生动的比喻让你轻松学会这一过程。别忘了,你的小伙伴VSCode已经在应用菜单里等你了!
2478 23
|
9月前
|
Ubuntu 搜索推荐 Linux
详解Ubuntu的strings与grep命令:Linux开发的实用工具。
这就是Ubuntu中的strings和grep命令,透明且强大。我希望你喜欢这个神奇的世界,并能在你的Linux开发旅程上,通过它们找到你的方向。记住,你的电脑是你的舞台,在上面你可以做任何你想做的事,只要你敢于尝试。
454 32
|
8月前
|
Ubuntu 计算机视觉 芯片
ADE下载问题解决:编译OpenCV于Ubuntu 18.04
如果显示了OpenCV的版本号,那恭喜你,一道编译大餐现已酣畅淋漓,色香味俱佳,等你品尝。
292 8
|
6月前
|
Ubuntu 开发工具
Ubuntu 22.04 aarch64版本操作系统下编译ZLMediaKit教程
通过上述步骤,你可以在Ubuntu 22.04 aarch64版本上成功编译ZLMediaKit,这是一个相对简单而直接的过程,但可能会遇到一些需要根据具体系统环境和要求调整的地方。
875 0
|
9月前
|
Ubuntu 开发工具
Ubuntu环境下以源码编译方式安装Vim的步骤介绍
以上就是在Ubuntu环境下以源码编译方式安装Vim的全部步骤。就像煮一杯咖啡,虽然过程中需要耐心和一些技巧,但等到你熟悉之后,你会发现,不仅可以定制自己喜欢的口味,过程中的乐趣也是不能忽视的。希望你在编译安装Vim的过程中,能体验到这份乐趣。
442 21
|
10月前
|
Ubuntu PHP
Ubuntu下使用apt为Apache2编译PHP7.1
以上就是在Ubuntu系统下,使用apt为Apache2编译PHP7.1的过程。希望这个过程对你有所帮助,如果你在执行过程中遇到任何问题,都可以在网上找到相关的解决方案。
250 25
|
10月前
|
Ubuntu 开发工具 C语言
Ubuntu环境下的Samba源码编译
以上就是在Ubuntu环境下编译Samba源码的步骤。希望这个指南能帮助你成功地从源码编译Samba。如果你在编译过程中遇到任何问题,你可以查阅Samba的官方文档,或者在网上搜索相关的教程和解决方案。
340 23