ffmpeg源码分析(三)结构体之AVFormatContext

AVFormatContext是API中直接接触到的结构体,位于avformat.h中,结构体描述了一个多媒体文件或流的构成和基本信息。,是FFmpeg中最为基本的一个结构体。贯穿了ffmpeg使用的整个流程。

可以说是ffmpeg中最顶端的一个结构体。

头文件

github.com

举个例子(转封装格式,doc/examples/remux.c)

int main(int argc, char **argv)
{
   
   
    const AVOutputFormat *ofmt = NULL;
    AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
    AVPacket *pkt = NULL;
    const char *in_filename, *out_filename;
    int ret, i;
    int stream_index = 0;
    int *stream_mapping = NULL;
    int stream_mapping_size = 0;

    // 检查命令行参数是否足够
    if (argc < 3) {
   
   
        printf("usage: %s input output\n"
               "API example program to remux a media file with libavformat and libavcodec.\n"
               "The output format is guessed according to the file extension.\n"
               "\n", argv[0]);
        return 1;
    }

    // 获取输入和输出文件名
    in_filename  = argv[1];
    out_filename = argv[2];

    // 分配一个AVPacket结构体
    pkt = av_packet_alloc();
    if (!pkt) {
   
   
        fprintf(stderr, "Could not allocate AVPacket\n");
        return 1;
    }

    // 打开输入文件并读取其头信息
    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
   
   
        fprintf(stderr, "Could not open input file '%s'", in_filename);
        goto end;
    }

    // 查找流信息,此时可能会预解析一些数据
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
   
   
        fprintf(stderr, "Failed to retrieve input stream information");
        goto end;
    }

    // 打印输入文件的格式信息
    av_dump_format(ifmt_ctx, 0, in_filename, 0);

    // 分配输出格式上下文
    avformat_alloc_output_context2(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值