Skip to content

Instantly share code, notes, and snippets.

@reed-jones
Created April 7, 2018 19:07
Show Gist options
  • Save reed-jones/6120155013eb231125ca2219f2493b20 to your computer and use it in GitHub Desktop.
Save reed-jones/6120155013eb231125ca2219f2493b20 to your computer and use it in GitHub Desktop.
generates simple .m3u playlists
####
# Generates an .m3u playlist of all desired files
# Reed Jones 07/04/18
#
# python playlists.py
# >> generates .m3u containing all .mp4's in current directory by default
# >> saves as playlist.m3u
#
# python playlists.py --name my-great-playlist --extension .mp3 --path "C:\music\album"
# >> generates .m3u playlist containing all .mp4's and .mp3's in C:\music\album
# >> saves as C:\music\album\my-greate-playlist.m3u
####
from sys import argv
from os import listdir, getcwd
from os.path import isfile, join, splitext, abspath
# parses the passed in arguments into a useful dictionary
# eg: doSomething.py -t test -c one -c two three
# >> {'-t': ['test'], '-c': ['one', 'two', 'three']}
def getopts(argv):
opts = {}
index = 1
while argv:
if argv[0][0] == '-':
options = []
for n in range(1, len(argv)):
index = n
if argv[n][0] == '-':
break
options.append(argv[n])
if argv[0] in opts:
opts[argv[0]] = opts[argv[0]] + options
else:
opts[argv[0]] = options
argv = argv[index:]
return opts
# checks if the given flag exists, and if not, sets a default
# if flag is a string, (such as '--extension') it will return all matching arguments
# as formatted in the above getOpts function, or the default value if none are found
# if flag is a list, it concats all matching arguments and returns that, or default
# with this, you can check for ['-ext', '--extension'] and combine all results into a
# useful list
def checkOpts(opts, flag, default=None):
if isinstance(flag, str):
return opts[flag] if flag in opts else default
elif isinstance(flag, list):
flags = []
for f in flag:
if f in opts:
flags += opts[f]
return flags if flags else default
# writes and formats an .m3u file containing all desired files
# and saves the playlist to the same directory as the files are in
def writeM3U(name, path, extensions):
# opens new file in current directory
with open(path + '/' + name + '.m3u', 'w+') as f:
# m3u file header
f.write('#EXTM3U\n\n')
# list all files in current directory
for file in listdir(path):
# get file name/extension
file_name, ext = splitext(file)
# print(isfile(path + '/' + file))
# check if file meets criteria
if isfile(path + '/' + file) and ext in extensions:
# writes filename to list
f.write('#EXTINF ' + file_name + '\n')
f.write(file + '\n\n')
def main():
# parse argv
opts = getopts(argv)
# set desired filetypes
accepted_filetypes = checkOpts(opts, ['-ext', '--extension'], ['.mp4'])
# set desired playlist name, if multiple are given, it joins them
desired_name = '_'.join(checkOpts(opts, ['-n', '--name'], 'playlist'))
# set path, defaults to current working directory
path = ''.join(checkOpts(opts, ['-p', '--path'], getcwd()))
# writes and formats .m3u file
writeM3U(desired_name, path, accepted_filetypes)
# run program
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment