Skip to content

Commit

Permalink
fix/sound_duration (#255)
Browse files Browse the repository at this point in the history
when using mediainfo the parsing would fail for files > 1min
  • Loading branch information
JarbasAl authored Jun 21, 2024
1 parent 12808d1 commit b1519c4
Showing 1 changed file with 21 additions and 9 deletions.
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")

0 comments on commit b1519c4

Please sign in to comment.