FFmpeg-录制MP4文件

初始化mp4文件信息,设置帧率:

AVFormatContext *oc;
AVFormatContext *oc;
int ffp_record_mp4_file_init(FFPlayer *ffp,const char* filename)
{

    int i,ret;
    VideoState      *is = ffp->is;
    AVFormatContext *mp4_ic = NULL;
    AVOutputFormat *fmt = NULL;
    AVDictionary *opt=NULL;

    if(isStartRecordMp4File){
        printf( "start already \n");
        return -1;
    }

    av_dict_set_int(&opt,"video_track_timescale",30,0);
    if (!is){
        printf( "VideoState is null \n");
        return 0;
    }
        
    mp4_ic = is->ic;
    if (!mp4_ic){
         printf( "mp4_ic is null \n");
        return 0;
    }
    
     /* allocate the output media context */  
    avformat_alloc_output_context2(&oc, NULL, NULL, filename);
    if(!oc)  
    {  
        printf( "Failed avformat_alloc_output_context2\n");  
        return 0; //failed
    }
   
    fmt = oc->oformat; 

    for (i = 0; i < mp4_ic->nb_streams; i++) {  
        //Create output AVStream according to input AVStream  
        AVStream *in_stream = mp4_ic->streams[i];  
        AVStream *out_stream = avformat_new_stream(oc, in_stream->codec->codec);  
        if (!out_stream) {  
            printf( "Failed allocating output stream\n");  
            ret = AVERROR_UNKNOWN;  
            return 0;  
        }  
        printf( "avcodec_copy_context\n");
        //Copy the settings of AVCodecContext  
        ret = avcodec_copy_context(out_stream->codec, in_stream->codec);  
        if (ret < 0) {  
            printf( "Failed to copy context from input to output stream codec context\n");  
            return 0;  
        }  
        out_stream->codec->codec_tag = 0;  
        if (fmt->flags & AVFMT_GLOBALHEADER)  
            out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;  
        printf( "oc->oformat->flags\n");
    }  
    
    av_dump_format(oc, 0, filename, 1); 

    if (avio_open(&(oc->pb), filename, AVIO_FLAG_READ_WRITE) < 0)  
    {  
         printf( "Failed avio_open\n");  
        return 0;  
    }  
     /* write the stream header, if any */  
    if(avformat_write_header(oc, &opt) < 0){
        printf( "Failed avformat_write_header\n");  
        return 0;
    } 
    isStartRecordMp4File = true;

    return 1;

写入每帧数据:

void ffp_record_mp4_write_media_frame(AVFormatContext *ic,AVPacket *pkt)
void ffp_record_mp4_write_media_frame(AVFormatContext *ic,AVPacket *pkt)
{


    AVPacket pkt_temp;
    av_copy_packet(&pkt_temp, pkt);
    AVStream *in_stream, *out_stream; 
    in_stream  = ic->streams[pkt->stream_index];  
    out_stream = oc->streams[pkt->stream_index];  
    /* copy packet */  
    //Convert PTS/DTS  
    pkt_temp.pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF);  
    pkt_temp.dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF);  
    pkt_temp.duration = av_rescale_q(pkt->duration, in_stream->time_base, out_stream->time_base);  
    pkt_temp.pos = -1;  
    int ret = -1;
    ret = av_interleaved_write_frame(oc, &pkt_temp);
    if (ret < 0)
    {
        printf("write frame failed");
    }  

}
//lijk
int ffp_record_mp4_close(FFPlayer *ffp)
{
    if(!isStartRecordMp4File)
        return -1;

    isStartRecordMp4File = false;
    isIFrame = false;
    waitkey = -1;

    if (oc)  
        av_write_trailer(oc);  
    if (oc && !(oc->oformat->flags & AVFMT_NOFILE))  
        avio_close(oc->pb);  
    if (oc)  
    {  
        avformat_free_context(oc);  
        oc = NULL;  
    }
    return 1;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值