Creating thumbnails from video files with Python

前端 未结 7 1790
陌清茗
陌清茗 2020-12-24 07:48

I need to create thumbnails for a video file once I\'ve uploaded to a webapp running python.

How would I go about this... I need a library that can basically either

相关标签:
7条回答
  • 2020-12-24 08:17
    import cv2
    vcap = cv2.VideoCapture(filename)
    res, im_ar = vcap.read()
    while im_ar.mean() < threshold and res:
          res, im_ar = vcap.read()
    im_ar = cv2.resize(im_ar, (thumb_width, thumb_height), 0, 0, cv2.INTER_LINEAR)
    #to save we have two options
    #1) save on a file
    cv2.imwrite(save_on_filename, im_ar)
    #2)save on a buffer for direct transmission
    res, thumb_buf = cv2.imencode('.png', im_ar)
    # '.jpeg' etc are permitted
    #get the bytes content
    bt = thumb_buf.tostring()
    

    "threshold" is an integer. When you get a video frame it can be very black, white etc to get some good thumbnail you can specify the mean value of all the pixel in the frame.

    0 讨论(0)
  • 2020-12-24 08:24

    You can use ffvideo

    from ffvideo import VideoStream
    pil_image = VideoStream('0.flv').get_frame_at_sec(5).image()
    pil_image.save('frame5sec.jpeg')
    
    0 讨论(0)
  • 2020-12-24 08:27

    I could not install ffvideo on OSX Sierra so i decided to work with ffmpeg.

    OSX:

    brew install ffmpeg
    

    Linux:

    apt-get install ffmpeg
    

    Python 3 Code:

    import subprocess
    video_input_path = '/your/video.mp4'
    img_output_path = '/your/image.jpg'
    subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', '-vframes', '1', img_output_path])
    
    0 讨论(0)
  • 2020-12-24 08:31

    You could use the Youtube API for storage and transcoding and grab the feed thumbnails for free. Honestly, that's the easiest way to handle online video and I'm not just shilling a 3rd party service, I'm a very happy user of that API and the internal video paths I was able to delete thanks to it.

    0 讨论(0)
  • 2020-12-24 08:34

    You can use the Python script pyvideothumbnailer found on GitHub, which uses MoviePy, MediaInfo and the PIL fork Pillow. It should do the complete job for you.

    0 讨论(0)
  • 2020-12-24 08:35

    Look into PythonMagick, a Python interface to ImageMagick. That should have what you need. (Disclaimer: I haven't used the Python interface before, but I know ImageMagick is good mojo.)

    0 讨论(0)
提交回复
热议问题