Skip to content

Commit c16e708

Browse files
committed
make ffmpegopusaudio codec behave according to docs
1 parent 96180fb commit c16e708

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

discord/player.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,10 @@ class FFmpegOpusAudio(FFmpegAudio):
355355
The codec to use to encode the audio data. Normally this would be
356356
just ``libopus``, but is used by :meth:`FFmpegOpusAudio.from_probe` to
357357
opportunistically skip pointlessly re-encoding Opus audio data by passing
358-
``copy`` as the codec value. Any values other than ``copy``, ``opus``, or
359-
``libopus`` will be considered ``libopus``. Defaults to ``libopus``.
358+
``copy`` as the codec value. Any values other than ``copy``, or
359+
``libopus`` will be considered ``libopus``. ``opus`` will also be considered
360+
``libopus`` since the ``opus`` encoder is still in development. Defaults to ``libopus``.
361+
360362
361363
.. warning::
362364
@@ -407,7 +409,9 @@ def __init__(
407409
args.append("-i")
408410
args.append("-" if pipe else source)
409411

410-
codec = "copy" if codec in ("opus", "libopus") else "libopus"
412+
# use "libopus" when "opus" is specified since the "opus" encoder is incomplete
413+
# link to ffmpeg docs: https://www.ffmpeg.org/ffmpeg-codecs.html#opus
414+
codec = "copy" if codec == "copy" else "libopus"
411415

412416
args.extend(
413417
(
@@ -417,17 +421,24 @@ def __init__(
417421
"opus",
418422
"-c:a",
419423
codec,
420-
"-ar",
421-
"48000",
422-
"-ac",
423-
"2",
424-
"-b:a",
425-
f"{bitrate}k",
426424
"-loglevel",
427425
"warning",
428426
)
429427
)
430428

429+
# only pass in bitrate, sample rate, channels arguments when actually encoding to avoid ffmpeg warnings
430+
if codec != "copy":
431+
args.extend(
432+
(
433+
"-ar",
434+
"48000",
435+
"-ac",
436+
"2",
437+
"-b:a",
438+
f"{bitrate}k",
439+
)
440+
)
441+
431442
if isinstance(options, str):
432443
args.extend(shlex.split(options))
433444

@@ -501,6 +512,8 @@ def custom_probe(source, executable):
501512

502513
executable = kwargs.get("executable")
503514
codec, bitrate = await cls.probe(source, method=method, executable=executable)
515+
# only re-encode ir source isn't already opus, else directly copy source audio stream
516+
codec = "copy" if codec in ("opus", "libopus") else "libopus"
504517
return cls(source, bitrate=bitrate, codec=codec, **kwargs) # type: ignore
505518

506519
@classmethod

0 commit comments

Comments
 (0)