2,543
社区成员
发帖
与我相关
我的任务
分享
ret = av_read_frame(pFormatCtx, packet);
if( ret>=0){
if(packet->stream_index==videoindex){
CLogDebug( LogModuleName, "[FfmpegPlay.play hwnd:%d] av_read_frame get packet. packet.size:%d", playhwnd, packet->size);
//修改为3.x模式 解码函数
//将接收到的包发送给解码器进行解码处理
got_picture = avcodec_send_packet(pCodecCtx,packet);
//修改前代码
//ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
if (got_picture >=0)
{
// 录像相关处理(基于 packet 数据进行)
if (FlagstartVideo && !FlagstopVideo)
{
if (flag)
{
//const char *outfilename = "1.mp4";
const char *outfilename = m_RecordfilePath.c_str();
//o_fmt_ctx = avformat_alloc_context();
avformat_alloc_output_context2(&o_fmt_ctx, NULL, NULL, outfilename);
o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);
{
// 修改为 3.4模式 解码器上下文获取方式
avcodec_parameters_copy( o_video_stream->codecpar, i_video_stream->codecpar );
o_video_stream->time_base = i_video_stream->time_base;
o_video_stream->avg_frame_rate = i_video_stream->avg_frame_rate;
o_video_stream->r_frame_rate = i_video_stream->r_frame_rate;
o_video_stream->display_aspect_ratio = i_video_stream->display_aspect_ratio;
}
avio_open(&o_fmt_ctx->pb, outfilename, AVIO_FLAG_WRITE);
//AVDictionary* opt = NULL;
//av_dict_set_int(&opt, "video_track_timescale", 25, 0);
avformat_write_header(o_fmt_ctx, &opt);
flag = false;
}
packet->flags |= AV_PKT_FLAG_KEY;
pts = packet->pts;
packet->pts += last_pts;
dts = packet->dts;
packet->dts += last_dts;
packet->stream_index = 0;
av_interleaved_write_frame(o_fmt_ctx, packet);
}
if (FlagstopVideo && FlagstartVideo)
{
FlagstartVideo = false;
FlagstopVideo = false;
av_write_trailer(o_fmt_ctx);
flag = true;
av_freep(&o_fmt_ctx->streams[0]);
avio_close(o_fmt_ctx->pb);
//av_free(o_fmt_ctx);
avformat_free_context(o_fmt_ctx);
}
}
const char *outfilename = m_RecordfilePath.c_str();
avformat_alloc_output_context2(&o_fmt_ctx, NULL, NULL, outfilename);
o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);
octx = avcodec_alloc_context3(NULL);
avcodec_parameters_to_context(octx,i_video_stream->codecpar);
octx->time_base.num = i_video_stream->time_base.num;
octx->time_base.den = i_video_stream->time_base.den;
octx->pix_fmt = pCodecCtx->pix_fmt;
octx->flags = pCodecCtx->flags;
octx->flags |= CODEC_FLAG_GLOBAL_HEADER;
octx->codec_type = pCodecCtx->codec_type;
octx->bit_rate = pCodecCtx->bit_rate;
octx->me_range = pCodecCtx->me_range;
octx->max_qdiff = pCodecCtx->max_qdiff;
octx->qmin = pCodecCtx->qmin;
octx->qmax = pCodecCtx->qmax;
octx->qcompress = pCodecCtx->qcompress;
oc = avcodec_find_encoder( octx->codec_id );
int iret = avcodec_open2( octx, oc, NULL );
TRACE("======%d========\r\n",iret);
avcodec_parameters_from_context(o_video_stream->codecpar, octx);
av_stream_set_r_frame_rate(o_video_stream, i_video_stream->r_frame_rate);
avio_open(&o_fmt_ctx->pb, outfilename, AVIO_FLAG_WRITE);
AVDictionary* opt = NULL;
//av_dict_set_int(&opt, "video_track_timescale", 25, 0);
avformat_write_header(o_fmt_ctx, &opt);