我正在尝试使用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)
返回错误:
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.
知道问题是什么吗?
答案 0 :(得分:1)
根据您在评论中的测试,确认传递给函数的路径。如果你以这种方式运行它,你将获得文件的原始路径,至少应该解决找不到文件的错误。
from django.urls import path, include
from rest_framework import routers
from rest_framework.response import Response
from rest_framework.views import APIView
from . import views
class DocsView(APIView):
"""
RESTFul Documentation of my app
"""
def get(self, request, *args, **kwargs):
apidocs = {'api1title': request.build_absolute_uri('api1endpoint/'),
'api2title': request.build_absolute_uri('api2endpoint/'),
'api3title': request.build_absolute_uri('api3endpoint/'),
'api4title': request.build_absolute_uri('api4endpoint/'),
}
return Response(apidocs)
router = routers.DefaultRouter()
router.register('api1endpoint', views.API1ViewSet)
router.register('api2endpoint', views.API2ViewSet)
urlpatterns = [
path('', DocsView.as_view()),
path('', include(router.urls)),
path('api3endpoint/', views.API4View.as_view()),
path('api4endpoint/', views.API4View.as_view()),
]