【问题标题】:Problem accessing user uploaded video in temporary memory访问临时内存中用户上传的视频时出现问题
【发布时间】:2011-10-17 22:16:16
【问题描述】:

我正在尝试使用 html 输入类型 file 和 python 模块 youtube-upload 将用户上传的视频提交到 youtube。提交表单时,处理方式如下:

if request.method == 'POST':
    video = request.FILES['file']

    v=str(video)

    command = 'youtube-upload --email=email@gmail.com --password=password --title=title --description=description --category=Sports ' + v

    r = subprocess.Popen(command,   stdout=subprocess.PIPE)
    v = r.stdout.read()

所以我认为问题在于我需要为视频提供更完整的路径。如果是这种情况,访问临时内存中的视频的路径是什么。

该命令的通用 for 是: youtube-upload --email=email --password=password --title=title --description=description --category=category video.avi

另外,我专门查看了 youtube api here,但如果有人可以提供更完整的解释,说明如何使用 api 在 python 中执行此操作,那就太棒了。不幸的是,网站上的指南只关注 xml。

根据 sacabuche 的评论进行编辑:

所以我现在的看法大致是:

def upload_video(request):
     if request.method == 'POST':
         video = request.FILE['file']
         v = video.temporary_file_path
         command = 'youtube-upload --email=email@gmail.com --password=password --title=title --description=description --category=Sports ' + v   

         r=subprocess.Popen(command, stdout=subprocess.PIPE)

         vid = r.stdout.read()
     else:
         form = VideoForm()
         request.upload_handlers.pop(0)
     return render_to_response('create_check.html', RequestContext(request, locals() ) )

v=video.temporary_file_path 得出错误'InMemoryUploadedFile' object has no attribute 'temporary_file_path'。所以视频仍然在临时内存中,我不知道应该调用什么对象 temporary_file_path 或如何获取所述对象。

【问题讨论】:

  • 您是否更改了您的 FILE_UPLOAD_HANDLERS?

标签: python django youtube


【解决方案1】:

实际上django将文件保存在内存中,但是大文件保存在一个路径中。
可以使用FILE_UPLOAD_MAX_MEMORY_SIZE在设置中定义“大文件”的大小

FILE_UPLOAD_HANDLERS 默认为:

("django.core.files.uploadhandler.MemoryFileUploadHandler",
 "django.core.files.uploadhandler.TemporaryFileUploadHandler",)

这给了我们两种可能性:

1。删除内存处理程序

删除..MemoryFileUploadHandler,但您的所有文件都将保存在一个临时文件中,这并不酷

2。动态修改处理程序

docs here

#views.py

def video_upload(request):
    # this removes the first handler (MemoryFile....)
    request.upload_handlers.pop(0)
    return _video_upload(request)

def _video_upload(request):
    ....

要获取文件路径你只需要video.temporary_file_path

【讨论】:

  • 这一切都写在docs
猜你喜欢
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多