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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
import logging
import os
import re
import tempfile
import threading
from lvc import execute
from lvc.widgets import idle_add
from lvc.settings import get_ffmpeg_executable_path
from lvc.utils import hms_to_seconds, convert_path_for_subprocess
logger = logging.getLogger(__name__)
class VideoFile(object):
def __init__(self, filename):
self.filename = filename
self.container = None
self.video_codec = None
self.audio_codec = None
self.width = None
self.height = None
self.duration = None
self.thumbnails = {}
self.parse()
def parse(self):
self.__dict__.update(
get_media_info(self.filename))
@property
def audio_only(self):
return self.video_codec is None
def get_thumbnail(self, completion, width=None, height=None, type_='.png'):
if self.audio_only:
# don't bother with thumbnails for audio files
return None
if width is None:
width = -1
if height is None:
height = -1
if self.duration is None:
skip = 0
else:
skip = min(int(self.duration / 3), 120)
key = (width, height, type_)
def complete(name):
self.thumbnails[key] = name
completion()
if key not in self.thumbnails:
temp_path = tempfile.mktemp(suffix=type_)
get_thumbnail(self.filename, width, height, temp_path, complete,
skip=skip)
return None
return self.thumbnails.get(key)
class Node(object):
def __init__(self, line="", children=None):
self.line = line
if not children:
self.children = []
else:
self.children = children
if ": " in line:
self.key, self.value = line.split(": ", 1)
else:
self.key = ""
self.value = ""
def add_node(self, node):
self.children.append(node)
def pformat(self, indent=0):
s = (" " * indent) + ("Node: %s" % self.line) + "\n"
for mem in self.children:
s += mem.pformat(indent + 2)
return s
def get_by_key(self, key):
if self.line.startswith(key):
return self
for mem in self.children:
ret = mem.get_by_key(key)
if ret:
return ret
return None
def __repr__(self):
return "<Node %s: %s>" % (self.key, self.value)
def get_indent(line):
length = len(line)
line = line.lstrip()
return (length - len(line), line)
def parse_ffmpeg_output(output):
"""Takes a list of strings and parses it into a loose AST-ish
thing.
ffmpeg output uses indentation levels to indicate a hierarchy of
data.
If there's a : in the line, then it's probably a key/value pair.
:param output: the content to parse as a list of strings.
:returns: a top level node of the ffmpeg output AST
"""
ast = Node()
node_stack = [ast]
indent_level = 0
for mem in output:
# skip blank lines
if len(mem.strip()) == 0:
continue
indent, line = get_indent(mem)
node = Node(line)
if indent == indent_level:
node_stack[-1].add_node(node)
elif indent > indent_level:
node_stack.append(node_stack[-1].children[-1])
indent_level = indent
node_stack[-1].add_node(node)
else:
for dedent in range(indent, indent_level, 2):
# make sure we never pop everything off the stack.
# the root should always be on the stack.
if len(node_stack) <= 1:
break
node_stack.pop()
indent_level = indent
node_stack[-1].add_node(node)
return ast
# there's always a space before the size and either a space or a comma
# afterwards.
SIZE_RE = re.compile(" (\\d+)x(\\d+)[ ,]")
def extract_info(ast):
info = {}
# logging.info("get_media_info: %s", ast.pformat())
input0 = ast.get_by_key("Input #0")
if not input0:
raise ValueError("no input #0")
foo, info['container'], bar = input0.line.split(', ', 2)
if ',' in info['container']:
info['container'] = info['container'].split(',')
metadata = input0.get_by_key("Metadata")
if metadata:
for key in ('title', 'artist', 'album', 'track', 'genre'):
node = metadata.get_by_key(key)
if node:
info[key] = node.line.split(':', 1)[1].strip()
major_brand_node = metadata.get_by_key("major_brand")
extra_container_types = []
if major_brand_node:
major_brand = major_brand_node.line.split(':')[1].strip()
extra_container_types = [major_brand]
else:
major_brand = None
compatible_brands_node = metadata.get_by_key("compatible_brands")
if compatible_brands_node:
line = compatible_brands_node.line.split(':')[1].strip()
extra_container_types.extend(line[i:i + 4]
for i in range(0, len(line), 4)
if line[i:i + 4] != major_brand)
if extra_container_types:
if not isinstance(info['container'], list):
info['container'] = [info['container']]
info['container'].extend(extra_container_types)
duration = input0.get_by_key("Duration:")
if duration:
_, rest = duration.line.split(':', 1)
duration_string, _ = rest.split(', ', 1)
logging.info("duration: %r", duration_string)
try:
hours, minutes, seconds = [
float(i) for i in duration_string.split(':')]
except ValueError:
if duration_string.strip() != "N/A":
logging.warn("Error parsing duration string: %r",
duration_string)
else:
info['duration'] = hms_to_seconds(hours, minutes, seconds)
for stream_node in duration.children:
stream = stream_node.line
if "Video:" in stream:
stream_number, video, data = stream.split(': ', 2)
video_codec = data.split(', ')[0]
if ' ' in video_codec:
video_codec, drmp = video_codec.split(' ', 1)
if 'drm' in drmp:
info.setdefault('has_drm', []).append('video')
info['video_codec'] = video_codec
match = SIZE_RE.search(data)
if match:
info["width"] = int(match.group(1))
info["height"] = int(match.group(2))
elif 'Audio:' in stream:
stream_number, video, data = stream.split(': ', 2)
audio_codec = data.split(', ')[0]
if ' ' in audio_codec:
audio_codec, drmp = audio_codec.split(' ', 1)
if 'drm' in drmp:
info.setdefault('has_drm', []).append('audio')
info['audio_codec'] = audio_codec
return info
def get_ffmpeg_output(filepath):
commandline = [get_ffmpeg_executable_path(),
"-i", convert_path_for_subprocess(filepath)]
logging.info("get_ffmpeg_output(): running %s", commandline)
try:
output = execute.check_output(commandline)
except execute.CalledProcessError as e:
if e.returncode != 1:
logger.exception("error calling %r\noutput:%s", commandline,
e.output)
# ffmpeg -i generally returns 1, so we ignore the exception and
# just get the output.
output = e.output
return output
def get_media_info(filepath):
"""Takes a file path and returns a dict of information about
this media file that it extracted from ffmpeg -i.
:param filepath: absolute path to the media file in question
:returns: dict of media info possibly containing: height, width,
container, audio_codec, video_codec
"""
logger.info('get_media_info: %r', filepath)
output = get_ffmpeg_output(filepath)
ast = parse_ffmpeg_output(output.splitlines())
info = extract_info(ast)
logger.info('get_media_info: %r', info)
return info
def get_thumbnail(filename, width, height, output, completion, skip=0):
name = 'Thumbnail - %r @ %sx%s' % (filename, width, height)
def run():
rv = get_thumbnail_synchronous(filename, width, height, output, skip)
idle_add(lambda: completion(rv))
t = threading.Thread(target=run, name=name)
t.start()
def get_thumbnail_synchronous(filename, width, height, output, skip=0):
executable = get_ffmpeg_executable_path()
filter_ = 'scale=%i:%i' % (width, height)
# bz19571: temporary disable: libav ffmpeg does not support this filter
# if 'ffmpeg' in executable:
# # supports the thumbnail filter, we hope
# filter_ = 'thumbnail,' + filter_
commandline = [executable,
'-ss', str(skip),
'-i', convert_path_for_subprocess(filename),
'-vf', filter_, '-vframes', '1', output]
try:
execute.check_output(commandline)
except execute.CalledProcessError as e:
logger.exception('error calling %r\ncode:%s\noutput:%s',
commandline, e.returncode, e.output)
return None
else:
return output
|