Skip to content

Commit

Permalink
Added merge_output_format option to media plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
blacklight committed Aug 13, 2024
1 parent 50beb14 commit 1189e71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
23 changes: 20 additions & 3 deletions platypush/plugins/media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ def __init__(
env: Optional[Dict[str, str]] = None,
volume: Optional[Union[float, int]] = None,
torrent_plugin: str = 'torrent',
youtube_format: Optional[str] = 'bv[height<=?1080][ext=mp4]+ba',
youtube_format: Optional[str] = None,
youtube_dl: str = 'yt-dlp',
merge_output_format: str = 'mp4',
**kwargs,
):
"""
Expand All @@ -182,15 +183,20 @@ def __init__(
YouTube videos - and any media supported by youtube-dl or the
selected fork. See the `youtube-dl documentation
<https://github.com/ytdl-org/youtube-dl#format-selection>`_ for more
info on supported formats. Default:
``bv*[height<=?1080][ext=mp4]+bestaudio/best`` - select the best
info on supported formats. Example:
``bestvideo[height<=?1080][ext=mp4]+bestaudio`` - select the best
mp4 video with a resolution <= 1080p, and the best audio format.
:param youtube_dl: Path to the ``youtube-dl`` executable, used to
extract information from YouTube videos and other media platforms.
Default: ``yt-dlp``. The default has changed from ``youtube-dl`` to
the ``yt-dlp`` fork because the former is badly maintained and its
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)
Expand Down Expand Up @@ -247,6 +253,7 @@ def __init__(
self._youtube_proc = None
self.torrent_plugin = torrent_plugin
self.youtube_format = youtube_format
self.merge_output_format = merge_output_format
self._latest_resource: Optional[MediaResource] = None

@staticmethod
Expand Down Expand Up @@ -767,6 +774,7 @@ def download(
sync: bool = False,
only_audio: bool = False,
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
Expand Down Expand Up @@ -794,6 +802,8 @@ def download(
:param only_audio: If set to True, only the audio track will be downloaded
(only supported for yt-dlp-compatible URLs for now).
: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.
"""
path = self._get_download_path(
Expand Down Expand Up @@ -949,6 +959,11 @@ def _get_download_path(
if youtube_format
else []
),
*(
['--merge-output-format', self.merge_output_format]
if self.merge_output_format
else []
),
'-O',
'%(title)s.%(ext)s',
url,
Expand Down Expand Up @@ -981,6 +996,7 @@ def _download_youtube_url(
url: str,
path: str,
youtube_format: Optional[str] = None,
merge_output_format: Optional[str] = None,
only_audio: bool = False,
) -> YouTubeDownloadThread:
download_thread = YouTubeDownloadThread(
Expand All @@ -989,6 +1005,7 @@ def _download_youtube_url(
ytdl=self._ytdl,
only_audio=only_audio,
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,
post_event=self._post_event,
stop_event=self._should_stop,
Expand Down
6 changes: 6 additions & 0 deletions platypush/plugins/media/_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,14 @@ def __init__(
*args,
ytdl: str,
youtube_format: Optional[str] = None,
merge_output_format: Optional[str] = None,
only_audio: bool = False,
**kwargs,
):
super().__init__(*args, **kwargs)
self._ytdl = ytdl
self._youtube_format = youtube_format
self._merge_output_format = merge_output_format
self._only_audio = only_audio
self._proc = None
self._proc_lock = threading.Lock()
Expand Down Expand Up @@ -273,6 +275,10 @@ def _run(self):
'%(progress)j',
*(['-x'] if self._only_audio 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,
'-o',
self.path,
Expand Down

0 comments on commit 1189e71

Please sign in to comment.