2015-11-06 59 views
0

我面臨着一個我希望能夠獲得一些洞察力的問題。我在我的網絡應用程序中加密了視頻,將視頻解密爲BytesIO內存對象,然後使用base64編碼,從而允許用戶通過HTML5視頻播放器播放視頻。帶有base64的大文件的MemoryError

視頻本身不應該真正在文件系統上解密(每說)。但是,如果我嘗試加載大於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}} 

視頻播放器注意到我傳遞編碼BytesIO數據給玩家以base64。 @Anthony - 我不是100%確定你的建議。

這裏是發生

File "/usr/lib/python2.7/base64.py", line 53, in b64encode 
    encoded = binascii.b2a_base64(s)[:-1] 
MemoryError 
+0

你可以給解密函數的定義嗎? –

+0

你如何確定它是一個內存問題?這是託管在什麼上的?你有多少內存可用於你的應用程序?你有多少個併發用戶? –

+0

增加了解密方法。 @Demian - 加載視頻大於建議大小時收到MemoryError。我是這個Web應用程序的唯一用戶。我不知道我的服務器的規格。 – Maurth

回答

0

而不是嵌入在頁面的HTML底座64編碼的視頻文件的的MemoryError,也許提供URL作爲視頻播放器源和流從該網址的文件:

 <source type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' 
     src="{{=URL('default', 'video', args=request.args(0))}}" /> 

然後,大部分代碼將移到default.py控制器中的video函數。而不是基於64編碼視頻,只需通過response.stream()返回outfile

相關問題