You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.8 KiB
Python

import subprocess
import yt_dlp
from urllib.error import HTTPError
from pathlib import Path
from yt_dlp_stream_to_s3.s3_client import s3
from yt_dlp_stream_to_s3.utils import remove_queries_from_url
from yt_dlp_stream_to_s3.config import AWS_S3_BUCKET_NAME
from yt_dlp_stream_to_s3.errors import YtDlpStreamToS3Error
def yt_dlp_stream_to_s3(url: str, s3_extra_args: dict = ()) -> None:
media_url = remove_queries_from_url(url)
ydl_opts = {
"outtmpl": "%(title)s-%(id)s.%(ext)s",
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(media_url, download=False)
video_title = info_dict.get("title", None)
video_id = info_dict.get("id", None)
video_ext = info_dict.get("ext", None)
filename = f"{video_title}-{video_id}.{video_ext}"
yt_dlp_process = subprocess.Popen(
[
"yt-dlp",
media_url,
"--downloader",
"ffmpeg",
"--hls-use-mpegts",
"--no-part",
"-o",
"-",
],
stdout=subprocess.PIPE,
)
s3.upload_fileobj(
yt_dlp_process.stdout,
AWS_S3_BUCKET_NAME,
filename,
ExtraArgs=s3_extra_args,
)
yt_dlp_process.wait()
except HTTPError:
raise YtDlpStreamToS3Error("Could not connect to the media resource.")
except yt_dlp.utils.ExtractorError:
raise YtDlpStreamToS3Error("Could not extra info from media.")
except yt_dlp.utils.DownloadError:
raise YtDlpStreamToS3Error("Could not download media. Check logs.")