[media] Added only_audio option to media.download.

This commit is contained in:
Fabio Manganiello 2024-07-16 03:50:14 +02:00
parent f78027a6eb
commit 398925d76e
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 22 additions and 4 deletions

View file

@ -765,6 +765,7 @@ class MediaPlugin(RunnablePlugin, ABC):
directory: Optional[str] = None, directory: Optional[str] = None,
timeout: int = 10, timeout: int = 10,
sync: bool = False, sync: bool = False,
only_audio: bool = False,
youtube_format: Optional[str] = None, youtube_format: Optional[str] = None,
): ):
""" """
@ -790,7 +791,7 @@ class MediaPlugin(RunnablePlugin, ABC):
:param timeout: Network timeout in seconds (default: 10). :param timeout: Network timeout in seconds (default: 10).
:param sync: If set to True, the download will be synchronous and the :param sync: If set to True, the download will be synchronous and the
action will return only when the download is completed. action will return only when the download is completed.
:param youtube_format: Override the default YouTube format selection. :param youtube_format: Override the default ``youtube_format`` setting.
:return: The absolute path to the downloaded file. :return: The absolute path to the downloaded file.
""" """
path = self._get_download_path( path = self._get_download_path(
@ -799,9 +800,14 @@ class MediaPlugin(RunnablePlugin, ABC):
if self._is_youtube_resource(url): if self._is_youtube_resource(url):
dl_thread = self._download_youtube_url( dl_thread = self._download_youtube_url(
url, path, youtube_format=youtube_format url, path, youtube_format=youtube_format, only_audio=only_audio
) )
else: else:
if only_audio:
self.logger.warning(
'Only audio download is not supported for non-YouTube URLs'
)
dl_thread = self._download_url(url, path, timeout=timeout) dl_thread = self._download_url(url, path, timeout=timeout)
if sync: if sync:
@ -969,12 +975,17 @@ class MediaPlugin(RunnablePlugin, ABC):
return download_thread return download_thread
def _download_youtube_url( def _download_youtube_url(
self, url: str, path: str, youtube_format: Optional[str] = None self,
url: str,
path: str,
youtube_format: Optional[str] = None,
only_audio: bool = False,
) -> YouTubeDownloadThread: ) -> YouTubeDownloadThread:
download_thread = YouTubeDownloadThread( download_thread = YouTubeDownloadThread(
url=url, url=url,
path=path, path=path,
ytdl=self._ytdl, ytdl=self._ytdl,
only_audio=only_audio,
youtube_format=youtube_format or self.youtube_format, youtube_format=youtube_format or self.youtube_format,
on_start=self._on_download_start, on_start=self._on_download_start,
post_event=self._post_event, post_event=self._post_event,

View file

@ -224,11 +224,17 @@ class YouTubeDownloadThread(DownloadThread):
""" """
def __init__( def __init__(
self, *args, ytdl: str, youtube_format: Optional[str] = None, **kwargs self,
*args,
ytdl: str,
youtube_format: Optional[str] = None,
only_audio: bool = False,
**kwargs,
): ):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._ytdl = ytdl self._ytdl = ytdl
self._youtube_format = youtube_format self._youtube_format = youtube_format
self._only_audio = only_audio
self._proc = None self._proc = None
self._proc_lock = threading.Lock() self._proc_lock = threading.Lock()
@ -265,6 +271,7 @@ class YouTubeDownloadThread(DownloadThread):
str(self._progress_update_interval), str(self._progress_update_interval),
'--progress-template', '--progress-template',
'%(progress)j', '%(progress)j',
*(['-x'] if self._only_audio else []),
*(['-f', self._youtube_format] if self._youtube_format else []), *(['-f', self._youtube_format] if self._youtube_format else []),
self.url, self.url,
'-o', '-o',