Skip to content

Implement len() for FFStream #25

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 14 additions & 2 deletions ffprobe/ffprobe.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ def __repr__(self):

return template.format(**self.__dict__)

def __len__(self):
"""
Returns the truncated integer duration of the stream (in seconds), if available, defaulting to 0.

Raises a TypeError if called on a stream that is not labelled as video or audio.
"""
if self.is_video() or self.is_audio():
return int(float(self.__dict__.get('duration', '0.0')))

__subclass = f"[{self.__dict__.get('codec_type', None)}]".replace("[]","")
raise TypeError(f"object of type '{self.__class__.__name__}{__subclass}' has no len()")

def is_audio(self):
"""
Is this stream labelled as an audio stream?
Expand Down Expand Up @@ -219,8 +231,8 @@ def frames(self):

def duration_seconds(self):
"""
Returns the runtime duration of the video stream as a floating point number of seconds.
Returns 0.0 if not a video stream.
Returns the runtime duration of the audio/video stream as a floating point number of seconds.
Returns 0.0 if not an audio stream or video stream.
"""
if self.is_video() or self.is_audio():
try:
Expand Down