FFmpeg处理音视频的常用API及一般流程

FFmpeg是一个开源的音视频处理库,提供了丰富的API用于音视频的编解码、转码、过滤、播放等操作。

一、使用FFmpeg API解码涉及到的函数及一般流程如下:

  1. av_register_all(): 注册所有的编解码器和格式。
cpp 复制代码
av_register_all();
  1. avformat_open_input(): 打开输入文件(流)并获取文件(流)的格式信息。
cpp 复制代码
AVFormatContext *fmt_ctx = NULL;
if (avformat_open_input(&fmt_ctx, input_file, NULL, NULL) < 0) {
    // 打开文件失败
}
  1. avformat_find_stream_info(): 获取流的详细信息。
cpp 复制代码
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
    // 获取流信息失败
}
  1. avcodec_find_decoder(): 查找解码器。
cpp 复制代码
AVCodec *codec = avcodec_find_decoder(stream->codecpar->codec_id);
if (!codec) {
    // 找不到解码器
}
  1. avcodec_open2(): 打开解码器。
cpp 复制代码
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
if (avcodec_open2(codec_ctx, codec, NULL) < 0) {
    // 打开解码器失败
}
  1. av_read_frame(): 读取一帧音视频数据。
cpp 复制代码
AVPacket packet;
while (av_read_frame(fmt_ctx, &packet) >= 0) {
    // 处理音视频数据
    av_packet_unref(&packet);
}
  1. avcodec_send_packet()和avcodec_receive_frame(): 解码音视频数据。
cpp 复制代码
AVFrame *frame = av_frame_alloc();
while (av_read_frame(fmt_ctx, &packet) >= 0) {
    if (packet.stream_index == audio_stream_index) {
        // 解码音频数据
        avcodec_send_packet(codec_ctx, &packet);
        while (avcodec_receive_frame(codec_ctx, frame) >= 0) {
            // 处理解码后的音频帧
        }
    } else if (packet.stream_index == video_stream_index) {
        // 解码视频数据
        avcodec_send_packet(codec_ctx, &packet);
        while (avcodec_receive_frame(codec_ctx, frame) >= 0) {
            // 处理解码后的视频帧
        }
    }
    av_packet_unref(&packet);
}

二、与解码类似,编码的流程一般为:

  1. avformat_alloc_output_context2(): 创建输出格式上下文。
cpp 复制代码
AVFormatContext *out_fmt_ctx = NULL;
if (avformat_alloc_output_context2(&out_fmt_ctx, NULL, NULL, output_file) < 0) {
    // 创建输出格式上下文失败
}
  1. avcodec_find_encoder(): 查找编码器。
cpp 复制代码
AVCodec *encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!encoder) {
    // 找不到编码器
}
  1. avcodec_open2(): 打开编码器。
cpp 复制代码
AVCodecContext *encoder_ctx = avcodec_alloc_context3(encoder);
if (avcodec_open2(encoder_ctx, encoder, NULL) < 0) {
    // 打开编码器失败
}
  1. avformat_write_header(): 写入输出文件的头部信息。
cpp 复制代码
if (avformat_write_header(out_fmt_ctx, NULL) < 0) {
    // 写入头部信息失败
}
  1. avcodec_send_frame()和avcodec_receive_packet(): 编码音视频数据。
cpp 复制代码
AVPacket encoded_packet;
while (av_read_frame(fmt_ctx, &packet) >= 0) {
    if (packet.stream_index == audio_stream_index) {
        // 编码音频数据
        avcodec_send_frame(encoder_ctx, frame);
        while (avcodec_receive_packet(encoder_ctx, &encoded_packet) >= 0) {
            // 处理编码后的音频数据
        }
    } else if (packet.stream_index == video_stream_index) {
        // 编码视频数据
        avcodec_send_frame(encoder_ctx, frame);
        while (avcodec_receive_packet(encoder_ctx, &encoded_packet) >= 0) {
            // 处理编码后的视频数据
        }
    }
    av_packet_unref(&packet);
}
  1. av_write_frame()和av_write_trailer(): 写入编码后的音视频数据。
cpp 复制代码
if (av_write_frame(out_fmt_ctx, &encoded_packet) < 0) {
    // 写入音视频数据失败
}

三、解码和编码结束后都要及时释放内存:

  1. avformat_close_input(): 关闭输入文件。
cpp 复制代码
avformat_close_input(&fmt_ctx);
  1. avcodec_free_context(): 释放编解码器上下文。
cpp 复制代码
avcodec_free_context(&codec_ctx);
  1. av_frame_free(): 释放帧对象。
cpp 复制代码
av_frame_free(&frame);
  1. avformat_free_context(): 释放格式上下文。
cpp 复制代码
avformat_free_context(fmt_ctx);

了解这些常用API及流程对使用FFmpeg开发将大有裨益。

相关推荐
HUN金克斯8 分钟前
C++/C函数
c语言·开发语言·c++
慢半拍iii9 分钟前
数据结构——F/图
c语言·开发语言·数据结构·c++
iceslime42 分钟前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析
虾球xz1 小时前
CppCon 2015 学习:3D Face Tracking and Reconstruction using Modern C++
开发语言·c++·学习·3d
small_wh1te_coder2 小时前
c语言超详细知识点总结 1500行手写源码 持续更新中ing 从25年5月到6月5日
c++·c
SteveDraw3 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
十五年专注C++开发4 小时前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
?!7144 小时前
算法打卡第18天
c++·算法
zh_xuan4 小时前
c++ std::pair
开发语言·c++
CodeWithMe5 小时前
【C/C++】EBO空基类优化介绍
开发语言·c++