Skip to content

Commit

Permalink
add YoutubeDownloader for video-indir command
Browse files Browse the repository at this point in the history
  • Loading branch information
kytpbs committed Jul 31, 2024
1 parent 882ec2b commit ad15c9e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions Constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
FUNNY_COLOR = 696969

RESPONSES_FILE = "responses.json"
MAX_VIDEO_DOWNLOAD_SIZE: int = 80 # in MB, do not use anything other than an integer
45 changes: 41 additions & 4 deletions src/Youtube.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import functools
import logging
import os
from queue import LifoQueue

import yt_dlp

from Constants import MAX_VIDEO_DOWNLOAD_SIZE
from src.downloader import VideoDownloader, VideoFile

ydl_opts = {
'format': 'bestaudio',
'noplaylist': True,
Expand Down Expand Up @@ -75,11 +79,11 @@ def yt_dlp_hook(progress_queue: LifoQueue, download):
def youtube_download(video_url, progress_queue: LifoQueue, file_path_with_name):
logging.debug(f"Downloading {video_url} to {file_path_with_name}")
yt_dlp_hook_partial = functools.partial(yt_dlp_hook, progress_queue)
ydl_opts_new = ydl_opts.copy()
ydl_opts_new["outtmpl"] = file_path_with_name
ydl_opts_new["progress_hooks"] = [yt_dlp_hook_partial]

ydl_opts["outtmpl"] = file_path_with_name
ydl_opts["progress_hooks"] = [yt_dlp_hook_partial]

with yt_dlp.YoutubeDL(ydl_opts) as ydl:
with yt_dlp.YoutubeDL(ydl_opts_new) as ydl:
return ydl.download(url_list=[video_url])


Expand All @@ -88,3 +92,36 @@ def youtube_download(video_url, progress_queue: LifoQueue, file_path_with_name):

def get_last_played_guilded() -> video_data_guild:
return last_played

class YoutubeDownloader(VideoDownloader):
@staticmethod
def download_video_from_link(url: str, path: str | None = None) -> list[VideoFile]:
if path is None:
path = os.path.join("downloads", "youtube")

os.makedirs(path, exist_ok=True)

costum_options = {
'format': f'bestvideo[filesize<{MAX_VIDEO_DOWNLOAD_SIZE}M]+bestaudio',
"outtmpl": os.path.join(path, "%(id)s.mp4"),
'noplaylist': True,
'default_search': 'auto',
'keepvideo': False,
'nooverwrites': True,
'quiet': True,
}

with yt_dlp.YoutubeDL(costum_options) as ydl:
ydt = ydl.extract_info(url, download=True)

if ydt is None:
return []

info = ydt.get("entries", [None])[0] or ydt
video_id = info["id"]
if video_id is None:
return []

file_path = os.path.join(path, f"{video_id}.mp4")

return [VideoFile(file_path, info.get("title", None))]
3 changes: 3 additions & 0 deletions src/downloading_system.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
from typing import Type

from src.Youtube import YoutubeDownloader
from src.downloader import VideoDownloader
from src.twitter import TwitterDownloader

Expand All @@ -15,4 +16,6 @@ def get_downloader(url: str) -> Type[VideoDownloader] | None:
"""
if re.match(_TWITTER_REGEX, url):
return TwitterDownloader
if re.match(_YOUTUBE_REGEX, url):
return YoutubeDownloader
return None

0 comments on commit ad15c9e

Please sign in to comment.