Close

Starting Software Development Back Up

A project log for 90s Raspberry Camcorder

Upgrading a 1993 RCA CC540 VHS camcorder with a Raspberry Pi, HQ Camera module, modern storage, power, and features.

DustinDustin 11/29/2020 at 16:590 Comments

The first version of the code I posted is very crude and often errors out. It keeps saying there is no video recording when you go to stop the video. It only does this sometimes, not consistently. As I type this, I suspect that the "if" statement that interrupts the "while" loop is actually stopping the video stream before it runs the stop command, causing it to error out. I'll go test it out. Here's the original code:

------------------------------------------------------------------------------------------------------------------------------------------------------

#Install modules with python3: python3.7 -m pip install
import keyboard
from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'

while True:    
    if keyboard.is_pressed('r'):
        sleep(2)
        camera.resolution = (1920, 1080)
        camera.start_preview()
        camera.start_recording('/home/pi/Documents/test_files/button.h264', splitter_port=2)
    
    if keyboard.is_pressed('s'):    
        camera.stop_recording(splitter_port=2)
        camera.stop_preview()
        
    
    if keyboard.is_pressed('p'):
        sleep(2)
        camera.resolution = (2592, 1944)
        camera.start_preview()
        camera.capture('/home/pi/Documents/test_files/testpic.png')
        camera.stop_preview()
        
    if keyboard.is_pressed('x'):
        break
    sleep(.01)

I started up the software(to make sure I didn't break it after viewing it), and it worked as usual. I commented out the following line and tested again:

camera.stop_recording(splitter_port=2)

That helped disprove my theory that the interrupt was causing problems. When I hit the "s" button to stop recording, the program didn't error out, but instead the preview went away, as it should have. I tried to start recording again, with the "r" button, but got an error stating that it was already recording. This tells me that the "s" function didn't stop the recording, which would indicate it crashed it.I think I need to take a look at how the "sleep()" function interacts with the camera feed...

Another test, with original code, shows that the error still occurs most of the time. I wonder if splitter port 2 is being closed prematurely or something. 

I'll eliminate the while loop and if statements and see if it gives any errors:

from time import sleep

from picamera import PiCamera

camera = PiCamera()
camera.start_preview()
sleep(2)
camera.start_recording('/home/pi/Documents/test_files/testvid.h264')
sleep(5)
camera.stop_recording()
camera.stop_preview()

 That code worked fine, as expected. Now I'll try forcing splitter 2:

from time import sleep

from picamera import PiCamera

camera = PiCamera()
camera.start_preview()
sleep(2)
camera.start_recording('/home/pi/Documents/test_files/testvid.h264', splitter_port=2)
sleep(5)
camera.stop_recording(splitter_port=2)
camera.stop_preview()

 That also worked just fine. I really need to learn more about programming and Python... I'm pretty sure I searched this error message before, but I'll try it again. A search turned up nothing of use to me, so I guess I just have to figure it out myself. I guess I could add "try" to the while loop, and ignore exceptions. Not the proper way of doing things, but it has worked for me in the past. This exception is killing off the entire program, which is not a good thing when trying to run a camera. I can see this being a problem when you go to stop a recording, it crashes the program, and you have to fumble with it to capture some footage. I'd be pretty mad if I missed a shot due to that.

I figured it out. The following code works to ignore the error, as well as print out when it occurs, without interrupting the program:

#Install modules with python3: python3.7 -m pip install
import keyboard
from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'

while True:    
    if keyboard.is_pressed('r'):
        sleep(2)
        camera.resolution = (1920, 1080)
        camera.start_preview()
        camera.start_recording('/home/pi/Documents/test_files/button.h264', splitter_port=2)
    
    if keyboard.is_pressed('s'):
        try:    
            camera.stop_recording(splitter_port=2)
            camera.stop_preview()            
        except:
            print("Error")
            pass
        
    
    if keyboard.is_pressed('p'):
        sleep(2)
        camera.resolution = (2592, 1944)
        camera.start_preview()
        camera.capture('/home/pi/Documents/test_files/testpic.png')
        camera.stop_preview()
        
    if keyboard.is_pressed('x'):
        break
    sleep(.01)

 I modified the "s" portion, which stops the video recording, so that it would ignore the errors and move on:

    if keyboard.is_pressed('s'):
        try:    
            camera.stop_recording(splitter_port=2)
            camera.stop_preview()            
        except:
            print("Error")
            pass

The ACTUAL problem here is that the wireless keyboard/mouse/media remote thing is sending multiple keypresses after a while. I feel it took me way longer to figure this out than it should have. I'm quite happy though, as this means I can move on, and reminds me that I am capable of completing this project.

Seeing as the entire main loop needs to be running for the camera to operate, I think I'll put the entire code into a "try" situation, so that the damn thing can't crash on me:

#Install modules with python3: python3.7 -m pip install
import keyboard
from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'

while True: 
	try:   
		if keyboard.is_pressed('r'):
			sleep(2)
			camera.resolution = (1920, 1080)
			camera.start_preview()
			camera.start_recording('/home/pi/Documents/test_files/button.h264', splitter_port=2)
		
		if keyboard.is_pressed('s'):    
			camera.stop_recording(splitter_port=2)
			camera.stop_preview()
			
		
		if keyboard.is_pressed('p'):
			sleep(2)
			camera.resolution = (2592, 1944)
			camera.start_preview()
			camera.capture('/home/pi/Documents/test_files/testpic.png')
			camera.stop_preview()
			
		if keyboard.is_pressed('x'):
			break
		sleep(.01)
	except:
		pass

That actually worked great, first time. That didn't normally happen to me when first learning to code. All that time writing my video codec rename program, and tinkering with Arduinos has paid off. I at least now have a simple program that can record a video, take pictures, and exit without crashing. I even pressed the various command buttons a ton of times while various things were running, and couldn't interrupt it. That's perfect, as you never know what buttons you might accidentally hit.

That's all for this section. Next up, I'll be adding some more features, maybe like  starting the preview window as soon as the program opens, and keeping one open the entire time. Once that's working, I can display the output on whatever screen I choose as my viewfinder. 

Discussions