[Libav-user] Encoding flv video by libavcodec not working

Mark Ma gbstack08 at gmail.com
Mon Nov 25 14:45:54 CET 2013


Hi, everyone.
I'm trying to use libavcodec to encode a flv video. Following code is a 
sample code to generate a mpeg video, it works well. But after replacing 
the codec ID with AV_CODEC_ID_FLV1, the generated video file cannot be 
played.

I opened the generated file and checked the first 4 bytes (previous tag 
size), but it's not 0. I'm curious whether converted packet is valid flv 
packet?
Any suggestion is appreciated.

(Code)

|void simpleEncode(){
     AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_MPEG1VIDEO);
     AVCodecContext *ctx = avcodec_alloc_context3(codec);
     ctx->bit_rate = 400000;
     ctx->width = 352;
     ctx->height = 288;
     AVRational time_base = {1,25};
     ctx->time_base = time_base;
     ctx->gop_size = 10;
     ctx->pix_fmt = AV_PIX_FMT_YUV420P;

     avcodec_open2(ctx, codec, NULL);

     AVFrame *frame = av_frame_alloc();
     av_image_alloc(frame->data, frame->linesize, ctx->width, ctx->height, ctx->pix_fmt, 32);
     frame->format = ctx->pix_fmt;
     frame->height = ctx->height;
     frame->width = ctx->width;

     AVPacket pkt;
     int got_output;
     FILE *f = fopen("test.mpg", "wb");
     for(int i=0; i<25; i++){
         av_init_packet(&pkt);
         pkt.data = NULL;
         pkt.size = 0;

         for(int w=0; w<ctx->width; w++){
             for(int h=0; h<ctx->height; h++){
                 frame->data[0][h*frame->linesize[0]+w]=i*10;
             }
         }
         for(int w=0; w<ctx->width/2; w++){
             for(int h=0; h<ctx->height/2; h++){
                 frame->data[1][h*frame->linesize[1]+w]=i*10;
                 frame->data[2][h*frame->linesize[2]+w]=i*10;
             }
         }

         frame->pts=i;
         avcodec_encode_video2(ctx, &pkt, frame, &got_output);

         fwrite(pkt.data, 1, pkt.size, f);
     }
}|

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20131125/7027c76f/attachment.html>


More information about the Libav-user mailing list