ffmpeg封装h264裸流为mp4文件,视频播放速度特别慢-解决

计算实际帧率时,需要从mp4的moov box中的mvhd中取得timescale和duration,将两者相除得到文件总时长,

再从stsz box中取得sample总数,即总帧数sample_count,用总帧数除以总时长即平均帧率,计算公式为

frame_rate = sample_count/(duration/timescale)


解决方法:通过查询mp4帧率计算得到答案:

AVDictionary* opt = NULL;

av_dict_set(&opt, "video_track_timescale", "90000", 0);


备注:

AVDictionary *opt=NULL;
av_dict_set_int(&opt,"video_track_timescale",25,0);
ret=avformat_write_header(ofmt_ctx,&opt);
而且pts只能+1递增。时间基没有设置也能十分正常播放。

timescale指一秒钟按时钟滴答数计算是多少,如果你设置1000,则一秒钟是以1000计算,你设置成25,那一秒钟就以25计算,所以你pts只能+1。 因为你设置了video_track_timescale所以不需要设置时间基

保存帧:

int mp4savePacket(int status,AVPacket *tmppacket){

            tmppacket->flags |= AV_PKT_FLAG_KEY;
            tmppacket->pts +=last_pts;
           // tmppacket->pts = av_rescale_q((frame_num)*30000, outfmt_ctx->streams[0]->codec->time_base,outfmt_ctx->streams[0]->time_base);//替换了
            frame_num++;
            tmppacket->dts += last_dts;
            tmppacket->stream_index = 0;
            tmppacket->duration=1;
            LOGE(3,"record_status ok,record_status = %d frame index %d %d %d\n",record_status,frame_num, tmppacket->duration,tmppacket->pts);

            //LOGE(3,"mpacket, 16Byte = %d,%d ",tmppacket.data[0],tmppacket.data[1]);
            av_interleaved_write_frame(outfmt_ctx, tmppacket);
            av_free_packet(tmppacket);

你可能感兴趣的:(视频编解码)