2

The problem is that I can not exit the while loop without stopping the entire program.

When I execute the code on my Raspberry Pi, the camera starts recording, but when I want to end the video and press Ctrl+c, the entire program stops instead of continuing after the while loop. I thought the signal handler would catch the keyboard interrupt but it does not.

My code:

import picamera
import signal
from time import sleep
from subprocess import call

def signal_handler(signal, frame):
        global interrupted
        interrupted = True

signal.signal(signal.SIGINT, signal_handler)

interrupted = False

# Setup the camera
with picamera.PiCamera() as camera:
    camera.resolution = (1640, 922)
    camera.framerate = 30

    # Start recording
    camera.start_recording("pythonVideo.h264")

while True:
        sleep(0.5)
        print("still recording")

        if interrupted:
                print("Ctrl+C pressed")
                camera.stop_recording()
                break

# Stop recording
#camera.stop_recording()

# The camera is now closed.

print("We are going to convert the video.")
# Define the command we want to execute.
command = "MP4Box -add pythonVideo.h264 convertedVideo.mp4 -fps 30"
#Execute command.
call([command], shell=True)
# Video converted.
print("Video converted.")

What I tried:

bflag = True
while bflag ==True:
        sleep(0.5)
        print("still recording")

        if interrupted:
                print("Ctrl+C pressed")
                camera.stop_recording()
                bflag = False

Error:

pi@raspberrypi:~/Documents/useThisFolder $ python cookieVideo.py 
still recording
still recording
still recording
still recording
still recording
^Cstill recording
Ctrl+C pressed
Traceback (most recent call last):
  File "cookieVideo.py", line 29, in <module>
    camera.stop_recording()
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 1193, in stop_recording
'port %d' % splitter_port)
picamera.exc.PiCameraNotRecording: There is no recording in progress on port 1
2
  • replace the "while True" with while somevar, and change this variable to False when you want to exit the loop Commented Jun 14, 2017 at 20:38
  • 2
    There is no need for things like "Disclaimer: Absolute noob attempting to explain a question.". The onus of asking a proper question is on you regardless of status, and trust me when I say noobishness will show if it's there. No need to apologize for it, just take the time to listen and learn. Commented Jun 14, 2017 at 20:40

2 Answers 2

4

This would do it:

while True:
    try:
        sleep(0.5)
        print("still recording")
    except KeyboardInterrupt:
        print("Ctrl+C pressed")
        camera.stop_recording()
        break
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. However, when I implement your solution, ctrl+c does nothing. The while loop keeps going.
You need this code instead of the signal handler stuff.
If I use this code instead, I get the error mentioned in the problem description.
Ah, this needs to be within the with picamera.PiCamera() as camera block. I suspect recording stops automatically when you leave that block.
It is fixed. It was an indentation problem. I needed to use 4 spaces instead of tabs, because I am modifying the code using nano (I think that is the reason, don't know for sure). Thank you.
|
-1

enter image description here

As you can see it works perfectly with @mdurant's code.

2 Comments

When I try this it gives an error, which I added to the question description.
The code provided by @mdurant works perfectly for me, try it again and see that your indentation is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.