Skip to content

Instantly share code, notes, and snippets.

@akshaykarnawat
Last active February 17, 2018 14:39
Show Gist options
  • Save akshaykarnawat/dbfeb1c94e77fc77517f2e9dc23bb53c to your computer and use it in GitHub Desktop.
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.
#! 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("./"))
@akshaykarnawat
Copy link
Author

Directory Tree
./
├── 6\ Hour\ Meditation\ Music.mp3
├── Nightsky\ Album\ by\ Tracey\ Chattaway
│   ├── All\ the\ Little\ Lights.m4a
│   ├── Forever.m4a
│   ├── Holding\ On.m4a
│   ├── Letting\ Go.m4a
│   ├── Light\ the\ Night.m4a
│   ├── Love\ is\ Here.m4a
│   ├── Nightsky.m4a
│   └── Starlights.m4a
├── Relaxing\ Soft\ Piano\ Music\ vol.2.mp3
├── Relaxing\ Soft\ Piano\ Music.mp3
└── musicplayer.py

@akshaykarnawat
Copy link
Author

(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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment