Last active
August 7, 2018 03:02
-
-
Save apast/19374fb12d4804ed04d9fa815758654a to your computer and use it in GitHub Desktop.
play, the mplayer player
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/python | |
import os | |
import sys | |
import uuid | |
class Player(): | |
""" | |
Your media files at a simple CLI command of distance! | |
play give full control for search and shuffle files under a root path. | |
Internally and sincerelly, it is a simple and personal wrapper for mplayer. | |
""" | |
def build_playlist(self, lookup_path, query, reload_files=False): | |
script_dir = os.path.realpath(os.path.dirname(__file__)) | |
cache_path = os.path.join(script_dir, ".cache") | |
query_id = query if query else "__full__" | |
playlist_cache = os.path.join(cache_path, query_id) | |
if not os.path.exists(playlist_cache) or reload_files: | |
self.lookup_playlist(playlist_cache, lookup_path, query) | |
return playlist_cache | |
def lookup_playlist(self, output_file, lookup_path, query): | |
playlist_query = ["-ipath \"*%(query)s*\"" % dict(query=query) if query else ""] | |
find_args = dict(path=lookup_path, | |
playlist_query=" ".join(playlist_query), | |
playlist_cache=output_file) | |
find_cmd = ("find %(path)s -type f %(playlist_query)s" | |
" -exec echo `realpath {}` \;" | |
" | sort -n > %(playlist_cache)s") % find_args | |
os.system(find_cmd) | |
def play_list(self, playlist, shuffle=False): | |
mplayer_options = ["-shuffle" if shuffle else "", ] | |
mplayer_args=dict(playlist = playlist, | |
options = " ".join(mplayer_options) | |
) | |
mplayer_cmd = "mplayer %(options)s -playlist %(playlist)s" % mplayer_args | |
print(mplayer_cmd) | |
os.system(mplayer_cmd) | |
def fetch(self, args): | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-s", "--shuffle", dest="shuffle", default=False, | |
action="store_true", help="Enable shuffle mode") | |
parser.add_argument("-q", "--query", dest="query", help=str("Patter to be filtered." | |
" Same syntax as unix `find` command")) | |
parser.add_argument("-p", "--path", dest="path", default="./", help="Root path for lookup") | |
parser.add_argument("-r", "--reload", dest="reload_files", | |
default=False, action="store_true", | |
help=str("File list are cached. If true, it will " | |
" discard previous list and warmup the " | |
" cache again")) | |
return parser.parse_args(args) | |
def play(self, args): | |
input = self.fetch(args) | |
lookup_path = input.path | |
query = input.query | |
shuffle = input.shuffle | |
reload_files = input.reload_files | |
playlist = self.build_playlist(lookup_path, query, reload_files=reload_files) | |
self.play_list(playlist=playlist, shuffle=shuffle) | |
if __name__ == "__main__": | |
Player().play(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment