Skip to content

Commit 05c01be

Browse files
committed
Add config options for concurrent fragment downloads and extractor thread pool.
1 parent 326b685 commit 05c01be

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

musicbot/config.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,32 @@ def __init__(self, config_file: pathlib.Path) -> None:
968968
"To allow either IPv4 or v6, set this to: *"
969969
),
970970
)
971+
self.ytdlp_concurrent_frags: int = self.register.init_option(
972+
section="MusicBot",
973+
option="YtdlpConcurrentFrags",
974+
dest="ytdlp_concurrent_frags",
975+
default=ConfigDefaults.ytdlp_concurrent_frags,
976+
getter="getint",
977+
comment=_Dd(
978+
"Use this many simultaneous threads to download each track.\n"
979+
"Same as ytdlp -N or --concurrent-fragments options.\n"
980+
"This option may speed up downloads, but has no effect on streams.\n"
981+
"Should not be higher than the available number of CPU cores."
982+
),
983+
)
984+
985+
self.downloader_threads_max: int = self.register.init_option(
986+
section="MusicBot",
987+
option="DownloaderTheadsMax",
988+
dest="downloader_threads_max",
989+
default=ConfigDefaults.downloader_threads_max,
990+
getter="getint",
991+
comment=_Dd(
992+
"MusicBot may use up-to this many threads for each separate extraction.\n"
993+
"Basically, the max number of simultaneous downloads or extractions.\n"
994+
"MusicBot will spawn threads as they are needed, not right away.\n"
995+
),
996+
)
971997

972998
self.user_blocklist_enabled: bool = self.register.init_option(
973999
section="MusicBot",
@@ -1551,6 +1577,8 @@ class ConfigDefaults:
15511577
ytdlp_proxy: str = ""
15521578
ytdlp_user_agent: str = ""
15531579
ytdlp_source_address: str = "*"
1580+
ytdlp_concurrent_frags: int = 1
1581+
downloader_threads_max: int = 2
15541582

15551583
pre_download_next_song: bool = True
15561584
default_search_service: str = "ytsearch"

musicbot/constants.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@
121121
# Each retry increases the timeout by multiplying attempts by the above timeout.
122122
VOICE_CLIENT_MAX_RETRY_CONNECT: int = 5
123123

124-
# Maximum number of threads MusicBot will use for downloading and extracting info.
125-
DEFAULT_MAX_INFO_DL_THREADS: int = 5
126124
# Maximum number of seconds to wait for HEAD request on media files.
127125
DEFAULT_MAX_INFO_REQUEST_TIMEOUT: int = 10
128126

musicbot/downloader.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from yt_dlp.utils import DownloadError # type: ignore[import-untyped]
2121
from yt_dlp.utils import UnsupportedError
2222

23-
from .constants import DEFAULT_MAX_INFO_DL_THREADS, DEFAULT_MAX_INFO_REQUEST_TIMEOUT
23+
from .constants import DEFAULT_MAX_INFO_REQUEST_TIMEOUT
2424
from .exceptions import ExtractionError, MusicbotException
2525
from .i18n import _L
2626
from .spotify import Spotify
@@ -123,7 +123,7 @@ def __init__(self, bot: "MusicBot") -> None:
123123
self.download_folder: pathlib.Path = bot.config.audio_cache_path
124124
# NOTE: this executor may not be good for long-running downloads...
125125
self.thread_pool = ThreadPoolExecutor(
126-
max_workers=DEFAULT_MAX_INFO_DL_THREADS,
126+
max_workers=bot.config.downloader_threads_max,
127127
thread_name_prefix="MB_Downloader",
128128
)
129129
self._supported_search = [
@@ -147,6 +147,12 @@ def __init__(self, bot: "MusicBot") -> None:
147147
ytdl_format_options = ytdl_format_options_immutable.copy()
148148
ytdl_format_options["http_headers"] = self.http_req_headers
149149

150+
# add concurrent-fragments option if it is needed.
151+
if bot.config.ytdlp_concurrent_frags > 1:
152+
ytdl_format_options["concurrent_fragment_downloads"] = (
153+
bot.config.ytdlp_concurrent_frags
154+
)
155+
150156
# apply source address settings.
151157
if bot.config.ytdlp_source_address != "*":
152158
ytdl_format_options["source_address"] = bot.config.ytdlp_source_address

0 commit comments

Comments
 (0)