add first version of generator, extracting frames and resizing them
This commit is contained in:
70
generator
Executable file
70
generator
Executable file
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""Video Thumbnail Generator
|
||||
|
||||
Usage:
|
||||
./generator <video> <interval> <width> <height> <columns> <output>
|
||||
./generator (-h | --help)
|
||||
./generator --version
|
||||
|
||||
Options:
|
||||
-h --help Show this screen.
|
||||
--version Show version.
|
||||
<video> Video filepath.
|
||||
<interval> Interval em seconds between frames.
|
||||
<width> Width of each thumbnail.
|
||||
<height> Height of each thumbnail.
|
||||
<columns> Total number of thumbnails per line.
|
||||
<output> Output.
|
||||
"""
|
||||
|
||||
from docopt import docopt
|
||||
from moviepy.editor import VideoFileClip
|
||||
from PIL import Image
|
||||
import glob, os, random, sys
|
||||
|
||||
|
||||
TMP_FRAMES_PATH="/tmp/frames/"
|
||||
|
||||
def generate_video_thumbnail(args):
|
||||
videoFileClip = VideoFileClip(args['<video>'])
|
||||
interval = int(args['<interval>'])
|
||||
size = (int(args['<width>']), int(args['<height>']))
|
||||
outputPrefix = get_output_prefix()
|
||||
|
||||
generate_frames(videoFileClip, interval, outputPrefix, size)
|
||||
# generate_sprite_from_images()
|
||||
|
||||
def generate_frames(videoFileClip, interval, outputPrefix, size):
|
||||
print "Extracting", int(videoFileClip.duration / interval), "frames (prefix:" + outputPrefix + ")"
|
||||
for i in range(0, int(videoFileClip.duration), interval):
|
||||
extract_frame(videoFileClip, i, outputPrefix, size)
|
||||
print "Frames extracted."
|
||||
|
||||
# framesMap = sorted(glob.glob(outputPrefix + "*.png"))
|
||||
# images = [Image.open(filename) for filename in framesMap]
|
||||
|
||||
def extract_frame(videoFileClip, moment, outputPrefix, size):
|
||||
sys.stdout.write(".")
|
||||
sys.stdout.flush()
|
||||
output = outputPrefix + str(moment) + ".png"
|
||||
videoFileClip.save_frame(output, t=int(moment))
|
||||
resize_image(output, size)
|
||||
|
||||
def resize_image(filename, size):
|
||||
sys.stdout.write("*")
|
||||
sys.stdout.flush()
|
||||
image = Image.open(filename)
|
||||
image.thumbnail(size, Image.ANTIALIAS)
|
||||
image.save(filename, "PNG")
|
||||
|
||||
def get_output_prefix():
|
||||
if not os.path.exists(TMP_FRAMES_PATH):
|
||||
os.makedirs(TMP_FRAMES_PATH)
|
||||
return TMP_FRAMES_PATH + ("%032x_" % random.getrandbits(128))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
arguments = docopt(__doc__, version='Naval Fate 2.0')
|
||||
generate_video_thumbnail(arguments)
|
||||
|
||||
Reference in New Issue
Block a user