Skip to content

Instantly share code, notes, and snippets.

@Koenvh1
Last active April 12, 2021 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Koenvh1/1e4793595e78724aba2b680c8bac37e4 to your computer and use it in GitHub Desktop.
Save Koenvh1/1e4793595e78724aba2b680c8bac37e4 to your computer and use it in GitHub Desktop.
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