Browse Source

feat: add duration-related methods to SourceMedia

main
Rob Hallam 4 months ago
parent
commit
9b2ddce115
1 changed files with 23 additions and 0 deletions
  1. +23
    -0
      pipeline/utils.py

+ 23
- 0
pipeline/utils.py View File

@@ -45,12 +45,35 @@ class Source():
if not provider: if not provider:
raise ValueError("Provider must be provided") # TODO: #API -- decide if this is necessary raise ValueError("Provider must be provided") # TODO: #API -- decide if this is necessary
self.provider = provider self.provider = provider

def __str__(self): def __str__(self):
"""See: 'accessing the object should return the path to the media file'""" """See: 'accessing the object should return the path to the media file'"""
return self.path return self.path


def __repr__(self): def __repr__(self):
return f"Source({self.source}, {self.path}, {self.provider})" return f"Source({self.source}, {self.path}, {self.provider})"

def duration(self):
"""Return the duration of the media file at self.path (result is cached)"""
return self._duration or self._get_duration(self.path)

def _get_duration(self, file):
"""Use ffprobe to get the duration of the media file at self.path and cache result (_duration)

usage: ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 <file>
"""
# test if file exists
try:
with open(file) as _:
pass
except FileNotFoundError:
raise FileNotFoundError(f"File not found: {file}")
# cache the result
self._duration = 0.0 or float(subprocess.check_output(["ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", file]))
return self._duration



class Interval(): class Interval():
"""An interval of time in a media file """An interval of time in a media file




Loading…
Cancel
Save