Skip to content

Instantly share code, notes, and snippets.

@ftabashir
Created January 20, 2018 21:08
Show Gist options
  • Save ftabashir/e4460cfbe5fc29ab9bcc521cbae71188 to your computer and use it in GitHub Desktop.
Save ftabashir/e4460cfbe5fc29ab9bcc521cbae71188 to your computer and use it in GitHub Desktop.
python - play random mp3 using windows media player
# example of running this file: python play_mp3.py path/to/root/mp3/directory 45
# 45 in above example is number of files to play (optional)
import os,random,time,sys,subprocess,threading
def getFiles(path, extension, recursive=True):
files = []
directories = []
for path,directory,element in os.walk(path,False):
if recursive:
directories += directory
print("Loading music from: " + path)
tmparray = element
for i in range(0,len(tmparray)-1):
if len(tmparray[i])>=4 and tmparray[i].endswith('.'+extension):
files.append('"'+os.path.normpath(path+'\\'+tmparray[i])+'"')
print("Loaded " + str(len(files)) + " files, of " + str(len(element)) + " total")
for directory in directories:
files += getFiles(os.path.join(path, directory), extension, recursive)
return files
print(str(sys.argv))
mediapath = os.path.abspath(sys.argv[1])
play_count = 30
if len(sys.argv)>=3: play_count = int(sys.argv[2])
mp3s = getFiles(mediapath, 'mp3')
random.shuffle(mp3s)
subprocess.Popen(['C:\\Program Files\\Windows Media Player\\wmplayer.exe', mp3s[:play_count]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment