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. 性能优化
相关推荐
Icoolkj13 小时前
阿里通义万相 Wan2.1-VACE:开启视频创作新境界
音视频
u1521096484913 小时前
NDS3211HV单路H.264/HEVC/HD视频编码器
音视频·实时音视频·视频编解码
科技小E1 天前
EasyRTC嵌入式音视频通信SDK打造带屏IPC全场景实时通信解决方案
人工智能·音视频
追随远方1 天前
FFmpeg在Android开发中的核心价值是什么?
android·ffmpeg
天上路人1 天前
AI神经网络降噪算法在语音通话产品中的应用优势与前景分析
深度学习·神经网络·算法·硬件架构·音视频·实时音视频
视频砖家1 天前
如何设置FFmpeg实现对高分辨率视频进行转码
ffmpeg·音视频·视频编解码·视频转码
yanjiee1 天前
视频质量分析时,遇到不同分辨率的对照视频和源视频,分辨率对齐的正确顺序。
ffmpeg·音视频
Sleepless_斑马1 天前
【FFmpeg+SDL】使用FFmpeg捕获屏幕,SDL显示
ffmpeg
hudawei9961 天前
flutter缓存网络视频到本地,可离线观看
flutter·缓存·音视频
lqj_本人1 天前
鸿蒙OS&UniApp实现视频播放与流畅加载:打造完美的移动端视频体验#三方框架 #Uniapp
uni-app·音视频·harmonyos