static int flv_write_header(AVFormatContext *s)
{
    ByteIOContext *pb = s->pb;
    FLVContext *flv = s->priv_data;
    int i, width, height, samplerate, samplesize, channels, audiocodecid, videocodecid;
    double framerate = 0.0;
    int metadata_size_pos, data_size;
    flv->hasAudio = 0;
    flv->hasVideo = 0;
    for(i=0; i<s->nb_streams; i++){
        AVCodecContext *enc = s->streams[i]->codec;
        if (enc->codec_type == CODEC_TYPE_VIDEO) {
            width = enc->width;
            height = enc->height;
            if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {
                framerate = av_q2d(s->streams[i]->r_frame_rate);
            } else {
                framerate = 1/av_q2d(s->streams[i]->codec->time_base);
            }
            flv->hasVideo=1;
            videocodecid = enc->codec_tag;
            if(videocodecid == 0) {
                av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\n");
                return -1;
            }
        } else {
            flv->hasAudio=1;
            samplerate = enc->sample_rate;
            channels = enc->channels;
            audiocodecid = enc->codec_tag;
            samplesize = (enc->codec_id == CODEC_ID_PCM_S8) ? 8 : 16;
            if(get_audio_flags(enc)<0)
                return -1;
        }
        av_set_pts_info(s->streams[i], 24, 1, 1000);
    }
    put_tag(pb,"FLV");
    put_byte(pb,1);
    put_byte(pb,   FLV_HEADER_FLAG_HASAUDIO * flv->hasAudio
                 + FLV_HEADER_FLAG_HASVIDEO * flv->hasVideo);
    put_be32(pb,9);
    put_be32(pb,0);
    for(i=0; i<s->nb_streams; i++){
        if(s->streams[i]->codec->codec_tag == 5){
            put_byte(pb,8);
            put_be24(pb,0);
            put_be24(pb,0);
            put_be32(pb,0);
            put_be32(pb,11);
            flv->reserved=5;
        }
    }
    put_byte(pb, 18);
    metadata_size_pos= url_ftell(pb);
    put_be24(pb, 0);
    put_be24(pb, 0);
    put_be32(pb, 0);
    put_byte(pb, AMF_DATA_TYPE_STRING);
    put_amf_string(pb, "onMetaData");
    put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
    put_be32(pb, 5*flv->hasVideo + 4*flv->hasAudio + 2);
    put_amf_string(pb, "duration");
    flv->duration_offset= url_ftell(pb);
    put_amf_double(pb, 0);
    if(flv->hasVideo){
        put_amf_string(pb, "width");
        put_amf_double(pb, width);
        put_amf_string(pb, "height");
        put_amf_double(pb, height);
        put_amf_string(pb, "videodatarate");
        put_amf_double(pb, s->bit_rate / 1024.0);
        put_amf_string(pb, "framerate");
        put_amf_double(pb, framerate);
        put_amf_string(pb, "videocodecid");
        put_amf_double(pb, videocodecid);
    }
    if(flv->hasAudio){
        put_amf_string(pb, "audiosamplerate");
        put_amf_double(pb, samplerate);
        put_amf_string(pb, "audiosamplesize");
        put_amf_double(pb, samplesize);
        put_amf_string(pb, "stereo");
        put_amf_bool(pb, (channels == 2));
        put_amf_string(pb, "audiocodecid");
        put_amf_double(pb, audiocodecid);
    }
    put_amf_string(pb, "filesize");
    flv->filesize_offset= url_ftell(pb);
    put_amf_double(pb, 0);
    put_amf_string(pb, "");
    put_byte(pb, AMF_END_OF_OBJECT);
    data_size= url_ftell(pb) - metadata_size_pos - 10;
    url_fseek(pb, metadata_size_pos, SEEK_SET);
    put_be24(pb, data_size);
    url_fseek(pb, data_size + 10 - 3, SEEK_CUR);
    put_be32(pb, data_size + 11);
    return 0;
}
['static int flv_write_header(AVFormatContext *s)\n{\n    ByteIOContext *pb = s->pb;\n    FLVContext *flv = s->priv_data;\n    int i, width, height, samplerate, samplesize, channels, audiocodecid, videocodecid;\n    double framerate = 0.0;\n    int metadata_size_pos, data_size;\n    flv->hasAudio = 0;\n    flv->hasVideo = 0;\n    for(i=0; i<s->nb_streams; i++){\n        AVCodecContext *enc = s->streams[i]->codec;\n        if (enc->codec_type == CODEC_TYPE_VIDEO) {\n            width = enc->width;\n            height = enc->height;\n            if (s->streams[i]->r_frame_rate.den && s->streams[i]->r_frame_rate.num) {\n                framerate = av_q2d(s->streams[i]->r_frame_rate);\n            } else {\n                framerate = 1/av_q2d(s->streams[i]->codec->time_base);\n            }\n            flv->hasVideo=1;\n            videocodecid = enc->codec_tag;\n            if(videocodecid == 0) {\n                av_log(enc, AV_LOG_ERROR, "video codec not compatible with flv\\n");\n                return -1;\n            }\n        } else {\n            flv->hasAudio=1;\n            samplerate = enc->sample_rate;\n            channels = enc->channels;\n            audiocodecid = enc->codec_tag;\n            samplesize = (enc->codec_id == CODEC_ID_PCM_S8) ? 8 : 16;\n            if(get_audio_flags(enc)<0)\n                return -1;\n        }\n        av_set_pts_info(s->streams[i], 24, 1, 1000);\n    }\n    put_tag(pb,"FLV");\n    put_byte(pb,1);\n    put_byte(pb,   FLV_HEADER_FLAG_HASAUDIO * flv->hasAudio\n                 + FLV_HEADER_FLAG_HASVIDEO * flv->hasVideo);\n    put_be32(pb,9);\n    put_be32(pb,0);\n    for(i=0; i<s->nb_streams; i++){\n        if(s->streams[i]->codec->codec_tag == 5){\n            put_byte(pb,8);\n            put_be24(pb,0);\n            put_be24(pb,0);\n            put_be32(pb,0);\n            put_be32(pb,11);\n            flv->reserved=5;\n        }\n    }\n    put_byte(pb, 18);\n    metadata_size_pos= url_ftell(pb);\n    put_be24(pb, 0);\n    put_be24(pb, 0);\n    put_be32(pb, 0);\n    put_byte(pb, AMF_DATA_TYPE_STRING);\n    put_amf_string(pb, "onMetaData");\n    put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);\n    put_be32(pb, 5*flv->hasVideo + 4*flv->hasAudio + 2);\n    put_amf_string(pb, "duration");\n    flv->duration_offset= url_ftell(pb);\n    put_amf_double(pb, 0);\n    if(flv->hasVideo){\n        put_amf_string(pb, "width");\n        put_amf_double(pb, width);\n        put_amf_string(pb, "height");\n        put_amf_double(pb, height);\n        put_amf_string(pb, "videodatarate");\n        put_amf_double(pb, s->bit_rate / 1024.0);\n        put_amf_string(pb, "framerate");\n        put_amf_double(pb, framerate);\n        put_amf_string(pb, "videocodecid");\n        put_amf_double(pb, videocodecid);\n    }\n    if(flv->hasAudio){\n        put_amf_string(pb, "audiosamplerate");\n        put_amf_double(pb, samplerate);\n        put_amf_string(pb, "audiosamplesize");\n        put_amf_double(pb, samplesize);\n        put_amf_string(pb, "stereo");\n        put_amf_bool(pb, (channels == 2));\n        put_amf_string(pb, "audiocodecid");\n        put_amf_double(pb, audiocodecid);\n    }\n    put_amf_string(pb, "filesize");\n    flv->filesize_offset= url_ftell(pb);\n    put_amf_double(pb, 0);\n    put_amf_string(pb, "");\n    put_byte(pb, AMF_END_OF_OBJECT);\n    data_size= url_ftell(pb) - metadata_size_pos - 10;\n    url_fseek(pb, metadata_size_pos, SEEK_SET);\n    put_be24(pb, data_size);\n    url_fseek(pb, data_size + 10 - 3, SEEK_CUR);\n    put_be32(pb, data_size + 11);\n    return 0;\n}']