Improve audio only downloads
This commit is contained in:
parent
b815f96c2e
commit
7d87628ea2
1 changed files with 34 additions and 0 deletions
|
@ -156,6 +156,36 @@ class YTDLCog(commands.Cog):
|
||||||
self.log.debug("Attachment index %d is out of range (%r)", attachment_index, message.attachments)
|
self.log.debug("Attachment index %d is out of range (%r)", attachment_index, message.attachments)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def convert_to_m4a(self, file: Path) -> Path:
|
||||||
|
"""
|
||||||
|
Converts a file to m4a format.
|
||||||
|
:param file: The file to convert
|
||||||
|
:return: The converted file
|
||||||
|
"""
|
||||||
|
new_file = file.with_suffix(".m4a")
|
||||||
|
args = [
|
||||||
|
"-vn",
|
||||||
|
"-sn",
|
||||||
|
"-i",
|
||||||
|
str(file),
|
||||||
|
"-c:a",
|
||||||
|
"aac",
|
||||||
|
"-b:a",
|
||||||
|
"64k",
|
||||||
|
"-movflags",
|
||||||
|
"faststart",
|
||||||
|
"-y",
|
||||||
|
str(new_file)
|
||||||
|
]
|
||||||
|
process = subprocess.run(
|
||||||
|
["ffmpeg", *args],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE
|
||||||
|
)
|
||||||
|
if process.returncode != 0:
|
||||||
|
raise RuntimeError(process.stderr.decode())
|
||||||
|
return new_file
|
||||||
|
|
||||||
@commands.slash_command(name="yt-dl")
|
@commands.slash_command(name="yt-dl")
|
||||||
@commands.max_concurrency(1, wait=False)
|
@commands.max_concurrency(1, wait=False)
|
||||||
# @commands.bot_has_permissions(send_messages=True, embed_links=True, attach_files=True)
|
# @commands.bot_has_permissions(send_messages=True, embed_links=True, attach_files=True)
|
||||||
|
@ -429,6 +459,10 @@ class YTDLCog(commands.Cog):
|
||||||
)
|
)
|
||||||
file = new_file
|
file = new_file
|
||||||
|
|
||||||
|
if audio_only and file.suffix != ".m4a":
|
||||||
|
self.log.info("Converting %r to m4a.", file)
|
||||||
|
file = await asyncio.to_thread(self.convert_to_m4a, file)
|
||||||
|
|
||||||
stat = file.stat()
|
stat = file.stat()
|
||||||
size_bytes = stat.st_size
|
size_bytes = stat.st_size
|
||||||
if size_bytes >= ((25 * 1024 * 1024) - 256):
|
if size_bytes >= ((25 * 1024 * 1024) - 256):
|
||||||
|
|
Loading…
Reference in a new issue