问题描述
我正在开发一个应用程序,使用Qt Designer工具和C ++中的V4L2和ffmpeg库从Linux中的网络摄像头捕获视频,以捕获图像,使用lib V4L2没有问题,并且因为图像准备好了,我把它发送到基于ffmpeg libs的编码器,最初编码器创建一个视频文件,并且它接收到的图像来编码它在这个文件中,我的问题如下:编码通常是完成的,但如果我开始播放录制的视频文件,速度似乎比普通速度加快!那么什么是清楚的,问题在于视频编码,我的问题是,是否有一种方法或功能来管理ffmpeg编码速度图片
感谢您的帮助。
使用FFMpeg创建自定义编码视频时,您实际上需要将每个AVPacket上的PTS设置为写入输出文件。设置您的AVCodecContext的time_base只会告诉容器什么期望。 PTS(演示时间戳)告诉解码器(当您查看视频时)何时实际显示该特定帧。
例如:
我有一个AVFrame,我从FFMpeg的V4L2部分获得。要开始,使用av_picture_copy制作此映像的副本更为安全。 (所以编码器不看AVFrame结构中的所有额外的信息。)
av_picture_copy((AVPicture *)图片, (AVPicture *)pFrame,c-> pix_fmt,c-> width,c-> height);
picture-> pts = numFrames;
现在根据编码框架数设置点数
pDestFrame-> pts = numFrames;
现在编码
code> numEncodedBytes = avcodec_encode_video(AVCodecCtx,buffer,bufferSize,pDestFrame);
现在创建一个AVPacket并重新设置时间戳,重新调整后
AVPacket pkt;
av_init_packet(& pkt);
pkt.pts = av_rescale_q(c-> coded_frame-> pts,c-> time_base,mpVideoStr-> time_base);
if(c-> coded_frame-> key_frame)pkt.flags | = AV_PKT_FLAG_KEY;
pkt.stream_index = mpVideoStr-> index;
pkt.data = outbuf;
pkt.size = out_size;
最后,你可以写出包
av_write_frame(formatCtx,& pkt);
我知道这适用于H264视频编码,但我不是100%肯定它适用于其他视频的类型,因为我写的时候只关心H264。
I am currently developing an application that makes capturing video from a webcam on Linux using the Qt Designer tool and V4L2 and ffmpeg libraries under C + +, to capture the image there is no problem using lib V4L2, and since That a picture is ready I send it to the encoder which is based on ffmpeg libs, initially the encoder creates a video file, and it receives images to encode it in this file, my problem is as follows: the encoding is normally done, but after if I start playing the recorded video file, speed appears to be accelerated compared to the regular speed! So what is clear, the problem is in video encoding,my question is, is there a method or function that manages ffmpeg encoding speed pictures ?????thank you for your help.
When creating a custom encoded video with FFMpeg, you'll actually need to set the PTS on each AVPacket that gets written to the output file. Setting the time_base of your AVCodecContext will only tell the container what to expect. The PTS (Presentation Time Stamp) tells the decoder (when you view your video) when to actually display that particular frame.
For example:
I have an AVFrame that i got from the V4L2 part of FFMpeg. To start, its safer to make a copy of this image using av_picture_copy. (So the encoder does not look at all the extra info in the AVFrame struct.)
av_picture_copy( (AVPicture*) picture, (AVPicture*) pFrame, c->pix_fmt, c->width, c->height );
picture->pts = numFrames;
now set the pts based on num of encoded frames
pDestFrame->pts = numFrames;
now encode
numEncodedBytes = avcodec_encode_video( AVCodecCtx, buffer, bufferSize, pDestFrame );
NOW Create an AVPacket and set the timestamp again, after rescaling
AVPacket pkt;
av_init_packet(&pkt);
pkt.pts = av_rescale_q( c->coded_frame->pts, c->time_base, mpVideoStr->time_base );
if ( c->coded_frame->key_frame ) pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index = mpVideoStr->index;
pkt.data = outbuf;
pkt.size = out_size;
Finally, you can write the packet out
av_write_frame( formatCtx, &pkt );
I know this works for H264 video encoding, but I'm not 100% sure it works for other types of video, since i was only concerned with H264 when I wrote this.
这篇关于ffmpeg速度编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!