Added merge_output_format option to media plugins

This commit is contained in:
Fabio Manganiello 2024-08-13 13:40:15 +02:00
parent 50beb1460b
commit 1189e71539
2 changed files with 26 additions and 3 deletions

View file

@ -156,8 +156,9 @@ class MediaPlugin(RunnablePlugin, ABC):
env: Optional[Dict[str, str]] = None, env: Optional[Dict[str, str]] = None,
volume: Optional[Union[float, int]] = None, volume: Optional[Union[float, int]] = None,
torrent_plugin: str = 'torrent', torrent_plugin: str = 'torrent',
youtube_format: Optional[str] = 'bv[height<=?1080][ext=mp4]+ba', youtube_format: Optional[str] = None,
youtube_dl: str = 'yt-dlp', youtube_dl: str = 'yt-dlp',
merge_output_format: str = 'mp4',
**kwargs, **kwargs,
): ):
""" """
@ -182,8 +183,8 @@ class MediaPlugin(RunnablePlugin, ABC):
YouTube videos - and any media supported by youtube-dl or the YouTube videos - and any media supported by youtube-dl or the
selected fork. See the `youtube-dl documentation selected fork. See the `youtube-dl documentation
<https://github.com/ytdl-org/youtube-dl#format-selection>`_ for more <https://github.com/ytdl-org/youtube-dl#format-selection>`_ for more
info on supported formats. Default: info on supported formats. Example:
``bv*[height<=?1080][ext=mp4]+bestaudio/best`` - select the best ``bestvideo[height<=?1080][ext=mp4]+bestaudio`` - select the best
mp4 video with a resolution <= 1080p, and the best audio format. mp4 video with a resolution <= 1080p, and the best audio format.
:param youtube_dl: Path to the ``youtube-dl`` executable, used to :param youtube_dl: Path to the ``youtube-dl`` executable, used to
@ -191,6 +192,11 @@ class MediaPlugin(RunnablePlugin, ABC):
Default: ``yt-dlp``. The default has changed from ``youtube-dl`` to Default: ``yt-dlp``. The default has changed from ``youtube-dl`` to
the ``yt-dlp`` fork because the former is badly maintained and its the ``yt-dlp`` fork because the former is badly maintained and its
latest release was pushed in 2021. latest release was pushed in 2021.
:param merge_output_format: If media download requires ``youtube_dl``,
and the upstream media contains both audio and video to be merged,
this can be used to specify the format of the output container -
e.g. ``mp4``, ``mkv``, ``avi``, ``flv``. Default: ``mp4``.
""" """
super().__init__(**kwargs) super().__init__(**kwargs)
@ -247,6 +253,7 @@ class MediaPlugin(RunnablePlugin, ABC):
self._youtube_proc = None self._youtube_proc = None
self.torrent_plugin = torrent_plugin self.torrent_plugin = torrent_plugin
self.youtube_format = youtube_format self.youtube_format = youtube_format
self.merge_output_format = merge_output_format
self._latest_resource: Optional[MediaResource] = None self._latest_resource: Optional[MediaResource] = None
@staticmethod @staticmethod
@ -767,6 +774,7 @@ class MediaPlugin(RunnablePlugin, ABC):
sync: bool = False, sync: bool = False,
only_audio: bool = False, only_audio: bool = False,
youtube_format: Optional[str] = None, youtube_format: Optional[str] = None,
merge_output_format: Optional[str] = None,
): ):
""" """
Download a media URL to a local file on the Platypush host (yt-dlp Download a media URL to a local file on the Platypush host (yt-dlp
@ -794,6 +802,8 @@ class MediaPlugin(RunnablePlugin, ABC):
:param only_audio: If set to True, only the audio track will be downloaded :param only_audio: If set to True, only the audio track will be downloaded
(only supported for yt-dlp-compatible URLs for now). (only supported for yt-dlp-compatible URLs for now).
:param youtube_format: Override the default ``youtube_format`` setting. :param youtube_format: Override the default ``youtube_format`` setting.
:param merge_output_format: Override the default
``merge_output_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(
@ -949,6 +959,11 @@ class MediaPlugin(RunnablePlugin, ABC):
if youtube_format if youtube_format
else [] else []
), ),
*(
['--merge-output-format', self.merge_output_format]
if self.merge_output_format
else []
),
'-O', '-O',
'%(title)s.%(ext)s', '%(title)s.%(ext)s',
url, url,
@ -981,6 +996,7 @@ class MediaPlugin(RunnablePlugin, ABC):
url: str, url: str,
path: str, path: str,
youtube_format: Optional[str] = None, youtube_format: Optional[str] = None,
merge_output_format: Optional[str] = None,
only_audio: bool = False, only_audio: bool = False,
) -> YouTubeDownloadThread: ) -> YouTubeDownloadThread:
download_thread = YouTubeDownloadThread( download_thread = YouTubeDownloadThread(
@ -989,6 +1005,7 @@ class MediaPlugin(RunnablePlugin, ABC):
ytdl=self._ytdl, ytdl=self._ytdl,
only_audio=only_audio, only_audio=only_audio,
youtube_format=youtube_format or self.youtube_format, youtube_format=youtube_format or self.youtube_format,
merge_output_format=merge_output_format or self.merge_output_format,
on_start=self._on_download_start, on_start=self._on_download_start,
post_event=self._post_event, post_event=self._post_event,
stop_event=self._should_stop, stop_event=self._should_stop,

View file

@ -228,12 +228,14 @@ class YouTubeDownloadThread(DownloadThread):
*args, *args,
ytdl: str, ytdl: str,
youtube_format: Optional[str] = None, youtube_format: Optional[str] = None,
merge_output_format: Optional[str] = None,
only_audio: bool = False, only_audio: bool = False,
**kwargs, **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._merge_output_format = merge_output_format
self._only_audio = only_audio self._only_audio = only_audio
self._proc = None self._proc = None
self._proc_lock = threading.Lock() self._proc_lock = threading.Lock()
@ -273,6 +275,10 @@ class YouTubeDownloadThread(DownloadThread):
'%(progress)j', '%(progress)j',
*(['-x'] if self._only_audio else []), *(['-x'] if self._only_audio else []),
*(['-f', self._youtube_format] if self._youtube_format else []), *(['-f', self._youtube_format] if self._youtube_format else []),
*(
['--stream-output-format', self._merge_output_format]
if self._merge_output_format else []
),
self.url, self.url,
'-o', '-o',
self.path, self.path,