Verified Commit 8c339d0d authored by Fabio Manganiello's avatar Fabio Manganiello
Browse files

Added support for custom YouTube preferred video/audio formats

parent 1962a8c4
Pipeline #77 passed with stages
in 5 minutes and 11 seconds
......@@ -76,6 +76,7 @@ class MediaPlugin(Plugin, ABC):
env: Optional[Dict[str, str]] = None,
volume: Optional[Union[float, int]] = None,
torrent_plugin: str = 'torrent',
youtube_format: str = 'best',
*args, **kwargs):
:param media_dirs: Directories that will be scanned for media files when
......@@ -95,6 +96,9 @@ class MediaPlugin(Plugin, ABC):
- ``rtorrent`` - torrent support over rtorrent RPC/XML interface (recommended)
- ``webtorrent`` - torrent support over webtorrent (unstable)
:param youtube_format: Select the preferred video/audio format for YouTube videos (default: ``best``).
See the `youtube-dl documentation <>`_ for
more info on supported formats.
......@@ -150,6 +154,7 @@ class MediaPlugin(Plugin, ABC):
self._videos_queue = []
self._youtube_proc = None
self.torrent_plugin = torrent_plugin
self.youtube_format = youtube_format
def _torrent_event_handler(evt_queue):
......@@ -496,9 +501,8 @@ class MediaPlugin(Plugin, ABC):
# noinspection PyProtectedMember
return YoutubeMediaSearcher()._youtube_search_html_parse(query)
def get_youtube_video_url(url):
youtube_dl = subprocess.Popen(['youtube-dl', '-f', 'best', '-g', url], stdout=subprocess.PIPE)
def get_youtube_video_url(self, url):
youtube_dl = subprocess.Popen(['youtube-dl', '-f', self.youtube_format, '-g', url], stdout=subprocess.PIPE)
url = youtube_dl.communicate()[0].decode().strip()
return url
......@@ -521,11 +525,13 @@ class MediaPlugin(Plugin, ABC):
def get_youtube_url(self, url):
def get_youtube_url(self, url, youtube_format: Optional[str] = None):
youtube_id = self.get_youtube_id(url)
if youtube_id:
url = '{}'.format(youtube_id)
proc = subprocess.Popen(['youtube-dl', '-f', 'best', '-g', url], stdout=subprocess.PIPE)
proc = subprocess.Popen([
'youtube-dl', '-f', youtube_format or self.youtube_format, '-g', url], stdout=subprocess.PIPE
raw_url ="utf-8", "strict")[:-1]
return raw_url if raw_url else url
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment