Refactored to pass PEP8 rules
This commit is contained in:
81
generator
81
generator
@@ -22,80 +22,97 @@ from docopt import docopt
|
||||
from moviepy.editor import VideoFileClip
|
||||
from PIL import Image
|
||||
from click import progressbar
|
||||
import glob, os, random, shutil, math, tempfile
|
||||
import glob
|
||||
import os
|
||||
import random
|
||||
import shutil
|
||||
import math
|
||||
import tempfile
|
||||
|
||||
|
||||
TMP_FRAMES_PATH = tempfile.mkdtemp()
|
||||
|
||||
def generate_video_thumbnail(args):
|
||||
videoFileClip = VideoFileClip(args['<video>'])
|
||||
|
||||
def generate_video_thumbnails(args):
|
||||
filepath = args['<video>']
|
||||
interval = int(args['<interval>'])
|
||||
size = (int(args['<width>']), int(args['<height>']))
|
||||
outputPrefix = get_output_prefix()
|
||||
generate_frames(videoFileClip, interval, outputPrefix, size)
|
||||
|
||||
output_prefix = get_output_prefix()
|
||||
columns = int(args['<columns>'])
|
||||
output = args['<output>']
|
||||
generate_sprite_from_frames(outputPrefix, columns, size, output)
|
||||
|
||||
def generate_frames(videoFileClip, interval, outputPrefix, size):
|
||||
print("Extracting", int(videoFileClip.duration / interval), "frames")
|
||||
frameCount = 0
|
||||
with progressbar(range(0, int(videoFileClip.duration), interval)) as items:
|
||||
video_file_clip = VideoFileClip(filepath)
|
||||
generate_frames(video_file_clip, interval, output_prefix, size)
|
||||
generate_sprite_from_frames(output_prefix, columns, size, output)
|
||||
|
||||
|
||||
def generate_frames(video_file_clip, interval, output_prefix, size):
|
||||
duration = video_file_clip.duration
|
||||
print("Extracting", int(duration / interval), "frames")
|
||||
frame_count = 0
|
||||
with progressbar(range(0, int(duration), interval)) as items:
|
||||
for i in items:
|
||||
extract_frame(videoFileClip, i, outputPrefix, size, frameCount)
|
||||
frameCount += 1
|
||||
extract_frame(video_file_clip, i, output_prefix, size, frame_count)
|
||||
frame_count += 1
|
||||
print("Frames extracted.")
|
||||
|
||||
def extract_frame(videoFileClip, moment, outputPrefix, size, frameCount):
|
||||
output = outputPrefix + ("%05d.png" % frameCount)
|
||||
videoFileClip.save_frame(output, t=int(moment))
|
||||
|
||||
def extract_frame(video_file_clip, moment, output_prefix, size, frame_count):
|
||||
output = output_prefix + ("%05d.png" % frame_count)
|
||||
video_file_clip.save_frame(output, t=int(moment))
|
||||
resize_frame(output, size)
|
||||
|
||||
|
||||
def resize_frame(filename, size):
|
||||
image = Image.open(filename)
|
||||
image = image.resize(size, Image.ANTIALIAS)
|
||||
image.save(filename)
|
||||
|
||||
def generate_sprite_from_frames(framesPath, columns, size, output):
|
||||
framesMap = sorted(glob.glob(framesPath + "*.png"))
|
||||
|
||||
masterWidth = size[0] * columns
|
||||
masterHeight = size[1] * int(math.ceil(float(len(framesMap)) / columns))
|
||||
def generate_sprite_from_frames(frames_path, columns, size, output):
|
||||
frames_map = sorted(glob.glob(frames_path + "*.png"))
|
||||
|
||||
master_width = size[0] * columns
|
||||
master_height = size[1] * int(math.ceil(float(len(frames_map)) / columns))
|
||||
|
||||
line, column, mode = 0, 0, 'RGBA'
|
||||
|
||||
try:
|
||||
finalImage = Image.new(mode=mode, size=(masterWidth, masterHeight), color=(0,0,0,0))
|
||||
finalImage.save(output)
|
||||
final_image = Image.new(
|
||||
mode=mode,
|
||||
size=(master_width, master_height),
|
||||
color=(0, 0, 0, 0)
|
||||
)
|
||||
final_image.save(output)
|
||||
except IOError:
|
||||
mode = 'RGB'
|
||||
finalImage = Image.new(mode=mode, size=(masterWidth, masterHeight))
|
||||
final_image = Image.new(mode=mode, size=(master_width, master_height))
|
||||
|
||||
for filename in framesMap:
|
||||
with Image.open(filename) as image:
|
||||
for filename in frames_map:
|
||||
with Image.open(filename) as image:
|
||||
|
||||
locationX = size[0] * column
|
||||
locationY = size[1] * line
|
||||
location_x = size[0] * column
|
||||
location_y = size[1] * line
|
||||
|
||||
finalImage.paste(image, (locationX, locationY))
|
||||
final_image.paste(image, (location_x, location_y))
|
||||
|
||||
column += 1
|
||||
|
||||
|
||||
if column == columns:
|
||||
line += 1
|
||||
column = 0
|
||||
|
||||
finalImage.save(output)
|
||||
final_image.save(output)
|
||||
shutil.rmtree(TMP_FRAMES_PATH, ignore_errors=True)
|
||||
print("Saved!")
|
||||
|
||||
|
||||
def get_output_prefix():
|
||||
if not os.path.exists(TMP_FRAMES_PATH):
|
||||
os.makedirs(TMP_FRAMES_PATH)
|
||||
return TMP_FRAMES_PATH + os.sep + ("%032x_" % random.getrandbits(128))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
arguments = docopt(__doc__, version='0.0.2')
|
||||
generate_video_thumbnail(arguments)
|
||||
|
||||
generate_video_thumbnails(arguments)
|
||||
|
||||
Reference in New Issue
Block a user