Skip to content

Commit

Permalink
fix: Fix to an error when chunks are less than about 400ms and the vo…
Browse files Browse the repository at this point in the history
…lume is adjusted.

Change-Id: I7dc720420b76120c51d6a423de6df0c86f277186
  • Loading branch information
Kacper Krasowiak committed Oct 28, 2024
1 parent b334766 commit bde68db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ariel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.

"""Ariel library for for end-to-end video ad dubbing using AI."""
__version__ = "0.0.17"
__version__ = "0.0.18"
26 changes: 21 additions & 5 deletions ariel/audio_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
_TIMESTAMP_THRESHOLD: Final[float] = 0.001
_DEFAULT_RATE: Final[float] = 44100
_TARGET_LUFS: Final[float] = -16
_MIN_BLOCK_SIZE_MS: Final[int] = 400


def build_demucs_command(
Expand Down Expand Up @@ -581,11 +582,26 @@ def insert_audio_at_timestamps(
audio_chunk = AudioSegment.from_mp3(item["path"])
else:
audio_chunk = AudioSegment.from_mp3(item["dubbed_path"])
samples = np.array(audio_chunk.get_array_of_samples())
samples = samples.astype(np.float32) / np.iinfo(samples.dtype).max
loudness = meter.integrated_loudness(samples)
loudness_difference = _TARGET_LUFS - loudness
audio_chunk = audio_chunk.apply_gain(loudness_difference)
if len(audio_chunk) < _MIN_BLOCK_SIZE_MS:
logging.error(
f"The dubbed chunk duaration is less than {_MIN_BLOCK_SIZE_MS}."
f" Silent padding of {_MIN_BLOCK_SIZE_MS} will be added to"
" normalize the volume."
)
padding_duration = _MIN_BLOCK_SIZE_MS - len(audio_chunk)
audio_chunk += AudioSegment.silent(duration=padding_duration)
try:
samples = np.array(audio_chunk.get_array_of_samples())
samples = samples.astype(np.float32) / np.iinfo(samples.dtype).max
loudness = meter.integrated_loudness(samples)
loudness_difference = _TARGET_LUFS - loudness
audio_chunk = audio_chunk.apply_gain(loudness_difference)
except ValueError:
logging.error(
"Dubbed chunk volume could not be normalized. The orginial volume is"
" used."
)
pass
output_audio = output_audio.overlay(
audio_chunk, position=start_time, loop=False
)
Expand Down

0 comments on commit bde68db

Please sign in to comment.