2025-01-12 15:18:01 +00:00
|
|
|
from urllib.parse import ParseResult, urlunparse, urlparse
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from microkodi.repository import I
|
|
|
|
from microkodi.config import Config
|
|
|
|
from microkodi.helpers import recursive_dict_merge
|
|
|
|
|
|
|
|
import yt_dlp
|
|
|
|
|
2025-01-13 22:02:19 +00:00
|
|
|
DEFAULT_CONFIG = {"format": "bestvideo[width<=1920]+bestaudio", "ytdlp_options": {}}
|
|
|
|
|
2025-01-12 15:18:01 +00:00
|
|
|
|
|
|
|
def youtube_url_transformer(url: ParseResult) -> tuple[list[str], str]:
|
|
|
|
logger = logging.getLogger("Youtube")
|
|
|
|
youtube_config = I.get("YoutubeConfig")
|
|
|
|
opts = {
|
|
|
|
"format": youtube_config["format"],
|
|
|
|
**youtube_config["ytdlp_options"],
|
|
|
|
}
|
|
|
|
logger.debug("Using config for yt-dlp: %s", opts)
|
|
|
|
|
|
|
|
with yt_dlp.YoutubeDL(opts) as ytdl:
|
|
|
|
info = ytdl.extract_info(urlunparse(url), download=False)
|
|
|
|
|
2025-01-13 22:02:19 +00:00
|
|
|
#user_agent = None
|
2025-01-12 15:18:01 +00:00
|
|
|
audio_url = None
|
|
|
|
video_url = None
|
|
|
|
for format in info["requested_formats"]:
|
|
|
|
if format["width"] is None:
|
|
|
|
audio_url = format["url"]
|
|
|
|
else:
|
2025-01-13 22:02:19 +00:00
|
|
|
#user_agent = format["http_headers"]["User-Agent"]
|
2025-01-12 15:18:01 +00:00
|
|
|
video_url = format["url"]
|
|
|
|
|
2025-01-13 22:02:19 +00:00
|
|
|
args = (
|
|
|
|
[
|
|
|
|
f"--input-slave={audio_url}",
|
|
|
|
]
|
|
|
|
if audio_url
|
|
|
|
else None
|
|
|
|
)
|
2025-01-12 15:18:01 +00:00
|
|
|
return args, urlparse(video_url)
|
|
|
|
|
2025-01-13 22:02:19 +00:00
|
|
|
|
2025-01-12 15:18:01 +00:00
|
|
|
def init():
|
|
|
|
# Create the config
|
|
|
|
config: Config = I.get("Config")
|
|
|
|
youtube_config = recursive_dict_merge(
|
|
|
|
DEFAULT_CONFIG,
|
|
|
|
config.options.get(
|
|
|
|
"me.polynom.microkodi.youtube",
|
|
|
|
{},
|
|
|
|
),
|
|
|
|
)
|
|
|
|
I.register("YoutubeConfig", youtube_config)
|
|
|
|
|
|
|
|
# Register the transformers
|
|
|
|
I.get("VlcConfig").register_domain_transformer("youtu.be", youtube_url_transformer)
|
2025-01-13 22:02:19 +00:00
|
|
|
I.get("VlcConfig").register_domain_transformer(
|
|
|
|
"youtube.com", youtube_url_transformer
|
|
|
|
)
|
|
|
|
I.get("VlcConfig").register_domain_transformer(
|
|
|
|
"www.youtube.com", youtube_url_transformer
|
|
|
|
)
|