我面临一个问题,我希望能够获得一些见解。我在我的Web应用程序中加密了视频,将视频解密为BytesIO内存对象,然后进行base64编码,允许视频通过HTML5视频播放器播放给用户。
视频本身永远不应该在文件系统上真正解密(例如)。但是,如果我尝试加载大于50MB的视频文件,则会在web2py中显示“MemoryError”消息。我该如何解决这个问题?
(我相信它在base64.b64encode方法中失败了)
我正在使用Python27和Web2py
开发它import io
import base64
cry_upload = request.args(0)
cry = db(db.video_community.id == int(cry_upload)).select(db.video_community.ALL).first()
if cry:
if auth.user and auth.user.id not in (cry.community.members):
return redirect(URL('default', 'cpanel'), client_side = True)
filepath = os.path.join(request.folder, 'uploads/', '%s' % cry.file_upload)
form = FORM(LABEL('The Associated Key'), INPUT(_name='key', value="", _type='text', _class='string'), INPUT(_type='submit', _class='btn btn-primary'), _class='form-horizontal')
if form.accepts(request, session):
outfile = io.BytesIO()
with open(filepath, 'rb') as infile:
key = str(form.vars.key).rstrip()
try:
decrypt(infile, outfile, key)
except:
session.flash = 'Decryption error. Please verify your key'
return redirect(URL('default', 'cpanel'), client_side = True)
outfile.seek(0)
msg = base64.b64encode(outfile.getvalue().encode('utf-8'))
outfile.close()
response.flash = 'Success! Your video is now playable'
return dict(form=form, cry=cry, msg=msg)
非常感谢任何建议。
解密功能
由How to AES encrypt/decrypt files using Python/PyCrypto in an OpenSSL-compatible way?提供
from hashlib import md5
from Crypto.Cipher import AES
from Crypto import Random
from Crypto.Random import random
def derive_key_and_iv(password, salt, key_length, iv_length):
d = d_i = ''
while len(d) < key_length + iv_length:
d_i = md5(d_i + password + salt).digest()
d += d_i
return d[:key_length], d[key_length:key_length+iv_length]
def decrypt(in_file, out_file, password, key_length=32):
bs = AES.block_size
salt = in_file.read(bs)[len('Salted__'):]
key, iv = derive_key_and_iv(password, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv)
next_chunk = ''
finished = False
while not finished:
chunk, next_chunk = next_chunk, cipher.decrypt(in_file.read(1024 * bs))
if len(next_chunk) == 0:
padding_length = ord(chunk[-1])
if padding_length < 1 or padding_length > bs:
raise ValueError("bad decrypt pad (%d)" % padding_length)
# all the pad-bytes must be the same
if chunk[-padding_length:] != (padding_length * chr(padding_length)):
# this is similar to the bad decrypt:evp_enc.c from openssl program
raise ValueError("bad decrypt")
chunk = chunk[:-padding_length]
finished = True
out_file.write(chunk)
以下是视图中的视频播放器
{{if msg != None:}}
<div class="row" style="margin-top:20px;">
<div class="video-js-box">
<video class="video-js vjs-default-skin" data-setup='{"controls": true, "autoplay": false, "preload": "none", "poster": "https://www.cryaboutcrypt.ninja/static/crying.png", "width": 720, "height": 406}'>
<source type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' src="data:video/mp4;base64,{{=msg}}" />
</video>
</div>
</div>
{{pass}}
如上所述,我将base64编码的BytesIO数据传递给播放器。 @Anthony - 我不是100%肯定你的建议。
这是发生的MemoryError
File "/usr/lib/python2.7/base64.py", line 53, in b64encode
encoded = binascii.b2a_base64(s)[:-1]
MemoryError
答案 0 :(得分:0)
不是将基本64位编码的视频文件嵌入页面的HTML中,而是提供一个URL作为视频播放器源,并从该URL流式传输文件:
<source type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'
src="{{=URL('default', 'video', args=request.args(0))}}" />
然后,您的大多数代码将移至default.py控制器中的video
函数。只需通过response.stream()
返回outfile
,而不是对视频进行编码。