我正在开发基于 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, ())