我正在使用ffmpeg记录来自GDI(Windows屏幕录像机)的视频输入,以便稍后使用VLC(通过ActiveX插件)+ ffmpeg对其进行解码。
目前在视频中搜索不能通过插件在VLC中工作(这很关键)。 VLC播放器本身提供搜索,但它更像是字节位置搜索(在I帧上比其他帧大,它在水平滚动上做出更大的步骤,也没有时间戳)。
使用下一个默认值打开编码器:
avformat_alloc_output_context2(&outputContext, NULL, "mpegts", "test.mpg");
outputFormat = outputContext->oformat;
encoder = avcodec_find_encoder(AV_CODEC_ID_H264);
outputStream = avformat_new_stream(outputContext, encoder);
outputStream->id = outputContext->nb_streams - 1;
encoderContext = outputStream->codec;
encoderContext->bit_rate = bitrate; // 800000 by default
encoderContext->rc_max_rate = bitrate;
encoderContext->width = imageWidth; // 1920
encoderContext->height = imageHeight; // 1080
encoderContext->time_base.num = 1;
encoderContext->time_base.den = fps; // 25 by default
encoderContext->gop_size = fps;
encoderContext->keyint_min = fps;
encoderContext->max_b_frames = 0;
encoderContext->pix_fmt = AV_PIX_FMT_YUV420P;
outputStream->time_base = encoderContext->time_base;
avcodec_open2(encoderContext, encoder, NULL);
以这种方式录制:
// my impl of GDI recorder, returning AVFrame with only data and linesize filled.
AVFrame* tmp_frame = impl_->recorder->acquireFrame();
// converting RGB -> YUV420
sws_scale(impl_->scaleContext, tmp_frame->data, tmp_frame->linesize, 0, impl_->frame->height, impl_->frame->data, impl_->frame->linesize);
// pts variable is calculated by using QueryPerformanceCounter form WinAPI. It is strictly increasing
impl_->frame->pts = pts;
avcodec_encode_video2(impl_->encoderContext, impl_->packet, impl_->frame, &out_size);
if (out_size) {
impl_->packet->pts = pts;
impl_->packet->dts = pts;
impl_->packet->duration = 1; // here it is! It is set but has no effect
av_packet_rescale_ts(impl_->packet, impl_->encoderContext->time_base, impl_->outputStream->time_base);
// here pts = 3600*pts, dts = 3600*pts, duration = 3600 what I consider to be legit in terms of milliseconds
impl_->packet->stream_index = impl_->outputStream->index;
av_interleaved_write_frame(impl_->outputContext, impl_->packet);
av_packet_unref(impl_->packet);
out_size = 0;
}
ffprobe正在提供关于框架的下一个信息:
[FRAME]
media_type=video
stream_index=0
key_frame=1
pkt_pts=3600
pkt_pts_time=0:00:00.040000
pkt_dts=3600
pkt_dts_time=0:00:00.040000
best_effort_timestamp=3600
best_effort_timestamp_time=0:00:00.040000
pkt_duration=N/A
pkt_duration_time=N/A
pkt_pos=564
pkt_size=97.018555 Kibyte
width=1920
height=1080
pix_fmt=yuv420p
sample_aspect_ratio=N/A
pict_type=I
coded_picture_number=0
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
[/FRAME]
我认为问题出现在pkt_duration
变量中,尽管已设置。
我在录制中做错了所以我不能在视频中寻找?
P.S。在其他视频(也是h264)上寻求使用ActiveX VLC插件。
答案 0 :(得分:0)
绝对错误的是:
impl_->packet->pts = pts;
impl_->packet->dts = pts;
PTS和DTS不相等!如果你只有I帧,它们可能就是这种情况。此外,您的评论说: pts变量是使用QueryPerformanceCounter
格式WinAPI 计算的。如果您的帧速率不变,我相信它是,那么您不需要QueryPerformanceCounter
API。 PTS通常为90kHz单位。以90kHz表示的1帧的持续时间计算如下:
90000 x分母/分子
如果fps为25,则分子为25,分母为1.对于29.97 fps,分子为30000,分母为1001.每个新帧的PTS应增加该量(除非您丢帧)。关于DTS,编码器应提供该值。