Skip to content

Commit

Permalink
feat: optimized the code that handles dplii.
Browse files Browse the repository at this point in the history
feat: detects unsupported channels that dee cannot accept and offloads the downmix as needed to ffmpeg instead, but still encodes with dee
  • Loading branch information
jessielw committed Sep 20, 2023
1 parent b6d154f commit 66bb9d7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
7 changes: 7 additions & 0 deletions deezy/audio_encoders/dee/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def _generate_ffmpeg_cmd(
):
"""Method to generate FFMPEG command to process"""

@staticmethod
def _dee_allowed_input(input_channels: int):
"""Check's if the input channels are in the DEE allowed input channel list"""
if input_channels in [1, 2, 6, 8]:
return True
return False

@staticmethod
def _get_ffmpeg_cmd(
ffmpeg_path: Path,
Expand Down
28 changes: 23 additions & 5 deletions deezy/audio_encoders/dee/dd.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,22 @@ def encode(self, payload: object):
# temp filename
temp_filename = Path(tempfile.NamedTemporaryFile(delete=False).name).name

# check to see if input channels are accepted by dee
dee_allowed_input = self._dee_allowed_input(audio_track_info.channels)

# downmix config
down_mix_config = self._get_down_mix_config(
payload.channels, audio_track_info.channels
payload.channels,
audio_track_info.channels,
payload.stereo_mix,
dee_allowed_input,
)

# determine if FFMPEG downmix is needed
ffmpeg_down_mix = False
if down_mix_config == "off" and audio_track_info.channels != 2:
if (down_mix_config == "off" and not dee_allowed_input) or (
payload.channels.value == 2 and payload.stereo_mix == StereoDownmix.DPLII
):
ffmpeg_down_mix = payload.channels.value

# stereo mix
Expand Down Expand Up @@ -198,9 +206,19 @@ def _get_accepted_bitrates(channels: int):
return dee_dd_bitrates.get("dd_51")

@staticmethod
def _get_down_mix_config(channels: DolbyDigitalChannels, input_channels: int):
if channels.value == input_channels or not any(
member.value == input_channels for member in DolbyDigitalChannels
def _get_down_mix_config(
channels: DolbyDigitalChannels,
input_channels: int,
stereo_downmix: StereoDownmix,
dee_allowed_input: bool,
):
if (
(channels.value == input_channels)
or (
channels == DolbyDigitalChannels.STEREO
and stereo_downmix == StereoDownmix.DPLII
)
or not dee_allowed_input
):
return "off"
elif channels == DolbyDigitalChannels.MONO:
Expand Down
22 changes: 11 additions & 11 deletions deezy/audio_encoders/dee/ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from deezy.audio_processors.dee import ProcessDEE
from deezy.audio_processors.ffmpeg import ProcessFFMPEG
from deezy.enums.ddp import DolbyDigitalPlusChannels
from deezy.enums.shared import StereoDownmix
from deezy.exceptions import InvalidExtensionError, OutputFileNotFoundError
from deezy.track_info.mediainfo import MediainfoParser

Expand Down Expand Up @@ -86,19 +85,22 @@ def encode(self, payload: object):
# temp filename
temp_filename = Path(tempfile.NamedTemporaryFile(delete=False).name).name

# check to see if input channels are accepted by dee
dee_allowed_input = self._dee_allowed_input(audio_track_info.channels)

# downmix config
down_mix_config = self._get_down_mix_config(
payload.channels, audio_track_info.channels
payload.channels, audio_track_info.channels, dee_allowed_input
)

# determine if FFMPEG downmix is needed
# determine if FFMPEG downmix is needed for unsupported channels
ffmpeg_down_mix = False
if down_mix_config == "off":
if down_mix_config == "off" and not dee_allowed_input:
ffmpeg_down_mix = payload.channels.value

# stereo mix
stereo_mix = str(payload.stereo_mix.name).lower()

# file output (if an output is a defined check users extension and use their output)
if payload.file_output:
output = Path(payload.file_output)
Expand All @@ -122,7 +124,6 @@ def encode(self, payload: object):
sample_rate=audio_track_info.sample_rate,
ffmpeg_down_mix=ffmpeg_down_mix,
channels=payload.channels,
stereo_down_mix=payload.stereo_mix,
output_dir=temp_dir,
wav_file_name=wav_file_name,
)
Expand Down Expand Up @@ -203,10 +204,10 @@ def _get_accepted_bitrates(channels: int):
return dee_ddp_bitrates.get("ddp_71_combined")

@staticmethod
def _get_down_mix_config(channels: DolbyDigitalPlusChannels, input_channels: int):
if channels.value == input_channels or not any(
member.value == input_channels for member in DolbyDigitalPlusChannels
):
def _get_down_mix_config(
channels: DolbyDigitalPlusChannels, input_channels: int, dee_allowed_input: bool
):
if channels.value == input_channels or not dee_allowed_input:
return "off"
elif channels == DolbyDigitalPlusChannels.MONO:
return "mono"
Expand All @@ -225,7 +226,6 @@ def _generate_ffmpeg_cmd(
sample_rate: int,
ffmpeg_down_mix: Union[bool, DolbyDigitalPlusChannels],
channels: DolbyDigitalPlusChannels,
stereo_down_mix: StereoDownmix,
output_dir: Path,
wav_file_name: str,
):
Expand Down

0 comments on commit 66bb9d7

Please sign in to comment.