mirror of
https://github.com/tropicoo/yt-dlp-bot.git
synced 2025-03-04 10:42:58 +08:00
41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import logging
|
|
|
|
import yt_dlp
|
|
|
|
from yt_shared.schemas.video import DownVideo
|
|
|
|
try:
|
|
from ytdl_opts.user import YTDL_OPTS
|
|
except ImportError:
|
|
from ytdl_opts.default import YTDL_OPTS
|
|
|
|
|
|
class VideoDownloader:
|
|
def __init__(self) -> None:
|
|
self._log = logging.getLogger(self.__class__.__name__)
|
|
|
|
def download_video(self, url: str) -> DownVideo:
|
|
return self._download(url)
|
|
|
|
def _download(self, url: str) -> DownVideo:
|
|
self._log.info('Downloading %s', url)
|
|
with yt_dlp.YoutubeDL(YTDL_OPTS) as ytdl:
|
|
meta = ytdl.extract_info(url, download=False)
|
|
meta = ytdl.sanitize_info(meta)
|
|
try:
|
|
ytdl.download(url)
|
|
except yt_dlp.utils.MaxDownloadsReached as err:
|
|
self._log.warning(
|
|
'Check video URL %s. Looks like a page with videos. Stopped on %d: %s',
|
|
url,
|
|
YTDL_OPTS['max_downloads'],
|
|
err,
|
|
)
|
|
|
|
self._log.info('Finished downloading %s', url)
|
|
filepath = ytdl.prepare_filename(meta)
|
|
return DownVideo(
|
|
title=meta['title'],
|
|
name=filepath.rsplit('/', maxsplit=1)[-1],
|
|
meta=meta,
|
|
)
|