Created
September 10, 2024 15:12
-
-
Save timendum/966b45e348ac78c0c5b24bc272e5ffd4 to your computer and use it in GitHub Desktop.
Youtube Playlist to Podcast
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
# /// script | |
# requires-python = ">=3.10" | |
# dependencies = [ | |
# "yt-dlp>0.4.0", | |
# "feedendum", | |
# ] | |
# /// | |
import argparse | |
import mimetypes | |
from datetime import datetime | |
from pathlib import Path | |
import feedendum | |
import yt_dlp | |
def main(url, fout): | |
ydl_opts = { | |
"format": "m4a/bestaudio/best", | |
"restrictfilenames": True, | |
"listsubtitles": False, | |
"outtmpl": {"default": "%(id)s.%(ext)s"}, | |
"paths": {"home": fout}, | |
} | |
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
info = ydl.extract_info(url, download=False) | |
out_data = feedendum.feed.Feed( | |
description=info["description"], | |
title=info["title"], | |
url=url, | |
update=datetime.now(), | |
_data={ | |
"itunes:image": sorted(info["thumbnails"], key=lambda e: e["width"], reverse=True)[ | |
0 | |
]["url"].split("?")[0], | |
"language": info["entries"][0]["language"] + "-" + info["entries"][0]["language"], | |
"itunes:author": info["channel"], | |
}, | |
) | |
ydl.download([e["webpage_url"] for e in info["entries"]]) | |
for entry in info["entries"]: | |
efile = next((Path(".") / fout).glob(f"{entry['id']}.*")) | |
fentry = feedendum.feed.FeedItem( | |
url=entry["webpage_url"], | |
title=entry["title"], | |
id=entry["webpage_url"], | |
content=entry["description"], | |
update=datetime.fromtimestamp(entry["timestamp"]), | |
_data={ | |
"enclosure": {"@url": "./" + efile.name, "length": efile.stat().st_size , "type": mimetypes.guess_type(efile.name)[0]}, | |
"itunes:duration": entry["duration"], | |
"image": { | |
"url": sorted( | |
filter(lambda k: "jpg" in k["url"], entry["thumbnails"]), | |
key=lambda k: k["preference"], | |
reverse=True, | |
)[0]["url"] | |
}, | |
}, | |
) | |
out_data.items.append(fentry) | |
with open(Path(".") / fout / f"{info['id']}.xml", "w") as fo: | |
fo.write(feedendum.to_rss_string(out_data)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("url", help="playlist url") | |
parser.add_argument("-v", "--verbosity", action="count", help="increase output verbosity") | |
parser.add_argument("-o", "--output", help="output folder", default="out") | |
args = parser.parse_args() | |
main(args.url, args.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment