五、AVFormat的基本使用

本文档介绍如何使用AVFormat进行视频解封装,包括解复用的基本流程和处理NALU头信息的方法,通过实例代码展示解复用H264文件的过程,并提示了音频流解复用需要注意的事项。
摘要由CSDN通过智能技术生成

[TOC]

开始前的BB

通过上一章建立好相应的开发环境后,咱们就开始撸代码了,这章简单介绍了AVFormat的解封装以及简单的应用,这章的前提知识是

  1. 视频文件的组成(视频轨道,音频轨道、字幕轨道等)
  2. H264文件格式(NALU)

走你

我们新建一个类

这个类就是写我们今天的Demo,首先是要引入头文件

#include <iostream>

extern "C" {
#include <libavformat/avformat.h>
}
复制代码

使用std的命名空间,少打几个字母,没有错 我就是这么懒

using namespace std;
复制代码

解复用

这里我们先来讲一下解复用的流程

对应到代码上

/**
 * avformat 的简单使用
 *
 * 分离视频
 * 
 * @param url 视频的Url(本地/网络)
 */
void chapter05_h264(const char *url) {
    //打开文件流
    FILE *output = fopen("./output.h264", "wb+");

    //返回状态码
    int ret_code;
    //寻找到指定的流下标
    int media_index = -1;
    //分配一个存储解封装后的Packet
    AVPacket *packet = av_packet_alloc();

    //初始化网络
    avformat_network_init();

    //分配AVFormatContext
    AVFormatContext *avFormatContext = avformat_alloc_context();
    if (avFormatContext == nullptr) {
        cout << "[error] AVFormat alloc error" << endl;
        goto failed;
    }

    //打开输入流
    ret_code = avformat_open_input(&avFormatContext, url, nullptr, nullptr);
    if (ret_code < 0) {
        cout << "[error] open input failed " << av_err2str(AVERROR(ret_code)) << endl;
        goto failed;
    }

    //读取媒体文件信息
    ret_code = avformat_find_stream_info(avFormatContext, nullptr);
    if (ret_code < 0) {
        cout << "[error] find stream info failed " << av_err2str(AVERROR(ret_code)) << endl;
        goto failed;
    }

    //寻找到指定的视频流
    media_index = av_find_best_stream(avFormatContext, AVMEDIA_TYPE_VIDE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值