Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore start_pts only for mp3 files #104

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 22 additions & 14 deletions audiotools/core/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,26 @@ def r128stats(filepath: str, quiet: bool):
return stats_dict


def ffprobe_offset(path):
def ffprobe_offset_and_codec(path: str) -> Tuple[float, str]:
vickianand marked this conversation as resolved.
Show resolved Hide resolved
"""Given a path to a file, returns the start time offset and codec of
the first audio stream.
"""
ff = ffmpy.FFprobe(
inputs={path: None},
global_options="-show_entries format=start_time:stream=duration,start_time,codec_type,start_pts,time_base -of json -v quiet",
global_options="-show_entries format=start_time:stream=duration,start_time,codec_type,codec_name,start_pts,time_base -of json -v quiet",
)
streams = json.loads(ff.run(stdout=subprocess.PIPE)[0])["streams"]
seconds_offset = 0.0
# Get the offset of the first audio stream we find
codec = None

# Get the offset and codec of the first audio stream we find
# and return its start time, if it has one.
for stream in streams:
if stream["codec_type"] == "audio":
seconds_offset = stream.get("start_time", 0.0)
codec = stream.get("codec_name")
break
return float(seconds_offset)
return float(seconds_offset), codec


class FFMPEGMixin:
Expand Down Expand Up @@ -174,17 +180,19 @@ def load_from_file_with_ffmpeg(cls, audio_path: str, quiet: bool = True, **kwarg
)
ff.run()

# We pad the file using the start time offset
# in case it's an audio stream starting at some
# offset in a video container.
pad = ffprobe_offset(audio_path)
# Don't pad files with discrepancies less than
# 0.027s - it's likely due to codec latency.
# The amount of latency introduced by mp3 is
# 1152, which is 0.0261 44khz. So we
# set the threshold here slightly above that.
# We pad the file using the start time offset in case it's an audio
# stream starting at some offset in a video container.
pad, codec = ffprobe_offset_and_codec(audio_path)

# For mp3s, don't pad files with discrepancies less than 0.027s -
# it's likely due to codec latency. The amount of latency introduced
# by mp3 is 1152, which is 0.0261 44khz. So we set the threshold
# here slightly above that.
# Source: https://lame.sourceforge.io/tech-FAQ.txt.
if pad < 0.027:
#
# The client should match this logic.
# See https://linear.app/descript/issue/MEDIA-1034/mp3-files-decoding-incorrectly-during-exportweb-was-echo-during
if codec == 'mp3' and pad < 0.027:
anjoola marked this conversation as resolved.
Show resolved Hide resolved
pad = 0.0
ff = ffmpy.FFmpeg(
inputs={wav_file: None},
Expand Down