Search code examples
pythondjangomoviepy

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


I'm trying to generate a thumbnail from an uploaded video using Moviepy. Here's my function (instance is the uploaded FileField (video)):

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 the error:

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

Full Traceback:

Traceback:

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.

Any idea what the problem is?


Solution

  • Based on your test in the comments, there is confirmation of what path is being passed into the function. If you run it this way, you'll get the original path to the file which should at least solve the file not found error.

    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