我正在研发一款基于Raspberry Pi的头戴式移动相机。它始终是录音。在按钮上按前两分钟,将保存以下一分钟,将删除缓冲区中剩余的录制文件。
我现在想要通过usb声卡同时录制视频和音频。为此,我相信我需要Python线程。
可悲的是,以下代码没有做任何事情。两个 while循环都按预期工作。但是在这个脚本中加入线程它们不会。终端只承认“sudo python x.py”并且没有回应。
感谢您的每一条评论!
#!/usr/bin/envpython
import subprocess
import datetime
import RPi.GPIO as GPIO
import time
from subprocess import call
from datetime import datetime
import os
import sys
import time
import thread
def blink(pin):
GPIO.output(pin,GPIO.HIGH)
time.sleep(0.5)
GPIO.output(pin,GPIO.LOW)
return
loop=True
def my_callback(channel): #linking the blink to the button press on pin8
blink(12)
return
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.IN) #set pin8 as receivable to button input
GPIO.add_event_detect(8, GPIO.FALLING, callback=my_callback) #button and LED are ready
def ongoingvideo():
while True:
GPIO.setup(12, GPIO.OUT) #pin12 powers LED
GPIO.output(12, False) #turn on LED
ts=datetime.now()
a= ts.strftime("%H%M%S") #a is time variable
filename = a #video is called after timestamp
print "Start recording of a: " + a + (threadname1)
call (["raspivid -t 60000 -b 15000000 -hf -vf -n -o /home/pi/recordings/vid/buffer/" + filename + ".h264"], shell=True)
if GPIO.event_detected(8):
ts=datetime.now()
b= ts.strftime("%H%M%S") #b is time variable
lastfilename = b #video is called after timestamp
print "Start recording of b: " + b
call (["raspivid -t 60000 -b 15000000 -hf -vf -n -o /home/pi/recordings/vid/buffer/" + lastfilename + ".h264"], shell=True)
files = os.listdir("/home/pi/recordings/vid/buffer/")
files.sort()
for x in files[-3:]: #"saving" the last 3 files of the endless buffer
call (["mv /home/pi/recordings/vid/buffer/" + x + " /home/pi/recordings/vid/fullhd30fps"], shell=True)
call (["rm /home/pi/recordings/vid/buffer/*"], shell=True) #deleting remaining buffer files to free disk space
call (["sudo fake-hwclock save"], shell=True)
def ongoingaudio():
while True:
ts=datetime.now()
d= ts.strftime("%H%M%S") #d is time variable
audioname = d #video is called after timestamp
print "Start recording of d: " + d + (threadname2)
call (["sudo arecord -D plughw:1,0 -f cd -d 60 /home/pi/recordings/audio/buffer/" + audioname + ".wav"], shell=True)
if GPIO.event_detected(8):
ts=datetime.now()
e= ts.strftime("%H%M%S") #e is time variable
lastaudioname = e #video is called after timestamp
print "Start recording of e: " + e
call (["sudo arecord -D plughw:1,0 -f cd -d 60 /home/pi/recordings/audio/buffer/" + lastaudioname + ".wav"], shell=True)
files = os.listdir("/home/pi/recordings/audio/buffer/")
files.sort()
for x in files[-3:]: #"saving" the last 3 files of the endless buffer
call (["mv /home/pi/recordings/audio/buffer/" + x + " /home/pi/recordings/audio/wav"], shell=True)
call (["rm /home/pi/recordings/audio/buffer/*"], shell=True) #deleting remaining buffer files to free disk space
thread.start_new_thread(ongoingvideo, ())
thread.start_new_thread(ongoingaudio, ())