forked from MediaCrush/MediaCrush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.py
307 lines (273 loc) · 9.38 KB
/
migrate.py
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
296
297
298
299
300
301
302
303
304
305
306
307
from mediacrush.objects import File, RedisObject
from mediacrush.database import r, _k
from mediacrush.fileutils import file_storage
from mediacrush.processing.invocation import Invocation
from mediacrush.config import _cfg, _cfgi
import sys
import json
# This does file type detection by inspection rather than extensions
# It will return 'video' or 'audio' for anything ffmpeg supports
# It will return the full mimetype for PNG, JPG, BMP, and SVG files, which we have
# special procedures to optimize
# It will return 'image' for all other images that imagemagick supports
# It will return None for things we can't handle
# Will also try to detect plaintext files (including various kinds of source code)
# These will return 'text/x-python', for example, if we can detect the source code type
# If we can't and we know that it's plaintext, we'll use 'text/plain'
# The first goal is to detect files via inspection so that we aren't dependent on
# extensions, and so that we can catch naughty files before we even try to process them
# The second goal is to hopefully have support for far more media types. This should
# allow us to broaden our supported media types to support basically everything.
# We need to convert uploaded media to browser-friendly formats. We can do videos and
# audio with ffmpeg, and we can do images with imagemagick.
# Returns:
# {
# 'type': 'video' | 'audio' | 'image' | full mimetype,
# 'extra': { }, # type-specific extra data for the processor
# 'flags': { 'autoplay': True, 'loop': True, 'mute': False } # type-specific
# }
def detect(path):
result = detect_ffprobe(path)
if result != None:
return result
# ffprobe can't identify images without examining the extensions, and doesn't
# support SVG at all
# Note that ffprobe *can* confirm the integrity of images if it knows the extension
# first, so we allow it to resolve images if the provided extension makes sense.
result = detect_imagemagick(path)
if result != None:
return result
result = detect_plaintext(path)
if result != None:
return result
return None
# This does *not* work with any containers that only have images in them, by design.
def detect_ffprobe(path):
# IMPORTANT: jdiez, this doesn't work when the path has spaces in it
# I tried wrapping {0} in quotes to no avail
a = Invocation('ffprobe -print_format json -loglevel quiet -show_format -show_streams {0}')
a(path)
a.run()
if a.returncode or a.exited:
return None
result = json.loads(a.stdout[0])
if result["format"]["nb_streams"] == 1:
return detect_stream(result["streams"][0])
# Try to guess what it is from the streams inside
# I've done a little more detection than we really need to, for things like subtitles
audio_streams = 0
video_streams = 0
image_streams = 0
subtitle_streams = 0
font_streams = 0
# We shouldn't penalize people for unknown streams, I just figured we could make a note of it
unknown_streams = 0
for stream in result["streams"]:
s = detect_stream(stream)
t = s['type']
if not s or not t:
unknown_streams += 1
else:
if t.startswith('image'):
image_streams += 1
elif t == 'video':
video_streams += 1
elif t == 'audio':
audio_streams += 1
elif t == 'subtitle':
subtitle_streams += 1
elif t == 'font':
font_streams += 1
else:
unknown_streams += 1
if audio_streams == 1 and video_streams == 0:
return {
'type': 'audio',
'extra': { 'has_audio': True, 'has_video': False },
'flags': None
}
if video_streams > 0:
return {
'type': 'video',
'extra': { 'has_audio': audio_streams > 0, 'has_video': True },
'flags': {
'autoplay': False,
'loop': False,
'mute': False,
}
}
return None
def detect_stream(stream):
# This will return None for things it doesn't recognize, or:
# 'image/whatever' (uses full mimetype for images)
# 'video'
# 'audio'
# 'subtitle'
# 'font'
if not "codec_name" in stream:
if "tags" in stream and "mimetype" in stream["tags"]:
if stream["tags"]["mimetype"] == 'application/x-truetype-font':
return {
'type': 'font',
'extra': stream["tags"]["filename"],
'flags': None
}
else:
if stream["codec_name"] == 'mjpeg':
return {
'type': 'image/jpeg',
'extra': None,
'flags': None
}
if stream["codec_name"] == 'png':
return {
'type': 'image/png',
'extra': None,
'flags': None
}
if stream["codec_name"] == 'bmp':
return None
if stream["codec_name"] == 'gif':
return {
'type': 'video',
'extra': { 'has_audio': False, 'has_video': True },
'flags': {
'autoplay': True,
'loop': True,
'mute': True
}
}
if stream["codec_type"] == 'video':
return {
'type': 'video',
'extra': { 'has_audio': False, 'has_video': True },
'flags': {
'autoplay': False,
'loop': False,
'mute': False
}
}
if stream["codec_type"] == 'audio':
return {
'type': 'audio',
'extra': { 'has_audio': True, 'has_video': False },
'flags': {
'autoplay': False,
'loop': False,
'mute': False
}
}
if stream["codec_type"] == 'subtitle':
return {
'type': 'subtitle',
'extra': { 'codec_name': stream['codec_name'] },
'flags': None
}
return None
def detect_imagemagick(path):
a = Invocation('identify -verbose {0}')
a(path)
a.run()
if a.returncode or a.exited:
return None
result = a.stdout[0].split('\n')
# Check for an actual mimetype first
mimetype = None
for line in result:
line = line.lstrip(' ')
if line.startswith('Mime type: '):
mimetype = line[11:]
if mimetype in [ 'image/png', 'image/jpeg' ]:
return {
'type': mimetype,
'extra': None,
'flags': None
}
# Check for SVG, it's special
for line in result:
line = line.lstrip(' ')
if line == 'Format: SVG (Scalable Vector Graphics)':
return {
'type': 'image/svg+xml',
'extra': None,
'flags': None
}
return {
'type': 'image',
'extra': None,
'flags': None
}
def detect_plaintext(path):
a = Invocation('file -b -e elf -e tar -e compress -e cdf -e apptype -i {0}')
a(path)
a.run()
if a.returncode or a.exited:
return None
result = a.stdout[0]
if result.startswith('text/x-') or result == 'text/plain':
return {
'type': result[:result.find(';')],
'extra': None,
'flags': None
}
return None
class BitVector(object):
shifts = {}
_vec = 0
def __init__(self, names, iv=0):
for i, name in enumerate(names):
self.shifts[name] = i
self._vec = iv
def __getattr__(self, name):
if name not in self.shifts:
raise AttributeError(name)
value = self._vec & (1 << self.shifts[name])
return True if value != 0 else False
def __setattr__(self, name, v):
if name == '_vec':
object.__setattr__(self, '_vec', v)
return
if name not in self.shifts:
raise AttributeError(name)
newvec = self._vec
currentval = getattr(self, name)
if currentval == v:
return # No change needed
if currentval == True:
# Turn this bit off
newvec &= ~(1 << self.shifts[name])
else:
# Turn it on
newvec |= (1 << self.shifts[name])
object.__setattr__(self, '_vec', newvec)
def as_dict(self):
return dict((flag, getattr(self, flag)) for flag in self.shifts)
def __int__(self):
return self._vec
flags_per_processor = {
'video': ['autoplay', 'loop', 'mute']
}
if __name__ == '__main__':
files = File.get_all()
count = len(files)
print "About to process %d files." % count
done = 0
errors = []
for f in files:
h = f.hash
configvector = 0
try:
path = file_storage(f.original)
result = detect(path)
if result and result['flags']:
bv = BitVector(flags_per_processor.get(result['type'], []))
for flag, value in result['flags'].items():
setattr(bv, flag, value)
configvector = int(bv)
print h, result['type'], int(bv)
done += 1
except Exception, e:
errors.append(h)
k = _k("file.%s" % h)
r.hset(k, "configvector", configvector)
print "%d/%d files processed, errors:" % (done, count), errors