forked from funciton/py-ffmpeg-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_inspector.py
More file actions
236 lines (191 loc) · 5.99 KB
/
video_inspector.py
File metadata and controls
236 lines (191 loc) · 5.99 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import os
import re
import subprocess
from errors import CommandError
from errors import UnknownFormat
from errors import UnreadableFile
from errors import InputFileDoesNotExist
class VideoInspector(object):
_valid = False
def __init__(self, video_source, ffmpeg_bin="ffmpeg"):
if not os.path.exists(video_source):
raise InputFileDoesNotExist()
self.filename = os.path.basename(video_source)
self.path = os.path.dirname(video_source)
self.full_filename = video_source
output = subprocess.Popen('%s -i "%s"' % (
ffmpeg_bin,
self.full_filename
), stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True).communicate()
self._exec_response = output[1]
if re.search(
".*command\snot\sfound",
self._exec_response,
flags=re.IGNORECASE
):
raise CommandError()
self._metadata = re.search(
"(Input \#.*)\n(Must|At\sleast)",
self._exec_response,
flags=re.MULTILINE | re.DOTALL
)
if re.search(
"Unknown format",
self._exec_response,
flags=re.IGNORECASE
) or not self._metadata:
raise UnknownFormat()
if re.search(
"Duration: N\/A",
self._exec_response,
flags=re.IGNORECASE | re.MULTILINE
):
raise UnreadableFile()
self._metadata = self._metadata.group(1)
self._valid = True
def ffmpeg_version(self):
return self._exec_response.split("\n")[0]\
.split("version")[-1].split(",")[0].strip()
def ffmpeg_configuration(self):
return re.search(
"(\s*configuration:)(.*)\n",
self._exec_response
).group(2).strip()
def ffmpeg_libav(self):
lines = re.search(
"^(\s*lib.*)+",
self._exec_response,
flags=re.MULTILINE
).group(0).split("\n")
return [n.strip() for n in lines]
def ffmpeg_build(self):
return re.search(
"(\n\s*)(built on.*)(\n)",
self._exec_response
).group(2)
def container(self):
if not self._valid:
return
return re.search(
"Input \#\d+\,\s*(\S+),\s*from",
self._metadata
).group(1)
def dimension(self):
matching = re.search(
r'([0-9]{3,4})x([0-9]{3,4})',
self._exec_response
)
return [matching.group(1),matching.group(2)]
def raw_duration(self):
return re.search(
"Duration:\s*([0-9\:\.]+),",
self._exec_response
).group(1)
def duration(self):
if not self._valid:
return
units = self.raw_duration().split(":")
return (int(units[0]) * 60 * 60) + \
(int(units[1]) * 60) + \
int(float(units[2]))
def _bitrate_match(self):
return re.search("bitrate: ([0-9\.]+)\s*(.*)\s+", self._metadata)
def bitrate(self):
if not self._valid:
return
return int(self._bitrate_match().group(1))
def bitrate_units(self):
if not self._valid:
return
return self._bitrate_match().group(2)
def fps(self):
if not self._valid:
return
return re.search("([0-9\.]+) (fps|tb)", self._exec_response).group(1)
def video_stream(self):
m = re.search("\n\s*Stream.*Video:.*\n", self._exec_response)
if m:
return m.group(0).strip()
return
def _video_match(self):
if not self._valid:
return
m = re.search(
"Stream\s*(.*?)[,|:|\(|\[].*?\s*"\
"Video:\s*(.*?),\s*(.*?),\s*(\d*)x(\d*)",
self.video_stream()
)
if not m:
m = re.search(
"Stream\s*(.*?)[,|:|\(|\[].*?\s*Video:\s*(.*?),\s*(\d*)x(\d*)",
self.video_stream()
)
return m
def video_stream_id(self):
if not self._valid:
return
return self._video_match().group(1)
def video_codec(self):
if not self._valid:
return
return self._video_match().group(2)
def video_colorspace(self):
if not self._valid:
return
return self._video_match().group(3)
def width(self):
if not self._valid:
return
return int(self._video_match().group(4))
def height(self):
if not self._valid:
return
return int(self._video_match().group(5))
def resolution(self):
if not self._valid:
return
return "%sx%s" % (self.width(), self.height())
def audio_stream(self):
if not self._valid:
return
m = re.search("\n\s*Stream.*Audio:.*\n", self._exec_response)
if m:
return m.group(0).strip()
return
def _audio_match(self):
if not self._valid:
return
return re.search(
"Stream\s*(.*?)[,|:|\(|\[].*?\s*Audio:\s*(.*?),\s*([0-9\.]*) "\
"(\w*),\s*([a-zA-Z:]*)",
self.audio_stream()
)
def audio_codec(self):
if not self._valid:
return
return self._audio_match().group(2)
def audio_sample_rate(self):
if not self._valid:
return
return int(self._audio_match().group(3))
def audio_sample_units(self):
if not self._valid:
return
return self._audio_match().group(4)
def audio_channels_string(self):
if not self._valid:
return
return self._audio_match().group(5)
def audio_channels(self):
if not self._valid:
return
cs = self.audio_channels_string()
if cs == "stereo":
return 2
elif cs == "mono":
return 1
return
def audio_stream_id(self):
if not self._valid:
return
return self._audio_match().group(1)