Last active
September 26, 2016 15:28
-
-
Save wirewc/6f6febac26ad5afc0e2292c99b9b84e6 to your computer and use it in GitHub Desktop.
Video Looper for a Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
# Summary: Created by request for a project involving looping through videos on a Raspberry Pi. | |
# | |
# Inside Joke: Created for the New Biebs on the Block | |
# | |
# | |
#Copyright (c) 2016 Will Christensen<wirewc@gmail.com> | |
# | |
#Permission is hereby granted, free of charge, to any person obtaining a copy | |
#of this software and associated documentation files (the "Software"), to deal | |
#in the Software without restriction, including without limitation the rights | |
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
#copies of the Software, and to permit persons to whom the Software is | |
#furnished to do so, subject to the following conditions: | |
# | |
#The above copyright notice and this permission notice shall be included in all | |
#copies or substantial portions of the Software. | |
# | |
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
#SOFTWARE. | |
""" | |
### Change Log: | |
### Created 20160924 | |
### | |
### Errata: | |
### Playlist file feature needs proper implementation. | |
### Clean up code (always!) | |
### Remote interface (maybe) | |
### Support more command line options of Mplayer | |
import sys,os,subprocess,random | |
HOME_DIR = os.getcwd() # Constant in case I want to get back to the home directory. | |
class MplayerSettings: | |
ASPECTS = {0:None,1:"-aspect 4:3 ",2:"-aspect 16:9"} | |
def __init__(self): | |
self.hres = 0 | |
self.vres = 0 | |
self.fs = False | |
self.forceScale = False | |
self.vsync = False | |
self.aspect = None | |
self.stretchX = 0 | |
self.stretchY = 0 | |
def SetVRes(self,val): | |
"""Set Vertical Resolution""" | |
self.vres = val | |
def SetHRes(self,val): | |
"""Set Horizontal Resolution""" | |
self.hres = val | |
def SetFS(self,val): | |
"""Set Fullscren, true or false""" | |
if val > 0: | |
self.fs = True | |
else: | |
self.fs = False | |
def SetForceScale(self,val): | |
"""Forces scaling if the video isn't scaled at fullscreen.""" | |
if val > 0: | |
self.forceScale = True | |
else: | |
self.forceScale = False | |
def SetVsync(self,val): | |
"""Sets vertical sync option""" | |
if val > 0: | |
self.vsync = True | |
else: | |
self.vsync = False | |
def SetAspect(self,val): | |
"""Set Aspect Ratio | |
ASPECTS = {0:None,1:"-aspect 4:3 ",2:"-aspect 16:9"}""" | |
try: | |
self.aspect = self.ASPECTS[val] | |
except: | |
slef.aspect = None | |
def StetchX(self,val): | |
"""Stretch X resolution value""" | |
self.stretchX = val | |
def StetchY(self,val): | |
"""Stretch Y resolution value""" | |
self.stretchY = val | |
def Launch(self): | |
"""Returns values in a executable string.""" | |
retval = "/usr/bin/mplayer " | |
if self.fs: | |
retval += "-fs " | |
if self.forceScale: | |
retval += "-vo xv " | |
if self.vsync: | |
retval += "-vsync " | |
if self.vres != 0 and self.hres != 0: | |
retval += "-vf expand="+str(self.hres)+":"+str(self.vres)+" " | |
if self.aspect != None: | |
retval += self.aspect | |
if self.stretchX != 0: | |
retval += "-x " + str(self.stretchX) + " " | |
if self.stretchY != 0: | |
retval += "-y " + str(self.stretchY) + " " | |
return retval | |
def __str__(self): | |
"""ToString method override""" | |
return self.Launch() | |
def __repr__(self): | |
return self.Launch() | |
class Mplayer: | |
def __init__(self,videofolder = "../video", playlist=None): | |
"""Mplayer class for playing videos | |
videofolder= name where videos reside. | |
playlist= optional explicit playlist""" | |
self.videofolder = videofolder | |
self.playlist = playlist | |
self.lastplayed = None | |
self.numVideo = 0 # Last video from playlist number | |
self.numOfVideos = 0 | |
self.mode = 1 | |
self.launchLine = None | |
self.extension = ".mp4" | |
self.mset = MplayerSettings() | |
def SetExtension(self, extension=".mp4"): | |
"""Sets the extention of the video files.""" | |
self.extension = extension | |
def SetLauncher(self, launcher): | |
"""Sets the launcher.""" | |
mset = launcher | |
def SetPlayList(self,mode=0,playlist=None): | |
"""Sets the playlist | |
mode=int, 0 is alphanumeric, 1 is shuffled, 2 read from a play list.""" | |
try: | |
self.mode = mode | |
if playlist != None: | |
self.playlistname = playlist | |
# TODO: Put in play list reading functionality | |
if self.mode == 0: | |
self.playlist = [] | |
os.chdir(HOME_DIR) | |
self.playlist = os.listdir(self.videofolder) | |
# Clean the list for entries desired only by extension. | |
index = 0 | |
lenlist = len(self.playlist) | |
while(index != lenlist): | |
print("index is: "+str(index)) | |
if self.playlist[index].find(self.extension) == -1: | |
self.playlist.pop(index) | |
lenlist -= 1 | |
else: | |
index += 1 | |
self.numOfVideos = len(self.playlist) | |
if self.mode == 1: | |
self.playlist = [] | |
os.chdir(HOME_DIR) | |
self.playlist = os.listdir(self.videofolder) | |
# Clean the list for entries desired only by extension. | |
index = 0 | |
lenlist = len(self.playlist) | |
while(index != lenlist): | |
print("index is: "+str(index)) | |
if self.playlist[index].find(self.extension) == -1: | |
self.playlist.pop(index) | |
lenlist -= 1 | |
else: | |
index += 1 | |
random.shuffle(self.playlist) # Randomizes the playlist | |
self.numOfVideos = len(self.playlist) | |
#TODO: Support more than just mode 0 | |
except Merror as e: | |
raise e | |
except: | |
raise | |
#raise Merror("SetPlayList","Unknown") | |
def StartPlaylist(self): | |
"""Starts the playlist""" | |
try: | |
if self.playlist == None or self.playlist == [] or self.numOfVideos == 0: | |
raise Merror("StartPlaylist","empty playlist") | |
if self.mset == None: | |
raise Merror("StartPlaylist", "Mplayer settings not set") | |
if self.mode == 1: | |
try: | |
while(1): | |
params= str(self.launchLine).split() | |
params.append(self.videofolder + "/" + self.playlist[self.numVideo]) | |
for p in params: | |
print("param: " + p) | |
retval = subprocess.call(params) | |
if retval != 0: | |
print("Retval is " + str(retval)) | |
break | |
self.numVideo += 1 | |
if self.numVideo == self.numOfVideos: | |
self.numVideo = 0 | |
random.shuffle(self.playlist) # When the count resets, randomize playlist again. | |
except: | |
raise Merror("StartPlaylist", "Mplayer failed either to run or during run") | |
except Merror as e: | |
raise e | |
except: | |
raise Merror("StartPlaylist","unknown") | |
def _SetLastPlayed(self, value): | |
"""Sets last played video""" | |
self.last = value | |
def _LastPlayed(self): | |
"""Last played video""" | |
if self.last == None: | |
return "" | |
else: | |
return self.last | |
def SetLaunchLine(self,line): | |
"""Sets the command line arguments for Mplayer""" | |
self.launchLine = line | |
def SetVideoFolder(self, folder): | |
"""Sets folder of vidoes""" | |
self.videofolder = folder | |
class Merror(Exception): | |
def __init__(self,method,description): | |
"""Mplay exception.""" | |
self.message = "Mplay exception in method " + method + " with " + description + " error." | |
def main(): | |
"""Main function that will run when the script | |
is called from the command line.""" | |
settings = MplayerSettings() | |
settings.SetFS(True) | |
print('Settings: ' + str(settings)) | |
vidpunk = Mplayer() | |
vidpunk.SetVideoFolder("/home/build/vpunks/videos") | |
vidpunk.SetExtension(".mp4") | |
vidpunk.SetLaunchLine(settings) | |
vidpunk.SetPlayList(1) # Default option sets mode 0 | |
#Run the program | |
vidpunk.StartPlaylist() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment