fmpeg中第三方库的编译_libx264和librtmp

在前一篇blog中编译ffmpeg,是没有添加进任何库的.但是ffplay照样可以播放本地flv文件,播放rtmp协议,其中的编解码是h264和aac的.也就是说没有第三方库的支持,ffmpeg是支持h264和aac解码的.分析一下ffmpeg的configure的输出:

Enabled decoders:  //解码器 aac和h264都支持
aac ... h264 ....

Enabled encoders:  //编码器支持aac,h264就不支持了
aac ...

Enabled protocols:  //协议支持hls和rtmp
 hls         rtmp ...

Enabled demuxers:  //支持容器解析:flv
flv ... 

Enabled muxers:  //支持容器封装:flv
flv ...

通过上面,h264编码并不支持,必须得安装第三方库:x264.我原先以为ffmpeg默认使用了librtmp库和X264库.这个理解应该是错的.

先把这些库编译进去.

1 libx264

1.1 下载

下载网址是:
http://download.videolan.org/pub/videolan/x264/snapshots/

文件名:
last_stable_x264.tar.bz2

x264有一个git:

git clone http://git.videolan.org/git/x264.git

还有两个相关的网址:

http://www.x264.nl/x264_main.php
http://www.videolan.org/developers/x264.html  //直接点下载不行.

1.2 编译x264

启动:C:\MinGW\msys\1.0\msys.bat,cd到x264库的目录

$./configure --enable-shared --disable-asm
$ make
$ make install

编译结果在:C:\MinGW\msys\1.0\local 目录下会多6个文件:

C:\MinGW\msys\1.0\local\lib\libx264.dll.a
C:\MinGW\msys\1.0\local\bin\libx264-148.dll
C:\MinGW\msys\1.0\local\lib\pkgconfig\x264.pc
C:\MinGW\msys\1.0\local\include\x264_config.h
C:\MinGW\msys\1.0\local\include\x264.h
C:\MinGW\msys\1.0\local\bin\x264.exe

编译很顺利,几分钟的事.

1.3 编进ffmpeg

1.3.1 configure

在我们之前的configure添加:–enable-gpl –enable-libx264

./configure --enable-shared --disable-static --enable-ffplay --disable-ffprobe --disable-ffserver --extra-cflags=-IE:/myproject/ffmpeg-3.0.2/include   --extra-ldflags=-LE:/myproject/ffmpeg-3.0.2/lib  --enable-memalign-hack --enable-gpl --enable-libx264

哇塞,不会所有又需要重新编译一次吧.

产生错误,没有找到libx264.把C:\MinGW\msys\1.0\local的bin,include,lib都拷到E:/myproject/ffmpeg-3.0.2中.因为我的configure命令写着:

--extra-cflags=-IE:/myproject/ffmpeg-3.0.2/include   --extra-ldflags=-LE:/myproject/ffmpeg-3.0.2/lib

其实不拷贝,增加–extra-cflags和–extra-ldflags.

configure成功后,我们研究一下configure的输出.简化了一下,把关注的东西留下来.

install prefix            /usr/local
source path               .
C compiler                gcc
C library                 mingw32
ARCH                      x86 (generic)
big-endian                no
...
SDL support               yes    //只有这个为yes才能编译出ffplay

Enabled programs: //编译出来的执行文件
ffmpeg ffplay

External libraries: //扩展库,libx264是新增加的
iconv  libx264  sdl

Enabled decoders: //解码器
aac         ...

Enabled encoders: //编码器 , libx264 是新加的
aac         libx264 ...

Enabled hwaccels:  //硬件加速器

Enabled parsers:   //解析器
aac   h264 vp9

Enabled demuxers:  解复用,也就是封装解析
hls         ....

Enabled muxers:  合并器
mp3...

Enabled protocols: 协议
hls    rtmp

Enabled filters: 过滤器,过滤器是啥,没接触过.
select...

Enabled bsfs: //位流过滤器,不知道是什么?

Enabled indevs: //设备吗?

Enabled outdevs: //设备吗?

License: LGPL version 2.1 or later
Creating config.mak, config.h, and doc/config.texi...
config.h is unchanged
config.asm is unchanged
libavutil/avconfig.h is unchanged

增加libx264的编码器.说明configure成功.

1.3.2 make

编译完成后,生成的主要文件是:主要是新增了哪些库

INSTALL libavdevice/avdevice.dll
INSTALL libavfilter/avfilter.dll
INSTALL libavformat/avformat.dll   //format
INSTALL libavcodec/avcodec.dll     //codec
INSTALL libpostproc/postproc.dll
INSTALL libswresample/swresample.dll
INSTALL libswscale/swscale.dll
INSTALL libavutil/avutil.dll      
INSTALL ffmpeg.exe
INSTALL ffplay.exe

这些库是啥作用呢?我们后面会分析.

1.4 测试

测试libx264的编码,都没法测试.没搞过,先留着吧.从configure的输出来看,肯定是编译进去了.

2 librtmp

2.1 下载

git clone git://git.ffmpeg.org/rtmpdump

里面有一个文件夹:librtmp

2.2 编译

由于librtmp 需要openssl,这下麻烦了.

算了,不编译了.只是分析一下.

2.3 分析

rtmpdump的网址是:https://rtmpdump.mplayerhq.hu/

看一下它的说明:

rtmpdump is a toolkit for RTMP streams. All forms of RTMP are supported, including rtmp://, rtmpt://, rtmpe://, rtmpte://, and rtmps://.

librtmp 支持:rtmp://, rtmpt://, rtmpe://, rtmpte://, and rtmps://.

来分析一下ffmpeg支持了rtmp的哪些协议:

Enabled protocols:
rtmp  rtmpt

所以如果把librtmp编译进去后,ffmpeg会增加的协议是:rtmpe://, rtmpte://, and rtmps://.

这几个协议意思是:

RTMP
RTMPE = RTMP + ENC,已加密的RTMP
RTMPS = RTMP + SSL,通过SSL传输层传输RTMP
RTMPT = RTMP + HTTP,在HTTP协议中传输RTMP
RTMPTE = RTMP + HTTP + ENC,在HTTP协议中传输已加密RTMP
RTMPTS = RTMP + HTTP + SSL,通过SSL传输层在HTTP协议中传输RTMP
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以按照以下步骤使用`ff_libx264_encoder`: 1. 首先,确保你已经安装了`ffmpeg`工具,因为`ff_libx264_encoder`是`ffmpeg`库的一部分。 2. 在程序引入相关的头文件: ```c #include <libavcodec/avcodec.h> ``` 3. 初始化`ffmpeg`库: ```c av_register_all(); ``` 4. 创建编码器上下文和输出文件: ```c AVCodecContext *codec_ctx; AVCodec *codec; AVFrame *frame; AVPacket pkt; FILE *output_file; const char *filename = "output.mp4"; // 查找编码器 codec = avcodec_find_encoder_by_name("libx264"); if (!codec) { // 错误处理 } // 创建编码器上下文 codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { // 错误处理 } // 配置编码器参数 codec_ctx->width = width; // 视频宽度 codec_ctx->height = height; // 视频高度 // ... // 打开编码器 if (avcodec_open2(codec_ctx, codec, NULL) < 0) { // 错误处理 } // 创建输出文件 output_file = fopen(filename, "wb"); if (!output_file) { // 错误处理 } ``` 5. 分配和初始化帧数据: ```c frame = av_frame_alloc(); if (!frame) { // 错误处理 } // 设置帧参数 frame->format = codec_ctx->pix_fmt; frame->width = codec_ctx->width; frame->height = codec_ctx->height; // 分配帧数据缓冲区 int ret = av_frame_get_buffer(frame, 0); if (ret < 0) { // 错误处理 } ``` 6. 循环编码每一帧: ```c while (/* 读取下一帧图像 */) { // 将图像数据拷贝到帧数据缓冲区 // ... // 发送帧数据到编码器 ret = avcodec_send_frame(codec_ctx, frame); if (ret < 0) { // 错误处理 } // 接收编码后的数据包 while (ret >= 0) { ret = avcodec_receive_packet(codec_ctx, &pkt); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { // 继续下一帧 break; } else if (ret < 0) { // 错误处理 } // 写入编码后的数据到文件 fwrite(pkt.data, 1, pkt.size, output_file); // 释放数据包 av_packet_unref(&pkt); } } ``` 7. 编码完成后,进行清理工作: ```c av_frame_free(&frame); avcodec_close(codec_ctx); avcodec_free_context(&codec_ctx); fclose(output_file); ``` 这是一个基本的使用示例,具体的步骤和参数可能需要根据你的实际需求进行调整。每一步都需要进行错误处理,以确保程序的稳定运行。请参考`ffmpeg`官方文档和示例代码获取更多详细信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值