0

I am using RPi 2 Model B with a RPi camera module. I'm coding on Python and need to record a video while operating a motor. The code I'm working with looks something like this

with picamera.PiCamera() as camera:
    camera.start_recording('test.h264')
    for i in range(0,2001):
        *Code to run the motor*
    camera.stop_recording()

I'm not sure where to place the camera.wait_recording()command. I need the code to start recording, run the motor and then end recording.

Edit: Ok, what seems to be working is the following code

with picamera.PiCamera() as camera:
camera.start_recording('test.h264')
for i in range(0,2001):
    *Code to run the motor*
     camera.wait_recording()
camera.stop_recording()

So, basically added the camera.wait_recording() command in the for loop. Though I'm not sure if the recording begins exactly when the for loop starts and ends exactly when the for loop ends. Also not sure if this is achieving the desired result. If somebody could shed some light on this I'd be thankful.

  • depends on what the loop does. If you want to record while the loop is running, place the line above the for-loop. – 576i Mar 18 '15 at 8:03
  • @576i- yes I do want to record while the loop is running. But what argument do I enter into the wait command or do I leave it blank? – Vishaal Devanaboyina Mar 18 '15 at 10:32
  • Because it says here that the camera.wait_recording() command immediately returns if there is nothing passed, right? – Vishaal Devanaboyina Mar 18 '15 at 10:41
1

Though I'm not sure if the recording begins exactly when the for loop starts

The recording starts when you call start_recording(). The docs are pretty explicit about that.

and ends exactly when the for loop ends

The recording stops when you call stop_recording(), or when something goes wrong.

The purpose of wait_recording() is to check that the recording process hasn't stopped due to error, which will otherwise go unnoticed until you call stop_recording(). It apparently will throw an exception. That being the case, you probably want to wrap it properly:

try:
    camera.wait_recording()
except Exception:
    # Deal with it.

There's a bit of discussion in the comments below about exactly which forms of Exception that might be.

  • Spot on! As to what exception might be thrown - it largely depends on what you're writing to (IOError if it's a file, SocketError if it's a network socket, etc). Basically wait_recording is just a sync method to transfer an exception from the background recording thread to the calling thread. If nothing's gone wrong then it's just a wait-state on an event. – Dave Jones Mar 19 '15 at 11:06
  • @DaveJones Do you know for a fact those exceptions can be thrown here? The docs for that leave it up to the imagination, but when I clicked through to the source I notice the call is really a try/except which then throws PiCameraNotRecording -- but looking at it again now I realized the meat of the call is probably the subsequent emitter.wait() which could throw anything (I'm actually not a python user and did not go further; try/except/else is a strange looking sequence to me).... – goldilocks Mar 19 '15 at 13:27
  • ...So I've changed it to the most general case above but if you are sure which ones can come from lower down I'll add that into the answer. – goldilocks Mar 19 '15 at 13:28
  • 1
    The PiCameraNotRecording exception is thrown if you call wait_recording on a splitter port which isn't active (so it's a boring case basically). The more important cases are thrown by encoder.wait; this simply grabs any exception stored by the callback and raises it (in the main thread because that's where it's being called from) – Dave Jones Mar 19 '15 at 13:31
  • I should add that as soon as the encoder callback encounters an exception it stores it (for later raising by wait) and then terminates the recording. So, yeah, it can be just about any exception at all depending on the output (and the output can be "anything with a write() method", so the scope is pretty wide :) – Dave Jones Mar 19 '15 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.