diff --git a/src/cogs/ytdl.py b/src/cogs/ytdl.py index bac668f..0b631ad 100644 --- a/src/cogs/ytdl.py +++ b/src/cogs/ytdl.py @@ -258,6 +258,14 @@ class YTDLCog(commands.Cog): typing.Optional[str], discord.Option(description="A start and end position to trim. e.g. 00:00:00-00:10:00.", required=False), ], + subtitles: typing.Annotated[ + typing.Optional[str], + discord.Option( + str, + description="The language code of the subtitles to download. e.g. 'en', 'auto'", + required=False, + ), + ] ): """Runs yt-dlp and outputs into discord.""" await ctx.defer() @@ -312,17 +320,33 @@ class YTDLCog(commands.Cog): else: chosen_format = user_format + options.setdefault("postprocessors", []) if audio_only: # Overwrite format here to be best audio under 25 megabytes. chosen_format = "ba[filesize<20M]" # Also force sorting by the best audio bitrate first. options["format_sort"] = ["abr", "br"] - options["postprocessors"] = [ + # noinspection PyTypeChecker + options["postprocessors"].append( {"key": "FFmpegExtractAudio", "preferredquality": "96", "preferredcodec": "best"} - ] + ) options["format"] = chosen_format options["paths"] = paths + if subtitles: + subtitles, burn = subtitles.split("+", 1) if "+" in subtitles else (subtitles, False) + if subtitles.lower() == "auto": + options["writeautosubtitles"] = True + else: + options["writesubtitles"] = True + options["subtitleslangs"] = [subtitles] + + if burn: + # noinspection PyTypeChecker + options["postprocessors"].append( + {"key": "FFmpegEmbedSubtitle", "already_have_subtitle": True} + ) + with yt_dlp.YoutubeDL(options) as downloader: await ctx.respond(embed=discord.Embed().set_footer(text="Downloading (step 1/10)")) try: