ffmpeg speed encoding problem

2019-04-15 14:42发布

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.

3条回答
SAY GOODBYE
2楼-- · 2019-04-15 15:22

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.

查看更多
趁早两清
3楼-- · 2019-04-15 15:29

When you create the ffmpeg file you have to specify the frame rate depends on which library you are using but look for something like

pCodecContext->time_base.den = frames_per_second;
pCodecContext->time_base.num = 1;

Otherwise if you are generating 10-15fps from a webcam but the default in the file is 30 fps it will play back to fast.

See http://code.google.com/p/qtffmpegwrapper/ for a Qt ffmpeg wrapper

查看更多
成全新的幸福
4楼-- · 2019-04-15 15:37

I think you need to add time stamps to your pictures.
FFMpeg will do the encoding/decoding as fast as possible. You need to write a synchronization yourself. Usually in video decoding and playback you have timestamps attached to your frames or at least you can create some with the audio clock and your frame rate.
But this highly depends on how you want to sync and how you implemented it.
Maybe the tutorial of FFMpeg gives you some additionl hints

查看更多
登录 后发表回答