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

fix/sound_duration #255

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions ovos_utils/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def play_audio(uri, play_cmd=None, environment=None):
LOG.exception(e)
return None


def get_sound_duration(path: str, base_dir: Optional[str] = "") -> float:
"""return sound duration, in seconds"""
if base_dir and path.startswith("snd/"):
Expand All @@ -136,19 +135,32 @@ def get_sound_duration(path: str, base_dir: Optional[str] = "") -> float:
frames = f.getnframes()
rate = f.getframerate()
return frames / float(rate)
media_info = find_executable("mediainfo")
if media_info:
args = (media_info, path)
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read().decode("utf-8").split("Duration")[1].split("\n")[0]
output = "".join([c for c in output if c.isdigit()])
return int(output) / 1000
ffprobe = find_executable("ffprobe")
if ffprobe:
args = (ffprobe, "-show_entries", "format=duration", "-i", path)
popen = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
popen.wait()
output = popen.stdout.read().decode("utf-8")
return float(output.split("duration=")[-1].split("\n")[0])
media_info = find_executable("mediainfo")
if media_info:
args = (media_info, path)
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
output = popen.stdout.read().decode("utf-8").split("Duration")[1].split("\n")[0].split(":")[-1]
t = 0
if " h" in output:
h, output = output.split(" h")
t += int(h) * 60 * 60
if " min" in output:
m, output = output.split(" min")
t += int(m) * 60
if " s" in output:
m, output = output.split(" s")
t += int(m)
if " ms" in output:
m, output = output.split(" ms")
t += int(m) / 1000
return t
raise RuntimeError("Failed to determine sound length, please install mediainfo or ffprobe")

Loading