41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
import os, sys, pathlib
|
|
|
|
m3u8 = pathlib.Path(sys.argv[1]).resolve()
|
|
outdir = pathlib.Path(sys.argv[2]).resolve()
|
|
outdir.mkdir(parents=True, exist_ok=True)
|
|
pl_dir = m3u8.parent
|
|
|
|
def next_free(p: pathlib.Path) -> pathlib.Path:
|
|
if not p.exists():
|
|
return p
|
|
stem, suf = p.stem, p.suffix
|
|
i = 1
|
|
while True:
|
|
cand = p.with_name(f"{stem}-{i}{suf}")
|
|
if not cand.exists():
|
|
return cand
|
|
i += 1
|
|
|
|
with m3u8.open("r", encoding="utf-8") as f:
|
|
for line in f:
|
|
s = line.strip()
|
|
if not s or s.startswith("#"):
|
|
continue
|
|
src = pathlib.Path(s)
|
|
if not src.is_absolute():
|
|
src = (pl_dir / src).resolve()
|
|
else:
|
|
src = src.resolve()
|
|
|
|
if not src.exists():
|
|
print(f"skip (missing): {src}", file=sys.stderr)
|
|
continue
|
|
|
|
dest = next_free(outdir / src.name)
|
|
try:
|
|
dest.symlink_to(src)
|
|
except OSError:
|
|
# fallback to hardlink if symlink not permitted
|
|
os.link(src, dest)
|