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. 性能优化
相关推荐
余~~185381628001 小时前
矩阵碰一碰发视频的后端源码技术,支持OEM
线性代数·矩阵·音视频
划水哥~3 小时前
高清下载油管视频到本地
音视频
Luke Ewin8 小时前
根据音频中的不同讲述人声音进行分离音频 | 基于ai的说话人声音分离项目
人工智能·python·音视频·语音识别·声纹识别·asr·3d-speaker
Macdo_cn19 小时前
Infuse Pro for Mac v8.1 全能视频播放器 支持M、Intel芯片
macos·音视频
aaon223571 天前
ubuntu ffmpeg 安装踩坑
linux·ubuntu·ffmpeg
m0_748245171 天前
SpringCloud-使用FFmpeg对视频压缩处理
spring·spring cloud·ffmpeg
我爱蛋蛋后1 天前
Linux驱动开发之音频驱动与基础应用编程
linux·c语言·驱动开发·音视频
Macdo_cn1 天前
Screen Wonders for Mac v3.3.1 3D屏保应用 支持M、Intel芯片
macos·音视频
iummature1 天前
FFmpeg命令
ffmpeg
苏三福1 天前
rk3588/3576板端编译程序无法运行视频推理
arm开发·音视频