<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 2013/11/25 21:45, Mark Ma wrote:<br>
    </div>
    <blockquote cite="mid:52935492.4030904@gmail.com" type="cite">
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      Hi, everyone.<br>
      <span style="color: rgb(0, 0, 0); font-family: Arial, 'Liberation
        Sans', 'DejaVu Sans', sans-serif; font-size: 14px; font-style:
        normal; font-variant: normal; font-weight: normal;
        letter-spacing: normal; line-height: 18px; orphans: auto;
        text-align: left; text-indent: 0px; text-transform: none;
        white-space: normal; widows: auto; word-spacing: 0px;
        -webkit-text-stroke-width: 0px; background-color: rgb(255, 255,
        255); display: inline !important; float: none;">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.<br>
      </span><br>
      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?<br>
      Any suggestion is appreciated.<br>
      <br>
      (Code)<br>
      <pre style="margin: 0px 0px 10px; padding: 5px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: rgb(238, 238, 238); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; width: auto; max-height: 600px; word-wrap: normal; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial;"><code style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; vertical-align: baseline; background-color: rgb(238, 238, 238); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, ser
if
; background-position: initial initial; background-repeat: initial initial;">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);
    }
}</code></pre>
    </blockquote>
    <br>
    Now it's solved by adding flv file header and tag header manually in
    the code.<br>
  </body>
</html>