Django - Trying to create a GIF from uploaded video

只愿长相守 提交于 2020-04-18 03:49:23

问题


I have a model class representing a video file. It has also a GIF field. I want to create also a GIF file of each video file. Before saving, I fill the gif field with the content of video field in the save() method like this:

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)

    def save(self, *args, **kwargs):
        # FileField's boolean value is true when there is a video file 
        if self.video:
            myGif = VideoFileClip(self.video).subclip(0,2).write_gif("myGif.gif")
            self.gif = myGif   
        super().save(*args, **kwargs)


    class Meta:
        ordering = ('created', )

So, using moviepy, I create a GIF file and store it into the gif field.

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 video
    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/videos/models.py", line 24, in save
    myGif = VideoFileClip(self.video).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 248, in ffmpeg_parse_infos
    is_GIF = filename.endswith('.gif')
AttributeError: 'FieldFile' object has no attribute 'endswith'
[25/Mar/2020 15:21:24] "POST /videos/upload/ HTTP/1.1" 500 19546

来源:https://stackoverflow.com/questions/60852102/django-trying-to-create-a-gif-from-uploaded-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!