Skip to content

Calculate frames with framerate and duration #22

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 2 commits 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
26 changes: 22 additions & 4 deletions ffprobe/ffprobe.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import platform
import re
import subprocess
from datetime import datetime

from ffprobe.exceptions import FFProbeError
from .exceptions import FFProbeError


class FFProbe:
Expand Down Expand Up @@ -211,7 +212,13 @@ def frames(self):
raise FFProbeError('None integer frame count')
else:
# When N/A is returned, set frame_count to 0 too
frame_count = 0
if self.is_video():
# Calculate Video framerate from Time and FPS
fps = self.frame_rate()
length = self.duration_seconds()
frame_count = round(fps * length)
else:
frame_count = 0
else:
frame_count = 0

Expand All @@ -225,8 +232,13 @@ def duration_seconds(self):
if self.is_video() or self.is_audio():
try:
duration = float(self.__dict__.get('duration', ''))
except ValueError:
raise FFProbeError('None numeric duration')

except:
# Get duration from tag
duration_tag = self.__dict__.get('TAG:DURATION')
pt = datetime.strptime(duration_tag[:-4],'%H:%M:%S.%f')
duration = pt.second + pt.minute*60 + pt.hour*3600 + pt.microsecond*0.000001

else:
duration = 0.0

Expand Down Expand Up @@ -264,3 +276,9 @@ def bit_rate(self):
return int(self.__dict__.get('bit_rate', ''))
except ValueError:
raise FFProbeError('None integer bit_rate')

def frame_rate(self):
"""
Returns framerate
"""
return float(self.__dict__.get('framerate',''))