Skip to content

Instantly share code, notes, and snippets.

@Koenvh1
Last active Apr 12, 2021
Embed
What would you like to do?
A script to convert m3u files with absolute paths to m3u files with relative paths. The original file will be overwritten! Just call using "python m3uconverter.py path/to/folder"
import codecs
import glob
import os
import sys
def parse(file, encoding="utf-8-sig"):
fp = codecs.open(file, "r", encoding=encoding)
lines = fp.readlines()
fp.close()
root = os.path.dirname(file)
new_lines = []
for line in lines:
line = line.strip()
if not line == "" and not line[0] == "#" and (line[1] == ":" or line[0] == "/"):
line = os.path.relpath(line.strip(), root)
print(line)
new_lines.append(line)
fp = open(file, "w", encoding=encoding, newline="\r\n")
fp.write("\n".join(new_lines))
path = sys.argv[1]
enc = sys.argv[2] if len(sys.argv) >= 3 else "utf-8-sig"
if os.path.isdir(path):
for f in glob.glob(path + "/**/*", recursive=True):
if f.endswith(".m3u") or f.endswith(".m3u8"):
print(f)
parse(f, enc)
print("")
else:
parse(path, enc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment