FFmpeg(音视频处理的瑞士军刀)开发实战指南

【欢迎关注编码小哥,学习更多实用的编程方法和技巧】

FFmpeg开发实战指南

1. FFmpeg简介

  • 开源多媒体处理框架
  • 支持音视频编解码
  • 跨平台(Windows/Linux/Mac)

2. 环境准备

2.1 安装FFmpeg

Ubuntu

sudo apt-get install ffmpeg libavcodec-dev

MacOS

brew install ffmpeg

Windows

下载官方安装包

配置环境变量

2.2 开发环境

  • C/C++
  • 推荐Qt/CMake
  • Visual Studio/CLion

3. 基础API使用

3.1 初始化

cpp 复制代码
extern "C" {
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
}

// 初始化
av_register_all();  // 旧版本
avformat_network_init();

3.2 打开视频文件

cpp 复制代码
AVFormatContext* formatContext = nullptr;
int ret = avformat_open_input(&formatContext, "video.mp4", nullptr, nullptr);
if (ret < 0) {
    // 错误处理
}

avformat_find_stream_info(formatContext, nullptr);

4. 视频解码实战

4.1 查找视频流

cpp 复制代码
int videoStreamIndex = -1;
for (unsigned int i = 0; i < formatContext->nb_streams; i++) {
    if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
        videoStreamIndex = i;
        break;
    }
}

4.2 创建解码器

cpp 复制代码
AVCodecParameters* codecParams = formatContext->streams[videoStreamIndex]->codecpar;
AVCodec* codec = avcodec_find_decoder(codecParams->codec_id);

AVCodecContext* codecContext = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(codecContext, codecParams);
avcodec_open2(codecContext, codec, nullptr);

5. 视频帧解码

5.1 解码流程

cpp 复制代码
AVPacket* packet = av_packet_alloc();
AVFrame* frame = av_frame_alloc();

while (av_read_frame(formatContext, packet) >= 0) {
    if (packet->stream_index == videoStreamIndex) {
        avcodec_send_packet(codecContext, packet);
        
        while (avcodec_receive_frame(codecContext, frame) == 0) {
            // 处理解码帧
            processFrame(frame);
        }
    }
    av_packet_unref(packet);
}

6. 视频转码

6.1 转码示例

cpp 复制代码
AVOutputFormat* outputFormat = av_guess_format(nullptr, "output.mp4", nullptr);
AVFormatContext* outputContext = nullptr;
avformat_alloc_output_context2(&outputContext, outputFormat, nullptr, nullptr);

// 配置编码器
AVCodec* encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
AVCodecContext* encoderContext = avcodec_alloc_context3(encoder);

7. 音频处理

7.1 音频重采样

cpp 复制代码
SwrContext* swrCtx = swr_alloc_set_opts(
    nullptr,
    AV_CH_LAYOUT_STEREO, 
    AV_SAMPLE_FMT_S16, 
    44100,
    AV_CH_LAYOUT_MONO, 
    AV_SAMPLE_FMT_FLTP, 
    44100,
    0, 
    nullptr
);

swr_init(swrCtx);

8. 高级实战

8.1 视频滤镜

cpp 复制代码
AVFilter* buffersrc = avfilter_get_by_name("buffer");
AVFilter* buffersink = avfilter_get_by_name("buffersink");

AVFilterGraph* filterGraph = avfilter_graph_alloc();
// 配置滤镜链

9. 错误处理

9.1 常用宏

cpp 复制代码
#define CHECK_RET(ret) \
    do { \
        if (ret < 0) { \
            char errbuf[1024]; \
            av_strerror(ret, errbuf, sizeof(errbuf)); \
            printf("Error: %s\n", errbuf); \
            return -1; \
        } \
    } while(0)

10. 内存管理

10.1 资源释放

cpp 复制代码
avformat_close_input(&formatContext);
avcodec_free_context(&codecContext);
av_frame_free(&frame);
av_packet_free(&packet);

11. 实战项目推荐

  1. 视频播放器
  2. 视频转码工具
  3. 直播推流
  4. 音视频编辑器

12. 学习建议

  1. 官方文档
  2. 源码研究
  3. 实践项目
  4. 社区交流

技术难点

  1. 音视频同步
  2. 内存管理
  3. 性能优化
相关推荐
阿酷tony2 小时前
将视频生成视频二维码步骤
音视频·视频格式·视频二维码·视频生成二维码
9527华安2 小时前
国产紫光同创FPGA视频采集转SDI编码输出,基于HSSTHP高速接口,提供2套工程源码和技术支持
fpga开发·音视频·紫光同创·sdi·高速接口·hssthp
潮汐退涨月冷风霜3 小时前
开发了一个b站视频音频提取器
音视频
qq_316837753 小时前
使用ffmpeg 将图片合成为视频,填充模糊背景,并添加两段音乐
ffmpeg·音视频
林鸿群7 小时前
Mediamtx与FFmpeg远程与本地推拉流使用
ffmpeg
18538162800余。8 小时前
碰一碰发视频源码,碰一碰发视频OEM
音视频
一念春风12 小时前
C# 音频分离(MP3伴奏)
数据库·c#·音视频
Oliverro14 小时前
智慧景区国标GB28181视频平台EasyGBS视频融合应用全场景解决方案
网络·音视频
h39741 天前
MFC文件-写MP4
c++·windows·音视频·mfc
王江奎1 天前
音视频小白系统入门课-4
音视频