Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active November 23, 2016 14:22
Show Gist options
  • Save ZiTAL/4293ae4130c0174af095 to your computer and use it in GitHub Desktop.
Save ZiTAL/4293ae4130c0174af095 to your computer and use it in GitHub Desktop.
Raspberry Pi video launcher
#!/usr/bin/python2
# -*- coding: utf-8 -*-
from sys import exit, argv
from os import walk, path, system, popen, path
from glob import glob
import re
# allowed extensions
ext_allow = ['mp4', 'avi', 'webm', 'ogv', 'ogg', 'mkv', 'flv', 'vob', 'mov', 'wmv', 'm4v', 'mpg', '3gp']
# get folder from command line
try:
if argv[1]:
if path.isdir(argv[1]) != False:
folder = argv[1]
except:
folder = path.expanduser("~")
# omxplayer command "-o" -> output mode, "r" -> fullscreen
command = "omxplayer -o hdmi -r"
# pagination
pagination = 10
# show menu function
def showMenu(videos):
print ""
print "{Number}: Play Video, 1 for the first, 2 for second... "
print "{Any key}: More videos"
print "c: close playing video"
print "q: quit"
r = raw_input('Insert Option:').lower()
print ''
if(r.isdigit()):
index = int(r)-1
try:
v = videos[index]
cmd = command+' "'+v+'"'
print "Command: "+cmd
system(cmd)
exit()
except IndexError:
print "Index: "+r+" no exists"
exit()
if(r=='c'):
close()
exit()
if(r=='q'):
exit()
def close():
process = popen("ps -A | grep omxplayer").read()
lines = re.split("\n", process)
# get pids of omxplayer
pids = []
for line in lines:
p = re.compile("^\s*([0-9]+)")
r = p.match(line)
if(r):
pids.append(str(r.group(1)))
cmd = 'kill -9 '+' '.join(pids)
print "Command: "+cmd
system(cmd)
exit()
def getFiles(folder):
files = []
for x in walk(folder):
a = glob(path.join(x[0], '*'))
for b in a:
files.append(b)
return files
def filterFiles(files, ext):
# get videos from files
filter_files = []
for file in files:
# match file extensions
for ea in ext_allow:
p = re.compile("\."+ea+"$", re.IGNORECASE)
if(p.search(file)!=None):
filter_files.append(file)
# order files by name
filter_files.sort()
return filter_files
# get files
files = getFiles(folder)
# filter files by extension
videos = filterFiles(files, ext_allow)
# header
print ''
print "Raspberry Pi video launcher"
print "==========================="
if len(videos)>0:
# show video list
for i in videos:
show = False
print str(videos.index(i)+1)+": "+i
if (videos.index(i)+1)%pagination==0:
show = True
showMenu(videos)
if show == False:
showMenu(videos)
else:
print "There is no multimedia resource in: "+folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment