Last active
February 17, 2018 14:39
-
-
Save akshaykarnawat/dbfeb1c94e77fc77517f2e9dc23bb53c to your computer and use it in GitHub Desktop.
Terminal music player written in python. Plays the music from the file path specified.
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 | |
""" | |
Plays the music in the directory specified | |
""" | |
import sys | |
from os import listdir, system | |
from os.path import isfile, isdir, join | |
def get_music_files(path): | |
""" | |
Gets all the music files in the path specified. | |
Arguments | |
--------- | |
path: str | |
the location path to where the music files are | |
Returns | |
------- | |
files: list(str) | |
list of all the music files | |
""" | |
return [join(path, file) for file in listdir(path) | |
if ('.mp3' in file or '.m4a' in file) and '._' not in file and isfile(join(path, file)) | |
] | |
def play_music(music_files): | |
""" | |
Plays the music using the afplay system command. | |
Arguments | |
--------- | |
music_files: list(str) | |
list of all the music files which needs to be played | |
""" | |
for index, file in enumerate(music_files): | |
print("[{0}] Playing :: {1}".format(index, file)) | |
system("afplay -q 1 " + "\'" + file + "\'") | |
if __name__ == '__main__': | |
ARGS_LENGTH = len(sys.argv[1:]) | |
ARGUMENT = str(sys.argv[1]) | |
play_music([ARGUMENT]) if ARGS_LENGTH > 0 and isfile(ARGUMENT) \ | |
else play_music(get_music_files(ARGUMENT)) if ARGS_LENGTH > 0 and isdir(ARGUMENT) \ | |
else play_music(get_music_files("./")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(devPy3) /Volumes/ExPac/music > python musicplayer.py ./
[0] Playing :: ./Relaxing Soft Piano Music vol.2.mp3
[1] Playing :: ./Relaxing Soft Piano Music.mp3
[2] Playing :: ./6 Hour Meditation Music.mp3