[Libav-user] libavformat, RTSP: periodically read last available frame of live stream

Alex Cohn alexcohn at netvision.net.il
Mon Sep 9 22:33:09 CEST 2013


On Sep 8, 2013 8:15 PM, "Sandro Mani" <manisandro at gmail.com> wrote:
>
> On 08.09.2013 18:32, Alex Cohn wrote:
>>
>> On Sep 8, 2013 6:17 PM, "Sandro Mani" <manisandro at gmail.com> wrote:
>> >
>> > Hello,
>> >
>> > I would like to periodically (say every second) grab the latest frame from a rtsp live stream of a webcam. I am able to successfully open the stream and read and decode frames, however if I read one frame every second, I am still reading subsequent frames (as opposed to frames which are i.e. one second apart). I guess I need to seek to the last available frame before reading the next frame. Can anyone point out how this can be done?
>> >
>> > For reference, my code is here: [1] [2].
>> >
>> > Thanks for any inputs.
>> >
>> > Sandro
>> >
>> >
>> > [1] http://smani.fedorapeople.org/VideoCapture.hpp
>> > [2] http://smani.fedorapeople.org/VideoCapture.cpp
>>
>> If your webcam has GOP of 1 sec, e.g. GOP length = 30 and FPS = 30, you can skip to next second; otherwize, the correct strategy would be to decode all frames, but throw away all frames that you don't need.
>>
>> BR
>> Alex Cohn
>>
>>
> Thanks for the reply. With skip do you mean by calling av_seek_frame with an appropriately computed timestamp? (Sorry if I may be asking the obvious, I'm rather a novice in this area.)
>
> Sandro

This is a good question. I would suggest av_seek_frame with relevant
timestamp and flags=0 (i.e. only keyframes). But if your stream does
not support seeking, you can simply read incoming packets
(av_read_frame) until you reach the next key frame (when
AVPacket::flags is PKT_FLAG_KEY).

Good luck,
Alex Cohn


More information about the Libav-user mailing list