forked from dmcallejo/ffprobe-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFStream.py
More file actions
115 lines (93 loc) · 3.26 KB
/
Copy pathFFStream.py
File metadata and controls
115 lines (93 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
Python wrapper for ffprobe command line tool. ffprobe must exist in the path.
"""
from ffprobe.exceptions import FFProbeError
class FFStream:
"""
An object representation of an individual stream in a multimedia file.
"""
def __init__(self, data_dict):
self._data = data_dict
def __repr__(self):
#if self.is_video():
# template = "<Stream: #{index} [{codec_type}] {codec_long_name}, {framerate}, ({width}x{height})>"
#elif self.is_audio():
# template = "<Stream: #{index} [{codec_type}] {codec_long_name}, channels: {channels} ({channel_layout}), " \
# "{sample_rate}Hz> "
#elif self.is_subtitle() or self.is_attachment():
# template = "<Stream: #{index} [{codec_type}] {codec_long_name}>"
#else:
# template = ''
template = "<Stream: #{index} [{codec_type}] {codec_long_name}>"
return template.format(**self._data)
def stream_index(self):
"""
The stream index
"""
return int(self._data['index'])
def frames(self):
"""
Returns the length of a video stream in frames.
"""
try:
return int(self._data['nb_frames'])
except:
raise FFProbeError('No integer frame count')
def duration_seconds(self):
"""
Returns the runtime duration of the stream as a floating point number of seconds.
"""
try:
return float(self._data['duration'])
except:
pass
try:
dur_tuple = self._data['TAG:DURATION'].split(':')
return float(dur_tuple[0])*3600 + float(dur_tuple[1])*60 + float(dur_tuple[2])
except:
raise FFProbeError('No numeric duration')
def codec(self):
"""
Returns a string representation of the stream codec.
"""
return self._data.get('codec_name', None)
def codec_description(self):
"""
Returns a long representation of the stream codec.
"""
return self._data.get('codec_long_name', None)
def codec_tag(self):
"""
Returns a short representative tag of the stream codec.
"""
return self._data.get('codec_tag_string', None)
def bit_rate(self):
"""
Returns bit_rate as an integer in bps
"""
try:
return int(self._data['bit_rate'])
except:
raise FFProbeError('No integer bit_rate')
def max_bit_rate(self):
"""
Return max_bit_rate as an integer in bps
"""
try:
return int(self._data['max_bit_rate'])
except:
raise FFProbeError('No integer bit_rate')
def start_pts(self):
return int(self._data['start_pts'])
def start_time(self):
return float(self._data['start_time'])
def duration_ts(self):
return int(self._data['duration_ts'])
def language(self):
return self._data['TAG:language']
def frame_count(self):
return int(self._data['nb_read_frames'])
def packet_count(self):
return int(self._data['nb_read_packets'])
def tag_encoder(self):
return self._data['TAG:encoder']