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. 性能优化
相关推荐
每次的天空4 小时前
Android-自定义View的实战学习总结
android·学习·kotlin·音视频
爱分享的飘哥5 小时前
【V6.0 - 听觉篇】当AI学会“听”:用声音特征捕捉视频的“情绪爽点”
人工智能·音视频
菜包eo6 小时前
二维码驱动的独立站视频集成方案
网络·python·音视频
不太会编程的IT男8 小时前
在 Jetson Orin 开发套件上使用 Hardware Encoder / Decoder 构建 FFmpeg
ffmpeg·视频编解码·h.264
阿蒙Amon9 小时前
【Python小工具】使用 OpenCV 获取视频时长的详细指南
python·opencv·音视频
m0_687399849 小时前
写一个Ununtu C++ 程序,调用ffmpeg API, 来判断一个数字电影的视频文件mxf 是不是Jpeg2000?
开发语言·c++·ffmpeg
aqi0011 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体
正在走向自律13 小时前
第二章-AIGC入门-开启AIGC音频探索之旅:从入门到实践(6/36)
人工智能·aigc·音视频·语音识别·ai音乐·ai 音频·智能语音助手
Java患者·15 小时前
【小白】linux安装ffmpeg | java转码 【超详细】
ffmpeg
suifen_15 小时前
RK平台ffmpeg支持硬件编解码
ffmpeg