User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Time limit for circular buffer ?

Sat Aug 27, 2016 12:19 am

Hi, I'm interested in writing a Python script to record some video with a circular buffer, I've seen somewhere that there is a time limit (depending on video resolution) of 20 seconds, does the time limit only refer to the portion of video contained in the circular buffer? or does it refer to the length of the entire video? Ideally I want to record 5 seconds of video from the buffer and then 2 minutes of video after the trigger event - a button press.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23382
Joined: Sat Jul 30, 2011 7:41 pm

Re: Time limit for circular buffer ?

Sat Aug 27, 2016 10:11 am

Depends on the SW and the available memory. Work out how much on average a frame of your video takes when encoded to H264, divide that in to available memory. Presuming of course you want to keep in memory rather than flushing to SD card.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Time limit for circular buffer ?

Sat Aug 27, 2016 10:21 pm

Pardon my ignorance, was that a yes or a no? I intend to RECORD to a usb stick or an sd card with a resolution of 1980x1080, 25fps. ( 5 seconds before trigger event plus 2 minutes after event) All done with an A+.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23382
Joined: Sat Jul 30, 2011 7:41 pm

Re: Time limit for circular buffer ?

Sun Aug 28, 2016 6:19 pm

Yes, it's possible, with the right software. There was some circula buffer stuffa added to Raspistill, but I don;t how that compares with your requirements.

I'd be inclined to write my own code for the particular use case. Keep the last 5 seconds or so of H264 frames in memory, and output to SD cards once things start happening. You need to keep some idea of the particular H264 frame type (you can only start on an P-frame) so you know when the first one back 5 seconds is.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7146
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Time limit for circular buffer ?

Sun Aug 28, 2016 7:20 pm

Er, james, I think you meant that a stream can only be started at an I-frame, plus you'll want inline headers set so as to always get the headers in advance of any I-frame - that is what the raspivid (not raspistill) circular buffer code does.

@Ace Rimmer: How accurate does your 5 second preroll have to be? As above, any H264 stream has to start with an I-Frame, and the number of frames between I-frames is a configurable parameter. The lower the number, the more of your bitrate is consumed by I-frames and the lower the overall quality of the video, but the smaller the segment size that you can chop the stream into.
You could go with a Intra-I-frame period of 1 second (30 frames), and then you would be able to do 5-seconds +1/-0 seconds. To do that your circular buffer would have to store the position of the last 6 header frames, and ditch the data from the oldest 1 second section.
All quite possible, but you need to understand a small amount about how H264 works.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

paulhothersall
Posts: 28
Joined: Wed Jul 31, 2013 5:55 am

Re: Time limit for circular buffer ?

Sun Aug 28, 2016 9:29 pm

its more than possible,

the only "hard part" is actually keeping it frame accurate for the 5 second go back?

http://picamera.readthedocs.io/en/relea ... lar-stream gives some good info.

message back if you want a more detailed way of doing it than that. Especially if you want it to be closer to the 5 second accuracy

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Time limit for circular buffer ?

Sun Aug 28, 2016 10:15 pm

Thanks everyone. It looks like what I want to do is possible, the 5 seconds is not very critical. I'll think I'll just have to start with some basic code and take it from there.

@paulhothersall I may take you up on the offer of a PM if I get hopelessly stuck. I'm hoping that the trigger event can start the timer displaying in annotate_text.
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Time limit for circular buffer ?

Mon Aug 29, 2016 12:45 am

I tried to start with some very simple code...(copied from Pi documentation)

Code: Select all

import io
import random
import picamera

def motion_detected():
    # Randomly return True (like a fake motion detection routine)
    return random.randint(0, 10) == 0

camera = picamera.PiCamera()
stream = picamera.PiCameraCircularIO(camera, seconds=20)
camera.start_recording(stream, format='h264')
try:
    while True:
        camera.wait_recording(1)
        if motion_detected():
            # Keep recording for 10 seconds and only then write the
            # stream to disk
            camera.wait_recording(10)
            stream.copy_to('motion.h264')
finally:
    camera.stop_recording()
:( Unfortunately I end up with the following error...

Code: Select all

[email protected] ~ $ sudo python2 neeewww.py
Traceback (most recent call last):
  File "neeewww.py", line 19, in <module>
    stream.copy_to('motion.h264')
AttributeError: 'PiCameraCircularIO' object has no attribute 'copy_to'
[email protected] ~ $ 
:cry: Any ideas??
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Time limit for circular buffer ?

Mon Aug 29, 2016 5:59 am

:shock: Panic over. I cut and pasted some simpler code from the Pi documents and got it working, then copied the relevant lines to my script. After an hour or so I had it doing what I wanted it to. :) (If only I could get omxplayer to play at the right speed!!)
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23382
Joined: Sat Jul 30, 2011 7:41 pm

Re: Time limit for circular buffer ?

Tue Aug 30, 2016 9:09 am

6by9 wrote:Er, james, I think you meant that a stream can only be started at an I-frame, plus you'll want inline headers set so as to always get the headers in advance of any I-frame - that is what the raspivid (not raspistill) circular buffer code does.

@Ace Rimmer: How accurate does your 5 second preroll have to be? As above, any H264 stream has to start with an I-Frame, and the number of frames between I-frames is a configurable parameter. The lower the number, the more of your bitrate is consumed by I-frames and the lower the overall quality of the video, but the smaller the segment size that you can chop the stream into.
You could go with a Intra-I-frame period of 1 second (30 frames), and then you would be able to do 5-seconds +1/-0 seconds. To do that your circular buffer would have to store the position of the last 6 header frames, and ditch the data from the oldest 1 second section.
All quite possible, but you need to understand a small amount about how H264 works.
Yes, I definitely meant I frame. Soz. Brain fart. Good point on the inline headers too. Most of the code to do this is also in the segmented mode in Raspivid which has to split at the right point and use inline headers.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

User avatar
Ace Rimmer
Posts: 104
Joined: Wed May 27, 2015 1:51 am
Location: Wit's End

Re: Time limit for circular buffer ?

Fri Sep 02, 2016 10:44 pm

Ace Rimmer wrote:...... After an hour or so I had it doing what I wanted it to.......
It turns out I still need to record this video in 2 halves so I do need the "copy_to" but I always get
"AttributeError: PiCameraCircularIO object has no attribute 'copy_to' "

What does this mean? and how can I rectify it? :?
There must be an easier way!!!!!
I hope there is.....I haven't found it yet.

Return to “Camera board”