Skip to content

Instantly share code, notes, and snippets.

@caseyanderson
Last active February 23, 2018 21:51
Show Gist options
  • Save caseyanderson/18f0c7bb87ec823011c0aa105d2c1105 to your computer and use it in GitHub Desktop.
Save caseyanderson/18f0c7bb87ec823011c0aa105d2c1105 to your computer and use it in GitHub Desktop.
control media via python3 on raspberry pi

misc media control

Required Equipment:

Bookmark the following repositories:

Trigger Audio Recording via gpiozero

Using a digital input, one can trigger arecord to record for a particular duration. Go here to view the code.

This code allows one to set and trigger a fixed duration for recording (defaults to 30 [note: the unit of measure is seconds here]) via some button press. It's worth pointing out that this exact same code could be used with any other digital input device with no alterations.

To use this code we need to separate files. One, video_player.py provides the class information for Recorder, which uses a Python library called subprocess to wrap simple command line commands in Python code.

pi video control

The pi video control repository contains a number of examples for controlling video playback, and recording.

Most of the examples should require little-to-no extra steps to use with the exception of threshold_video_trig. A description of the steps necessary to use this code follows:

  1. turn on i2c in raspi-config: sudo raspi-config, select Interfacing Options, select I2C, click Yes to enable.

  2. download i2c-tools: sudo apt-get install -y i2c-tools

  3. reboot for changes to take effect: sudo reboot now

  4. Connect the ADC to the Pi as follows:

    • ADS1x15 VDD to Raspberry Pi 3.3V
    • ADS1x15 GND to Raspberry Pi GND
    • ADS1x15 SCL to Raspberry Pi SCL
    • ADS1x15 SDA to Raspberry Pi SDA
  5. Test to confirm that the pi sees your i2c device: sudo i2cdetect -y 1 If everything is working you should see something like this:

    0 1 2 3 4 5 6 7 8 9 a b c d e f

    00: -- -- -- -- -- -- -- -- -- -- -- -- --

    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --

    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

    70: -- -- -- -- -- -- -- --

  6. Install the adafruit-ads1x15 library from pip3: sudo pip3 install adafruit-ads1x15

  7. Clone the github library so we have access to Adafruit's examples: git clone https://github.com/adafruit/Adafruit_Python_ADS1x15.git

  8. cd into the examples directory

  9. Make simpletest.py executable: sudo chmod +x simpletest.py

  10. Confirm functionality by running simpletest.py with Python3 (apparently we need to use sudo here): sudo python3 simpletest.py

  11. Ctl-C to exit once you have confirmed that this works.

putting everything together

The following example repurposes trig_play.py from the pi video control to trigger video playback when an analog sensor passes a fixed threshold:

#!/usr/bin/python3

'''
trigger video playback when analog sensor crosses a value
'''

from video_player import *
import time
import Adafruit_ADS1x15

BASE_DIR = '/home/cta/'
FILENAME =  'dramatic_chipmunk.mp4'

play_path = ''.join([BASE_DIR, FILENAME])

play = Player(play_path)
adc = Adafruit_ADS1x15.ADS1015()

GAIN = 1
threshold = 500
is_playing = False

try:
    print("reading sensor values!")
    while True:
        sensor_val = adc.read_adc(0, gain=GAIN)
        if is_playing == False and sensor_val <= threshold:
            print('threshold crossed, starting video')
            play.play()
            is_playing = True
        elif is_playing == True:
            if play.status() == 'done':
                print(''.join(['done', '\n', '\n']))
                is_playing = False
except KeyboardInterrupt:
    print(''.join([ '\n', '\n', 'INTERRUPTED', '\n']))
    if play.status() == 'playing':
        print('video is running, terminating now!')
        play.kill()
    else:
        print('no video running, exiting now')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment