Created
August 31, 2017 22:41
-
-
Save mbrgm/ef153ebad9c669bd580103c399ee8b77 to your computer and use it in GitHub Desktop.
Export songs from an m3u8 playlist to a directory
This file contains hidden or 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/env python | |
import os | |
import re | |
import sys | |
import shutil | |
from urllib.parse import unquote | |
def main(): | |
playlist = sys.argv[1] | |
rootdir = sys.argv[2] | |
dest_dir = sys.argv[3] | |
with open(playlist, 'r') as f: | |
lines = f.readlines() | |
lines = filter(lambda x: not x.startswith('#'), lines) | |
for idx, line in enumerate(lines): | |
src_path = get_entry_path(line.strip(), rootdir) | |
comps = src_path.split('/') | |
dest_name = '{} - {}'.format(comps[-3], comps[-1]) | |
dest_path = os.path.join(dest_dir, dest_name) | |
shutil.copy2(src_path, dest_path) | |
def get_entry_path(entry, rootdir): | |
if entry.startswith('#'): | |
return | |
local_entry = re.sub(r'^local:track:', '', entry) | |
relpath = unquote(local_entry) | |
abspath = os.path.join(rootdir, relpath) | |
return abspath | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment