Linux下FFmpeg之H264编译

一、概述

FFmpeg内部以及支持多种编解码模式,但是也有一些编解码模式并没有加入 FFmpeg 源码中。因此,当我们需要定的时候就需要自己以第三方库的方式加入 FFmpeg 中。以下内容接着上篇文章介绍 FFmpeg 中加入第三方库的编译方法,主要以 H264 为主。

二、安装 H264 

     1)配置

./configure --prefix=/home/llh/ffmpeg/build/

根据错误提示,修改配置信息如下:

./configure --prefix=/home/llh/ffmpeg/build/ --disable-asm

上述信息看出并不会编译出相应动态库,因此我们继续修改配置信息如下:

./configure --prefix=/home/llh/ffmpeg/build/ --disable-asm --enable-shared

     2)编译

make

     3)安装

make install

至此,H264安装成功!

 

三、编译 FFmpeg

     1)配置

      根据上篇文章说明,我们在配置第三方库的时候需要配置环境变量 PKG_CONFIG_PATH ,以及制定相应的头文件和库文件路径。具体配置如下:

export PKG_CONFIG_PATH=/home/llh/ffmpeg/build/lib/pkgconfig:$PKG_CONFIG_PATH
./configure --prefix=/home/llh/ffmpeg/build/ --enable-shared --extra-cflags=-I/home/llh/ffmpeg/build/include --extra-ldflags=-L/home/llh/ffmpeg/build/lib --enable-gpl --enable-libx264

 

     2)编译

make

     3)安装

make install

至此,FFmpeg 安装成功,一下开始测试是否加载了 H264。

四、测试H264

     1)查看 ffmpeg 支持的格式

ffmpeg -formats | grep h26

 

     2)查看 FFmpeg 支持的编码器

ffmpeg -codecs | grep h26

 

     3)查看 FFmpeg 中 H.264 编码参数支持  

ffmpeg -h encoder=h264

 

     4)查看 FFmpeg 支持的解码器

ffmpeg -decoders | grep h26

以上测试看出当前编译的 FFmpeg 中已经支持了 H264。

五、其他第三方库的编译   ---》在线安装方式

     1)H265

sudo apt-get install libx265-dev libnuma-dev

     编译 FFmpeg 时需要加上 --enable-gpl --enable-libx265    

     2)libvpx

sudo apt-get install libvpx-dev

     编译 FFmpeg 时需要加上 --enable-libvpx 

     3)libfdk-aac

sudo apt-get install libfdk-aac-dev

     编译 FFmpeg 时需要加上 --enable-libfdk-aac  --enable-nonfree 

     4)libmp3lame

sudo apt-get install libmp3lame-dev

     编译 FFmpeg 时需要加上 --enable-libmp3lame

     5)libopus

sudo apt-get install libopus-dev

     编译 FFmpeg 时需要加上 --enable-libopus

六、总结

     1)解压源码包之后一般都会有一个 README 文件,这个文件中一般会介绍如何编译这个源码包的步骤,或者是介绍整个源码包框架。

     2)执行 ./configure -h 查看配置信息(以上两点点很重要,但是很多人都会忽略)

     3)编译三部曲

./configure [arg1] [arg2] [...] [argn]

make 

make install

     4)当遇到要加入外部库的时候一般都需要指定外部库的头文件路径和库文件路径,具体怎么指定,最好以第 2 步查看为准。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 系统下使用 FFmpeg 解码 H.264 实时视频,可以按照以下步骤进行: 1. 安装 FFmpeg 首先需要安装 FFmpeg 库,可以使用以下命令在 Ubuntu 系统中安装: ``` sudo apt-get install ffmpeg ``` 2. 编写代码 接下来需要编写 C++ 代码,使用 FFmpeg 库进行 H.264 视频解码。 示例代码如下: ```c++ #include <iostream> #include <fstream> #include <string.h> #include <stdio.h> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #include <libavutil/imgutils.h> } using namespace std; int main(int argc, char* argv[]) { AVFormatContext* pFormatCtx = NULL; AVCodecContext* pCodecCtx = NULL; AVCodec* pCodec = NULL; AVFrame* pFrame = NULL; AVFrame* pFrameRGB = NULL; uint8_t* buffer = NULL; int numBytes; if(argc < 2) { printf("Please provide a H264 video file.\n"); return -1; } av_register_all(); if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0) { printf("Error: Cannot open video file.\n"); return -1; } if(avformat_find_stream_info(pFormatCtx, NULL) < 0) { printf("Error: Cannot find stream information.\n"); return -1; } AVCodecParameters* pCodecParams = NULL; int videoStreamIndex = -1; for(int i = 0; i < pFormatCtx->nb_streams; i++) { if(pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStreamIndex = i; pCodecParams = pFormatCtx->streams[i]->codecpar; break; } } if(videoStreamIndex == -1) { printf("Error: Cannot find video stream.\n"); return -1; } pCodec = avcodec_find_decoder(pCodecParams->codec_id); if(pCodec == NULL) { printf("Error: Cannot find codec.\n"); return -1; } pCodecCtx = avcodec_alloc_context3(pCodec); if(avcodec_parameters_to_context(pCodecCtx, pCodecParams) != 0) { printf("Error: Cannot copy codec parameters to context.\n"); return -1; } if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { printf("Error: Cannot open codec.\n"); return -1; } pFrame = av_frame_alloc(); pFrameRGB = av_frame_alloc(); if(pFrame == NULL || pFrameRGB == NULL) { printf("Error: Cannot allocate frames.\n"); return -1; } numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t)); av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); AVPacket packet; int frameFinished = 0; struct SwsContext* sws_ctx = NULL; while(av_read_frame(pFormatCtx, &packet) >= 0) { if(packet.stream_index == videoStreamIndex) { avcodec_send_packet(pCodecCtx, &packet); while(avcodec_receive_frame(pCodecCtx, pFrame) == 0) { sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL); sws_scale(sws_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize); sws_freeContext(sws_ctx); printf("Decoding frame %d\n", pCodecCtx->frame_number); } } av_packet_unref(&packet); } av_free(buffer); av_free(pFrameRGB); av_free(pFrame); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); return 0; } ``` 3. 编译代码 使用以下命令编译上述代码: ``` g++ -o decode_h264 decode_h264.cpp -lavformat -lavcodec -lswscale -lavutil ``` 4. 运行代码 使用以下命令运行编译好的代码: ``` ./decode_h264 input.h264 ``` 其中 `input.h264` 是待解码的 H.264 视频文件名。 以上就是在 Linux 系统下使用 FFmpeg 解码 H.264 实时视频的过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值