问题
I wanted to apply Django's Signals to the following model:
class Video(models.Model):
created = models.DateTimeField(auto_now_add=True)
text = models.CharField(max_length=100, blank=True)
image = models.ImageField(upload_to='Images/',blank=True)
video = models.FileField(upload_to='Videos/',blank=True)
gif = models.FileField(upload_to = 'Videos/', blank = True)
class Meta:
ordering = ('created', )
The reason why I use signals: Whenever a Video
object gets created, we get notified and then we update its gif
field using its video
field with the help of MoviePy
.
So, after reading this , I created a signals.py
with the following content:
# videos/signals.py
from moviepy.video.io.VideoFileClip import VideoFileClip
from videos.models import Video
from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=Video)
def create_gif_from_video(sender, instance, created, **kwargs):
if created:
instance.gif = VideoFileClip(instance.video.name).subclip(0,2).write_gif("myGif.gif")
instance.save()
Then in videos/apps.py
, I did this:
from django.apps import AppConfig
class VideosConfig(AppConfig):
name = 'videos'
def ready(self):
import videos.signals
And in videos/\__init\__.py
, I did this:
default_app_config = 'videos.apps.VideosConfig'
But I get the following error:
Internal Server Error: /videos/upload/
Traceback (most recent call last):
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/rest_framework/generics.py", line 242, in post
return self.create(request, *args, **kwargs)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/rest_framework/mixins.py", line 19, in create
self.perform_create(serializer)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/rest_framework/mixins.py", line 24, in perform_create
serializer.save()
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 213, in save
self.instance = self.create(validated_data)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 932, in create
instance = ModelClass._default_manager.create(**validated_data)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/db/models/query.py", line 422, in create
obj.save(force_insert=True, using=self.db)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/db/models/base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/db/models/base.py", line 790, in save_base
update_fields=update_fields, raw=raw, using=using,
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 175, in send
for receiver in self._live_receivers(sender)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 175, in <listcomp>
for receiver in self._live_receivers(sender)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/videos/signals.py", line 10, in create_gif_from_video
instance.gif = VideoFileClip(instance.video.name).subclip(0,2).write_gif("myGif.gif")
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/moviepy/video/io/VideoFileClip.py", line 91, in __init__
fps_source=fps_source)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/moviepy/video/io/ffmpeg_reader.py", line 33, in __init__
fps_source)
File "/home/ac3l1k/Desktop/PROJECT/djangoProject/env/lib/python3.6/site-packages/moviepy/video/io/ffmpeg_reader.py", line 276, in ffmpeg_parse_infos
"path.")%filename)
OSError: MoviePy error: the file Videos/2020-03-25-16-16-21-524_JTyCYlg.mp4 could not be found!
Please check that you entered the correct path.
MoviePy can not find the path of the created video file. Why ? It must be there because the signal method gets notified after the Video instance is created. What is wrong and how can I fix it?
Thanks in advance :)
来源:https://stackoverflow.com/questions/60855740/create-a-gif-from-video-using-django-signals-and-moviepy