Skip to content

Instantly share code, notes, and snippets.

@attilaolah
Created May 1, 2021 15:56
Show Gist options
  • Save attilaolah/9637fd6594cf25a9ef9bc5273bc5162f to your computer and use it in GitHub Desktop.
Save attilaolah/9637fd6594cf25a9ef9bc5273bc5162f to your computer and use it in GitHub Desktop.
Pokémon TV episode downloader
"""Pokemon episode downloader script.
Usage:
python dl.py https://s2.content.video.llnw.net/smedia/path/to/playlist0.ts
"""
import os
import shutil
import sys
import requests
def main(url: str) -> None:
"""Download a Pokemon episode."""
prefix, _, _ = url.rpartition("/")
_, _, filename = prefix.rpartition("/")
if not os.path.exists(filename):
os.mkdir(filename)
i = 0
while True:
part = f"{i}.ts"
dst = os.path.join(filename, part)
if os.path.exists(dst):
print(f"Already downloaded {part}...", end="\r")
i += 1
continue
print(f"Downloading part {part}...", end="\r")
req = requests.get(url.replace("0.ts", part))
if req.status_code == 404:
break
if req.status_code != 200:
print(f"HTTP {req.status_code} when downloading {part}.")
return
with open(os.path.join(filename, part), "wb") as tmp:
tmp.write(req.content)
i += 1
print()
concat = os.path.join(filename, "list.txt")
with open(concat, "w") as dst:
dst.write("\n".join([
f"file '{x}.ts'" for x in range(i)
]))
dst.write("\n")
print(f"Merging {i} parts into {filename}.ts...")
os.system(f"ffmpeg -f concat -safe 0 -i {concat} -c copy {filename}.ts")
output = filename.replace(".mpegts", ".mp4")
print(f"Converting result to {output}")
os.system(f"ffmpeg -i {filename}.ts -c:v copy -c:a copy"
f" -bsf:a aac_adtstoasc {output}")
print("Cleaning up...")
shutil.rmtree(filename)
os.unlink(f"{filename}.ts")
if __name__ == "__main__":
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment