title: Python Threading Audio + Video
body: <p>I am working on a head mounted mobile camera based on the Raspberry Pi. It is always recording. On button press the prior two minutes and the following one minute will be saved, remaining recorded files from the buffer will be deleted.</p>
  
  <p>I now want to record not only video but also audio simultaneously via usb soundcard. To do so I need Python threading, I believe. </p>
  
  <p>Sadly, the following code doesn't do anything. Both <strong>while loops</strong> on their one work as intended. But combined in this script as <strong>threading</strong> they won't. The terminal just acknowledges the "sudo python x.py" and responds nothing.</p>
  
  <p>Thanks for every comment!</p>
  
  <pre><code>#!/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, ())
  </code></pre>