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. 性能优化
相关推荐
英英_10 小时前
视频爬虫的Python库
开发语言·python·音视频
道剑剑非道13 小时前
QT开发技术【ffmpeg + QAudioOutput】音乐播放器 完善
开发语言·qt·ffmpeg
毒果13 小时前
FFmpeg 低延迟同屏方案
ffmpeg·同屏
weixin_5275504013 小时前
Linux 环境下高效视频切帧的实用指南
linux·运维·音视频
ai产品老杨13 小时前
减少交通拥堵、提高效率、改善交通安全的智慧交通开源了。
前端·vue.js·算法·ecmascript·音视频
红米饭配南瓜汤15 小时前
WebRTC中的几个Rtp*Sender
网络·网络协议·音视频·webrtc·媒体
EasyDSS18 小时前
EasyRTC嵌入式音视频通信SDK助力物联网/视频物联网音视频打造全场景应用
人工智能·音视频
aqi0019 小时前
FFmpeg开发笔记(六十三)FFmpeg使用vvenc把视频转为H.266编码
ffmpeg·音视频·直播·流媒体
babytiger21 小时前
rk3588 上运行smolvlm-realtime-webcam,将视频转为文字描述
音视频
Icoolkj21 小时前
Komiko 视频到视频功能炸裂上线!
人工智能·音视频