为什么官方的 LibAV 12 视频示例无法正常工作?

问题描述

我会说标题不言自明的,但我几乎完全复制了 LibAV right here 给出的示例,但它产生的输出视频无法播放。为什么不能播放?我是否使用了错误文件扩展名?我不明白我在这里可能做错了什么,而且我几乎找不到关于如何在 C++ 中编码 mp4 视频的文档。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libavcodec/avcodec.h"
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"

static void encode(AVCodecContext *enc_ctx,AVFrame *frame,AVPacket *pkt,FILE *outfile) {
    int ret;
    ret = avcodec_send_frame(enc_ctx,frame);
    if (ret < 0) {
        fprintf(stderr,"error sending a frame for encoding\n");
        exit(1);
    }
    while (ret >= 0) {
        ret = avcodec_receive_packet(enc_ctx,pkt);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
            fprintf(stderr,"error during encoding\n");
            exit(1);
        }
        printf("encoded frame %3"PRId64" (size=%5d)\n",pkt->pts,pkt->size);
        fwrite(pkt->data,1,pkt->size,outfile);
        av_packet_unref(pkt);
    }
}

int main() {
    const char *filename = "animation.mp4";
    const AVCodec *codec;
    AVCodecContext *c = NULL;
    int i,ret,x,y;
    FILE *f;
    AVFrame *picture;
    AVPacket *pkt;
    uint8_t endcode[] = { 0,0xb7 };
    if (argc <= 1) {
        fprintf(stderr,"Usage: %s <output file>\n",argv[0]);
        exit(0);
    }
    avcodec_register_all();
    codec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);
    if (!codec) {
        fprintf(stderr,"codec not found\n");
        exit(1);
    }
    c = avcodec_alloc_context3(codec);
    picture = av_frame_alloc();
    pkt = av_packet_alloc();
    if (!pkt)
        exit(1);
    c->bit_rate = 400000;
    c->width = 352;
    c->height = 288;
    c->time_base = (AVRational){1,25};
    c->framerate = (AVRational){25,1};
    c->gop_size = 10;
    c->max_b_frames=1;
    c->pix_fmt = AV_PIX_FMT_YUV420P;
    if (avcodec_open2(c,codec,NULL) < 0) {
        fprintf(stderr,"Could not open codec\n");
        exit(1);
    }
    f = fopen(filename,"wb");
    if (!f) {
        fprintf(stderr,"Could not open %s\n",filename);
        exit(1);
    }
    picture->format = c->pix_fmt;
    picture->width  = c->width;
    picture->height = c->height;
    ret = av_frame_get_buffer(picture,32);
    if (ret < 0) {
        fprintf(stderr,"Could not alloc the frame data\n");
        exit(1);
    }

    for(i=0;i<25;i++) {
        fflush(stdout);
        ret = av_frame_make_writable(picture);
        if (ret < 0)
            exit(1);

        for(y=0;y<c->height;y++) {
            for(x=0;x<c->width;x++) {
                picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
            }
        }

        for(y=0;y<c->height/2;y++) {
            for(x=0;x<c->width/2;x++) {
                picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
                picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
            }
        }

        picture->pts = i;
        encode(c,picture,pkt,f);
    }

    encode(c,NULL,f);

    fwrite(endcode,sizeof(endcode),f);
    fclose(f);
    avcodec_free_context(&c);
    av_frame_free(&picture);
    av_packet_free(&pkt);
    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)