Looping videos listed on Google Drive via Python on Raspberry Pi
Longest title ever. For a recent (fun) project at Avant, I built out a video looper that plays videos of our CEO on a Raspberry Pi. The videos are read in from a Google Doc, and played with omxplayer. The loops aren’t completely seamless, which I know a lot of people want, but for my purposes this works perfectly fine!
Some of the hardest parts of this are actually setting up the Raspberry Pi. Unfortunately I won’t hit on all of these topics (Sorry! The details should be easy to find online!)
Here’s what you’ll need to install:
I used this touchscreen from Adafruit for my display.
Without further ado, here is the commented code dump to run the video looper!
import os import gspread # Must have a secret.py file with your credentials in it! import secret import glob from oauth2client.client import SignedJwtAssertionCredentials import socket import traceback from time import sleep from tendo import singleton # This makes sure we don't have multiple instances running me = singleton.SingleInstance() print 'No other instance running, starting looper' def checknetwork(): """ Checks if we have a network connection :return: """ ip = False try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('google.com', 0)) ip = s.getsockname() s.close() except socket.error: return False else: return ip def main(): """ Uses a quick loop to check the network connection :return: """ success = checknetwork() ct = 1 while not success and ct < 5: print "Waiting for connection..." success = checknetwork() sleep(3) ct += 1 return success # If Connection, go and look for new videos on the Google Sheet connection = main() if connection: try: # os.system('fbcp &') # Sign-in to Google, MUST HAVE SECRET.PY CREDENTIALS scope = ['https://spreadsheets.google.com/feeds'] credentials = SignedJwtAssertionCredentials(secret.drive_details['client_email'], secret.drive_details['private_key'], scope) gc = gspread.authorize(credentials) # The name of your google doc wks = gc.open("al") # Name of your worksheet wks = wks.worksheet("videos") recs = wks.get_all_records() # I gave a column header of Video URL col_header = 'Video URL' recs = [i[col_header] for i in recs] video_list = glob.glob('videos/*') for val in recs: print val print video_list if not any(val.split('=')[-1] in s for s in video_list): # Use youtube-dl to download the video os.system("youtube-dl -o 'videos/%%(title)s-%%(id)s.%%(ext)s\' %s" % val) except Exception: print traceback.format_exc() # Something wrong, continue on, need to alert the user to this # TODO: Alert admin pass else: print 'No Connection!' # Start Video Loop ct = 1 video_list = glob.glob('videos/*') os.system('fbcp &') # It will eventually stop running! 300 is how many loops I did, you can make it while True to never stop while ct < 300: ct += 1 for val in video_list[::-1]: os.system('omxplayer "%s"' % val) os.system('sudo killall fbcp')
Here’s a quick look at what my Google Doc looks like:
Code available Here