Django Rest Framework - How to create a GIF file from an uploaded video

1 answer

I have a Django Rest Framework + Android App where the user can upload some video files to my local Django development server. I wanted to show the videos as GIF files via a RecyclerView. I thought that creating the GIF files on server side (Django) would be more efficient than creating them on the client side (device).

So I want to make a GIF file from an uploaded video before storing the video file. How can I do that in Django ?

Here is my

class Video(models.Model):     created = models.DateTimeField(auto_now_add=True)     text = models.CharField(max_length=100, blank=True)     video = models.FileField(upload_to='Videos/',                               blank=True)      class Meta:         ordering = ('created', ) 

This is how my looks like:

# used for displaying & creating video items  class VideoList(generics.ListCreateAPIView):     queryset = Video.objects.all()     serializer_class = VideoSerializer     parser_classes = (MultiPartParser, )      def list(self, request, *args, **kwargs):          queryset = self.filter_queryset(self.get_queryset())         page = self.paginate_queryset(queryset)          if page is not None:             serializer = self.get_serializer(page, many=True)             return self.get_paginated_response(          serializer = self.get_serializer(queryset, many=True)          # the only part we change         return Response({'videos':}) 

And this is the with the url the app uses to upload the video file:

urlpatterns = [     ...     path('upload/', views.VideoList.as_view()) ] 

The upload of videos from my app works, but I don't know what to do to create also a GIF file version of the uploaded video. Which is the best way to achieve that & how can I embed it into my Django-related files such as or

I would very thankful for any hint or any advice. Hope someone can help...

All answers to this question, which has the identifier 60837426

The best answer:

An example to convert a video into a GIF and using them in Django.

from moviepy.editor import *  clip = (VideoFileClip("frozen_trailer.mp4")             .subclip((1,22.65),(1,23.2))             .resize(0.3)) clip.write_gif("use_your_head.gif")  kris_sven = (VideoFileClip("frozen_trailer.mp4")                  .subclip((1,13.4),(1,13.9))                  .resize(0.5)                  .crop(x1=145,x2=400)) # remove left-right borders kris_sven.write_gif("kris_sven.gif")  anna_olaf = (VideoFileClip("frozen_trailer.mp4")              .subclip(87.9,88.1)              .speedx(0.5) # Play at half speed              .resize(.4))  snapshot = (anna_olaf             .crop(x2= anna_olaf.w/2) # remove right half             .to_ImageClip(0.2) # snapshot of the clip at t=0.2s             .set_duration(anna_olaf.duration))  composition = CompositeVideoClip([anna_olaf, snapshot]) composition.write_gif('anna_olaf.gif', fps=15)  import as dw  anna_kris = (VideoFileClip("frozen_trailer.mp4", audio=False)              .subclip((1,38.15),(1,38.5))              .resize(.5))  # coordinates p1,p2 define the edges of the mask mask = dw.color_split(anna_kris.size, p1=(445, 20), p2=(345, 275),                       grad_width=5) # blur the mask's edges  snapshot = (anna_kris.to_ImageClip()             .set_duration(anna_kris.duration)             .set_mask(ImageClip(mask, ismask=True))  composition = CompositeVideoClip([anna_kris,snapshot]).speedx(0.2) # 'fuzz' (0-100) below is for gif compression composition.write_gif('anna_kris.gif', fps=15, fuzz=3)  def time_symetrize(clip):     """ Returns the clip played forwards then backwards. In case     you are wondering, vfx (short for Video FX) is loaded by     >>> from moviepy.editor import * """     return concatenate([clip, clip.fx( vfx.time_mirror )])  clip = (VideoFileClip("frozen_trailer.mp4", audio=False)         .subclip(36.5,36.9)         .resize(0.5)         .crop(x1=189, x2=433)         .fx( time_symetrize ))  clip.write_gif('sven.gif', fps=15, fuzz=2)  olaf = (VideoFileClip("frozen_trailer.mp4", audio=False)         .subclip((1,21.6),(1,22.1))         .resize(.5)         .speedx(0.5)         .fx( time_symetrize ))  # Many options are available for the text (requires ImageMagick) text = (TextClip("In my nightmares\nI see rabbits.",                  fontsize=30, color='white',                  font='Amiri-Bold', interline=-25)         .set_pos((20,190))         .set_duration(olaf.duration))  composition = CompositeVideoClip( [olaf, text] ) composition.write_gif('olaf.gif', fps=10, fuzz=2)  castle = (VideoFileClip("frozen_trailer.mp4", audio=False)           .subclip(22.8,23.2)           .speedx(0.2)           .resize(.4))  d = castle.duration castle = castle.crossfadein(d/2)  composition = (CompositeVideoClip([castle,                                    castle.set_start(d/2),                                    castle.set_start(d)])                .subclip(d/2, 3*d/2))  composition.write_gif('castle.gif', fps=5,fuzz=5) 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data