static void new_video_stream(AVFormatContext *oc)
{
    AVStream *st;
    AVCodecContext *video_enc;
    int codec_id;
    st = av_new_stream(oc, oc->nb_streams);
    if (!st) {
        fprintf(stderr, "Could not alloc stream\n");
        exit(1);
    }
    avcodec_get_context_defaults2(st->codec, CODEC_TYPE_VIDEO);
    bitstream_filters[nb_output_files][oc->nb_streams - 1]= video_bitstream_filters;
    video_bitstream_filters= NULL;
    if(thread_count>1)
        avcodec_thread_init(st->codec, thread_count);
    video_enc = st->codec;
    if(video_codec_tag)
        video_enc->codec_tag= video_codec_tag;
    if(   (video_global_header&1)
       || (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))){
        video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
        avctx_opts[CODEC_TYPE_VIDEO]->flags|= CODEC_FLAG_GLOBAL_HEADER;
    }
    if(video_global_header&2){
        video_enc->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
        avctx_opts[CODEC_TYPE_VIDEO]->flags2|= CODEC_FLAG2_LOCAL_HEADER;
    }
    if (video_stream_copy) {
        st->stream_copy = 1;
        video_enc->codec_type = CODEC_TYPE_VIDEO;
    } else {
        const char *p;
        int i;
        AVCodec *codec;
        AVRational fps= frame_rate.num ? frame_rate : (AVRational){25,1};
        codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_VIDEO);
        if (video_codec_name)
            codec_id = find_codec_or_die(video_codec_name, CODEC_TYPE_VIDEO, 1);
        video_enc->codec_id = codec_id;
        codec = avcodec_find_encoder(codec_id);
        for(i=0; i<opt_name_count; i++){
            char buf[256];
            const AVOption *opt;
            const char *str= av_get_string(avctx_opts[CODEC_TYPE_VIDEO], opt_names[i], &opt, buf, sizeof(buf));
            if(str && (opt->flags & AV_OPT_FLAG_VIDEO_PARAM) && (opt->flags & AV_OPT_FLAG_ENCODING_PARAM))
                av_set_string(video_enc, opt_names[i], str);
        }
        video_enc->time_base.den = fps.num;
        video_enc->time_base.num = fps.den;
        if(codec && codec->supported_framerates){
            const AVRational *p= codec->supported_framerates;
            const AVRational *best=NULL;
            AVRational best_error= (AVRational){INT_MAX, 1};
            for(; p->den!=0; p++){
                AVRational error= av_sub_q(fps, *p);
                if(error.num <0) error.num *= -1;
                if(av_cmp_q(error, best_error) < 0){
                    best_error= error;
                    best= p;
                }
            }
            video_enc->time_base.den= best->num;
            video_enc->time_base.num= best->den;
        }
        video_enc->width = frame_width + frame_padright + frame_padleft;
        video_enc->height = frame_height + frame_padtop + frame_padbottom;
        video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*video_enc->height/video_enc->width, 255);
        video_enc->pix_fmt = frame_pix_fmt;
        if(codec && codec->pix_fmts){
            const enum PixelFormat *p= codec->pix_fmts;
            for(; *p!=-1; p++){
                if(*p == video_enc->pix_fmt)
                    break;
            }
            if(*p == -1)
                video_enc->pix_fmt = codec->pix_fmts[0];
        }
        if (intra_only)
            video_enc->gop_size = 0;
        if (video_qscale || same_quality) {
            video_enc->flags |= CODEC_FLAG_QSCALE;
            video_enc->global_quality=
                st->quality = FF_QP2LAMBDA * video_qscale;
        }
        if(intra_matrix)
            video_enc->intra_matrix = intra_matrix;
        if(inter_matrix)
            video_enc->inter_matrix = inter_matrix;
        video_enc->max_qdiff = video_qdiff;
        video_enc->thread_count = thread_count;
        p= video_rc_override_string;
        for(i=0; p; i++){
            int start, end, q;
            int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
            if(e!=3){
                fprintf(stderr, "error parsing rc_override\n");
                exit(1);
            }
            video_enc->rc_override=
                av_realloc(video_enc->rc_override,
                           sizeof(RcOverride)*(i+1));
            video_enc->rc_override[i].start_frame= start;
            video_enc->rc_override[i].end_frame  = end;
            if(q>0){
                video_enc->rc_override[i].qscale= q;
                video_enc->rc_override[i].quality_factor= 1.0;
            }
            else{
                video_enc->rc_override[i].qscale= 0;
                video_enc->rc_override[i].quality_factor= -q/100.0;
            }
            p= strchr(p, '/');
            if(p) p++;
        }
        video_enc->rc_override_count=i;
        if (!video_enc->rc_initial_buffer_occupancy)
            video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size*3/4;
        video_enc->me_threshold= me_threshold;
        video_enc->intra_dc_precision= intra_dc_precision - 8;
        video_enc->strict_std_compliance = strict;
        if (do_psnr)
            video_enc->flags|= CODEC_FLAG_PSNR;
        if (do_pass) {
            if (do_pass == 1) {
                video_enc->flags |= CODEC_FLAG_PASS1;
            } else {
                video_enc->flags |= CODEC_FLAG_PASS2;
            }
        }
    }
    video_disable = 0;
    av_freep(&video_codec_name);
    video_stream_copy = 0;
}
['static void opt_output_file(const char *filename)\n{\n    AVFormatContext *oc;\n    int use_video, use_audio, use_subtitle;\n    int input_has_video, input_has_audio, input_has_subtitle, i;\n    AVFormatParameters params, *ap = &params;\n    if (!strcmp(filename, "-"))\n        filename = "pipe:";\n    oc = av_alloc_format_context();\n    if (!file_oformat) {\n        file_oformat = guess_format(NULL, filename, NULL);\n        if (!file_oformat) {\n            fprintf(stderr, "Unable to find a suitable output format for \'%s\'\\n",\n                    filename);\n            exit(1);\n        }\n    }\n    oc->oformat = file_oformat;\n    av_strlcpy(oc->filename, filename, sizeof(oc->filename));\n    if (!strcmp(file_oformat->name, "ffm") &&\n        av_strstart(filename, "http:", NULL)) {\n        if (read_ffserver_streams(oc, filename) < 0) {\n            fprintf(stderr, "Could not read stream parameters from \'%s\'\\n", filename);\n            exit(1);\n        }\n    } else {\n        use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;\n        use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_name;\n        use_subtitle = file_oformat->subtitle_codec != CODEC_ID_NONE || subtitle_stream_copy || subtitle_codec_name;\n        if (nb_input_files > 0) {\n            check_audio_video_sub_inputs(&input_has_video, &input_has_audio,\n                                         &input_has_subtitle);\n            if (!input_has_video)\n                use_video = 0;\n            if (!input_has_audio)\n                use_audio = 0;\n            if (!input_has_subtitle)\n                use_subtitle = 0;\n        }\n        if (audio_disable) {\n            use_audio = 0;\n        }\n        if (video_disable) {\n            use_video = 0;\n        }\n        if (subtitle_disable) {\n            use_subtitle = 0;\n        }\n        if (use_video) {\n            new_video_stream(oc);\n        }\n        if (use_audio) {\n            new_audio_stream(oc);\n        }\n        if (use_subtitle) {\n            new_subtitle_stream(oc);\n        }\n        oc->timestamp = rec_timestamp;\n        if (str_title)\n            av_strlcpy(oc->title, str_title, sizeof(oc->title));\n        if (str_author)\n            av_strlcpy(oc->author, str_author, sizeof(oc->author));\n        if (str_copyright)\n            av_strlcpy(oc->copyright, str_copyright, sizeof(oc->copyright));\n        if (str_comment)\n            av_strlcpy(oc->comment, str_comment, sizeof(oc->comment));\n        if (str_album)\n            av_strlcpy(oc->album, str_album, sizeof(oc->album));\n        if (str_genre)\n            av_strlcpy(oc->genre, str_genre, sizeof(oc->genre));\n    }\n    output_files[nb_output_files++] = oc;\n    if (oc->oformat->flags & AVFMT_NEEDNUMBER) {\n        if (!av_filename_number_test(oc->filename)) {\n            print_error(oc->filename, AVERROR_NUMEXPECTED);\n            exit(1);\n        }\n    }\n    if (!(oc->oformat->flags & AVFMT_NOFILE)) {\n        if (!file_overwrite &&\n            (strchr(filename, \':\') == NULL ||\n             filename[1] == \':\' ||\n             av_strstart(filename, "file:", NULL))) {\n            if (url_exist(filename)) {\n                int c;\n                if ( !using_stdin ) {\n                    fprintf(stderr,"File \'%s\' already exists. Overwrite ? [y/N] ", filename);\n                    fflush(stderr);\n                    c = getchar();\n                    if (toupper(c) != \'Y\') {\n                        fprintf(stderr, "Not overwriting - exiting\\n");\n                        exit(1);\n                    }\n                                }\n                                else {\n                    fprintf(stderr,"File \'%s\' already exists. Exiting.\\n", filename);\n                    exit(1);\n                                }\n            }\n        }\n        if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {\n            fprintf(stderr, "Could not open \'%s\'\\n", filename);\n            exit(1);\n        }\n    }\n    memset(ap, 0, sizeof(*ap));\n    if (av_set_parameters(oc, ap) < 0) {\n        fprintf(stderr, "%s: Invalid encoding parameters\\n",\n                oc->filename);\n        exit(1);\n    }\n    oc->preload= (int)(mux_preload*AV_TIME_BASE);\n    oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);\n    oc->loop_output = loop_output;\n    for(i=0; i<opt_name_count; i++){\n        char buf[256];\n        const AVOption *opt;\n        const char *str= av_get_string(avformat_opts, opt_names[i], &opt, buf, sizeof(buf));\n        if(str && (opt->flags & AV_OPT_FLAG_ENCODING_PARAM))\n            av_set_string(oc, opt_names[i], str);\n    }\n    file_oformat = NULL;\n    file_iformat = NULL;\n}', 'static void new_video_stream(AVFormatContext *oc)\n{\n    AVStream *st;\n    AVCodecContext *video_enc;\n    int codec_id;\n    st = av_new_stream(oc, oc->nb_streams);\n    if (!st) {\n        fprintf(stderr, "Could not alloc stream\\n");\n        exit(1);\n    }\n    avcodec_get_context_defaults2(st->codec, CODEC_TYPE_VIDEO);\n    bitstream_filters[nb_output_files][oc->nb_streams - 1]= video_bitstream_filters;\n    video_bitstream_filters= NULL;\n    if(thread_count>1)\n        avcodec_thread_init(st->codec, thread_count);\n    video_enc = st->codec;\n    if(video_codec_tag)\n        video_enc->codec_tag= video_codec_tag;\n    if(   (video_global_header&1)\n       || (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))){\n        video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;\n        avctx_opts[CODEC_TYPE_VIDEO]->flags|= CODEC_FLAG_GLOBAL_HEADER;\n    }\n    if(video_global_header&2){\n        video_enc->flags2 |= CODEC_FLAG2_LOCAL_HEADER;\n        avctx_opts[CODEC_TYPE_VIDEO]->flags2|= CODEC_FLAG2_LOCAL_HEADER;\n    }\n    if (video_stream_copy) {\n        st->stream_copy = 1;\n        video_enc->codec_type = CODEC_TYPE_VIDEO;\n    } else {\n        const char *p;\n        int i;\n        AVCodec *codec;\n        AVRational fps= frame_rate.num ? frame_rate : (AVRational){25,1};\n        codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_VIDEO);\n        if (video_codec_name)\n            codec_id = find_codec_or_die(video_codec_name, CODEC_TYPE_VIDEO, 1);\n        video_enc->codec_id = codec_id;\n        codec = avcodec_find_encoder(codec_id);\n        for(i=0; i<opt_name_count; i++){\n            char buf[256];\n            const AVOption *opt;\n            const char *str= av_get_string(avctx_opts[CODEC_TYPE_VIDEO], opt_names[i], &opt, buf, sizeof(buf));\n            if(str && (opt->flags & AV_OPT_FLAG_VIDEO_PARAM) && (opt->flags & AV_OPT_FLAG_ENCODING_PARAM))\n                av_set_string(video_enc, opt_names[i], str);\n        }\n        video_enc->time_base.den = fps.num;\n        video_enc->time_base.num = fps.den;\n        if(codec && codec->supported_framerates){\n            const AVRational *p= codec->supported_framerates;\n            const AVRational *best=NULL;\n            AVRational best_error= (AVRational){INT_MAX, 1};\n            for(; p->den!=0; p++){\n                AVRational error= av_sub_q(fps, *p);\n                if(error.num <0) error.num *= -1;\n                if(av_cmp_q(error, best_error) < 0){\n                    best_error= error;\n                    best= p;\n                }\n            }\n            video_enc->time_base.den= best->num;\n            video_enc->time_base.num= best->den;\n        }\n        video_enc->width = frame_width + frame_padright + frame_padleft;\n        video_enc->height = frame_height + frame_padtop + frame_padbottom;\n        video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*video_enc->height/video_enc->width, 255);\n        video_enc->pix_fmt = frame_pix_fmt;\n        if(codec && codec->pix_fmts){\n            const enum PixelFormat *p= codec->pix_fmts;\n            for(; *p!=-1; p++){\n                if(*p == video_enc->pix_fmt)\n                    break;\n            }\n            if(*p == -1)\n                video_enc->pix_fmt = codec->pix_fmts[0];\n        }\n        if (intra_only)\n            video_enc->gop_size = 0;\n        if (video_qscale || same_quality) {\n            video_enc->flags |= CODEC_FLAG_QSCALE;\n            video_enc->global_quality=\n                st->quality = FF_QP2LAMBDA * video_qscale;\n        }\n        if(intra_matrix)\n            video_enc->intra_matrix = intra_matrix;\n        if(inter_matrix)\n            video_enc->inter_matrix = inter_matrix;\n        video_enc->max_qdiff = video_qdiff;\n        video_enc->thread_count = thread_count;\n        p= video_rc_override_string;\n        for(i=0; p; i++){\n            int start, end, q;\n            int e=sscanf(p, "%d,%d,%d", &start, &end, &q);\n            if(e!=3){\n                fprintf(stderr, "error parsing rc_override\\n");\n                exit(1);\n            }\n            video_enc->rc_override=\n                av_realloc(video_enc->rc_override,\n                           sizeof(RcOverride)*(i+1));\n            video_enc->rc_override[i].start_frame= start;\n            video_enc->rc_override[i].end_frame  = end;\n            if(q>0){\n                video_enc->rc_override[i].qscale= q;\n                video_enc->rc_override[i].quality_factor= 1.0;\n            }\n            else{\n                video_enc->rc_override[i].qscale= 0;\n                video_enc->rc_override[i].quality_factor= -q/100.0;\n            }\n            p= strchr(p, \'/\');\n            if(p) p++;\n        }\n        video_enc->rc_override_count=i;\n        if (!video_enc->rc_initial_buffer_occupancy)\n            video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size*3/4;\n        video_enc->me_threshold= me_threshold;\n        video_enc->intra_dc_precision= intra_dc_precision - 8;\n        video_enc->strict_std_compliance = strict;\n        if (do_psnr)\n            video_enc->flags|= CODEC_FLAG_PSNR;\n        if (do_pass) {\n            if (do_pass == 1) {\n                video_enc->flags |= CODEC_FLAG_PASS1;\n            } else {\n                video_enc->flags |= CODEC_FLAG_PASS2;\n            }\n        }\n    }\n    video_disable = 0;\n    av_freep(&video_codec_name);\n    video_stream_copy = 0;\n}', 'AVStream *av_new_stream(AVFormatContext *s, int id)\n{\n    AVStream *st;\n    int i;\n    if (s->nb_streams >= MAX_STREAMS)\n        return NULL;\n    st = av_mallocz(sizeof(AVStream));\n    if (!st)\n        return NULL;\n    st->codec= avcodec_alloc_context();\n    if (s->iformat) {\n        st->codec->bit_rate = 0;\n    }\n    st->index = s->nb_streams;\n    st->id = id;\n    st->start_time = AV_NOPTS_VALUE;\n    st->duration = AV_NOPTS_VALUE;\n    st->cur_dts = AV_NOPTS_VALUE;\n    st->first_dts = AV_NOPTS_VALUE;\n    av_set_pts_info(st, 33, 1, 90000);\n    st->last_IP_pts = AV_NOPTS_VALUE;\n    for(i=0; i<MAX_REORDER_DELAY+1; i++)\n        st->pts_buffer[i]= AV_NOPTS_VALUE;\n    s->streams[s->nb_streams++] = st;\n    return st;\n}']