MoviePy error: the file /tmp/testvideo.mp4 could not be found ! Please check that you entered the correct path

MoviePy error: the file /tmp/testvideo.mp4 could not be found ! Please check that you entered the correct path

我正在尝试使用 Moviepy 从上传的视频生成缩略图。这是我的功能(instance 是上传的 FileField (视频)):

def generate_thumbnail(instance):
    filename = instance.image.path.split('/')[-1]
    print(filename) #successfully prints name of uploaded file: "testvideo.mp4"
    thumbnail = VideoFileClip('/tmp/%s' % filename) #this line sparks the error
    name = random_string() + '.png'
    time = random.randrange(60)
    thumbnail.save_frame('media/' + name, t=time, withmask=True)
    instance.thumbnail = name

thumbnail = VideoFileClip('/tmp/%s' % filename) returns 错误:

OSError at /post/
MoviePy error: the file /tmp/testvideo.mp4 could not be found !
Please check that you entered the correct path.

完整追溯:

回溯:

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/Users/zorgan/Desktop/project/site/post/views.py" in post
  50.                     generate_thumbnail(instance)

File "/Users/zorgan/Desktop/project/site/functions/helper_functions.py" in generate_thumbnail
  45.     thumbnail = VideoFileClip('/tmp/%s' % filename)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/moviepy/video/io/VideoFileClip.py" in __init__
  81.                                          fps_source=fps_source)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/moviepy/video/io/ffmpeg_reader.py" in __init__
  32.                                    fps_source)

File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/moviepy/video/io/ffmpeg_reader.py" in ffmpeg_parse_infos
  272.                       "path.")%filename)

Exception Type: OSError at /post/
Exception Value: MoviePy error: the file /tmp/testvideo.mp4 could not be found !
Please check that you entered the correct path.

知道问题出在哪里吗?

根据您在评论中的测试,可以确认将什么路径传递到函数中。如果你运行这样,你会得到文件的原始路径,这至少应该解决找不到文件的错误。

def generate_thumbnail(instance):
    filename = instance.image.path
    thumbnail = VideoFileClip(filename)
    name = random_string() + '.png'
    time = random.randrange(60)
    thumbnail.save_frame('media/' + name, t=time, withmask=True)
    instance.thumbnail = name