...

Music Player For Terminal

I created a simple media player that I use it very frequently. On a Linux home directory you may keep your music files in Music folder. When you run the script it lists all mp3 's from Music folder.

The script has a UI that is self-explanetory. It is simple and there is a guide when youstart the script.

Do not forget to have mpv installed on your linux machine, before running the script.

 

#!/usr/bin/python3

"""
This script plays list of mp3's from <HOME_DIR>/Music.
'mpv' used as media player.
"""

import sys
from pathlib import Path
from os.path import join
from os import listdir, system, chdir, execv
from subprocess import call

volume = '60'

home = str(Path.home())
MUSIC_DIR= join(home, 'Music')

mp3_list = sorted([ each for each in listdir(MUSIC_DIR) if each.endswith('.mp3') ] , key=str.lower)

with open('/tmp/mp3_list', 'w')as f:
    i = 0
    while i < len(mp3_list):
        f.write(str(i) + "\t" +  str(mp3_list[i]) + "\n")
        print(str(i) + "\t" +  str(mp3_list[i]) )
        i += 1
    
    print(""" 
    \tOPTIONS: 
    \t[ <#> number of single mp3, plays in loop ]
    \t[ <#1>, <#2>, ..., <#n>  a comma separated list of multiple mp3's ]
    \t[ <a> play all mp3's ],
    \t[ <l> view all mp3's by less command ] 
    \t[ <s> prompts for search to play all mp3's with some pattern ]
    \t[ <q> quits program at any time ]
    """)

def clear():
    _ = call('clear')         

def rerun():    
    # RE-RUN CURRENT RUNNING SCRIPT
    clear()
    execv(sys.argv[0], sys.argv)

val = input(": ")
val.strip()
if val == "q":
    clear()
    exit(0)

elif val == 'l':
    system('less -i /tmp/mp3_list')
    rerun()

elif val == 'a':
    answer = input('Please reconfirm to play all songs by <Enter> or <q> to quit : ')
    if answer == '':
        clear() 
        chdir(MUSIC_DIR)
        cmd_str = 'mpv --volume='+ volume + " " + join(MUSIC_DIR,'*')
        print("[ PLAY ALL ]")
        system(cmd_str)      
        rerun()
    else:
        rerun()
        

elif str.isnumeric(val) and 0 <= int(val) <= len(mp3_list):
   mp3 = mp3_list[int(val)]
   chdir(MUSIC_DIR)
   cmd_str = 'mpv --volume='+ volume + " " + join(MUSIC_DIR , mp3) + ' --loop'
   print("[", mp3, "]")
   system(cmd_str)
   rerun()

elif ',' in str(val):
    lst = list(val.split(","))
    for item in lst:
        chdir(MUSIC_DIR)
        cmd_str = 'mpv --volume=' + volume + " " + join(MUSIC_DIR , mp3_list[int(item)])
        print("[", mp3_list[int(item)] , "]")
        system(cmd_str)
    rerun()

elif val == 's':
    i = input('Search: ').lower()
    found_list = []
    for item in mp3_list:
        if i in item.lower():
            found_list.append(item)

    if len(found_list) == 0:
        print()
        input("No match found! - press a key: ")
        rerun()
    else:
        clear()
        print("Found songs:\n------------")
        for i in found_list:
            print(i)
        
        print("\n")

        x = input("\nPresss Enter to play or 'q' to quit:")
        if x == 'q':
            rerun()
        else: 
            for item in found_list:
                chdir(MUSIC_DIR)
                cmd_str = 'mpv --volume=' + volume + " " + join(MUSIC_DIR , item)
                print("[", item, "]")
                system(cmd_str)

            rerun()

elif val == 'r':
    # restart the script by itself
    execv(sys.executable, ['python'] + sys.argv)

else:
    rerun()

 

 

Screenshot