Moviepy: Save Video Thumbnail to ImageField Django

Created on 5 Feb 2017  路  3Comments  路  Source: Zulko/moviepy

Hello, thanks for this package.

I have a problem. How can I get the image 'thumbnail.jpg' and save it to my ImageField. Because when I print

     clip=VideoFileClip(path).save_frame('thumb.jpg',t=0.00)

it will return None.

When I also do this,

    >>> xm=MPV.objects.get(id=3)
    >>> xm.video_file_thumb=clip
    >>> xm.save()

It won't save any image. I will only see the image in my project path.

question

Most helpful comment

This is what I do:

def generate_thumbnails(tmp_directory):
    clip = editor.VideoFileClip(os.path.join(tmp_directory, "video.mp4"))
    for i in range(3):
        thumbnail = os.path.join(tmp_directory, "thumbnail_%s.png" % i)
        clip.save_frame(thumbnail, t=random.uniform(0.1, clip.duration))

All 3 comments

I'm back with the possible solution. Should have posted it here that same day but the busy schedule didn't help the situation.

For folks who might be facing this problem, here's what I did.

Code

   #I placed this in my views. And it will create a thumbnail when a form is saved mshare.save()
   #get thumbnail from video and save
                  mipp='media/'
                  mipx=mshare.travel_file
                  md_all='%s%s' % (mipp, mipx)
                  #the path to image
                  mf_way=os.path.abspath(md_all)
                  #change the \\ for / in order to get the correct path
                 the_mf_path= '/'.join(mf_way.split('\\'))
                 #video and get image
                 (filepath, filename)=os.path.split(the_mf_path)
                 (shortname, extension)=os.path.splitext(filename)
                 #get the video name and use it as file name. E.g aux.mp4 will be saved as aux.jpg
                 pl=shortname
                 xl='.jpg'
                 mf_name='%s%s' % (pl, xl)
                clip=VideoFileClip(the_mf_path).save_frame(mf_name,t=0.00)
                #path to already saved image frame 
                vo=os.path.join(os.path.abspath(clip), mf_name)
                 pz= '/'.join(vo.split('\\'))
                rep=open(pz, 'rb')
                django_file= File(rep)
                xp=mshare
                #video_file_thumb is the imagefield
                 xp.video_file_thumb.save(mf_name, django_file, save=True)
                #close the opened file
                rep.close()
                #you dont want to have images in your project path since it has been saved to the image folder
                os.remove(mf_name)

That's it. If you have a better solution, you can post them. If you have any question, let me know.

This is what I do:

def generate_thumbnails(tmp_directory):
    clip = editor.VideoFileClip(os.path.join(tmp_directory, "video.mp4"))
    for i in range(3):
        thumbnail = os.path.join(tmp_directory, "thumbnail_%s.png" % i)
        clip.save_frame(thumbnail, t=random.uniform(0.1, clip.duration))

It looks like a resolution was found.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Netherdrake picture Netherdrake  路  4Comments

Deng-deng-deng-deng picture Deng-deng-deng-deng  路  3Comments

tburrows13 picture tburrows13  路  3Comments

bobozar picture bobozar  路  4Comments

djevo1 picture djevo1  路  3Comments