File indexing completed on 2025-01-19 03:59:51

0001 import io
0002 import os
0003 
0004 import cv2
0005 import numpy
0006 from PIL import Image
0007 
0008 from .cairo import export_png
0009 from .gif import _log_frame
0010 from .base import exporter
0011 from ..parsers.baseporter import ExtraOption
0012 
0013 
0014 ## @see http://www.fourcc.org/codecs.php
0015 formats4cc = {
0016     "avi": cv2.VideoWriter_fourcc(*"XVID"),
0017     "mp4": cv2.VideoWriter_fourcc(*'MP4V'),
0018     #"mp4": cv2.VideoWriter_fourcc(*'X264'),
0019     "webm": cv2.VideoWriter_fourcc(*'VP80'),
0020 }
0021 
0022 
0023 @exporter("Video", list(formats4cc.keys()), [
0024     ExtraOption("format", default=None, help="Specific video format", choices=list(formats4cc.keys())),
0025 ], [], "video")
0026 def export_video(animation, fp, format=None):
0027     start = int(animation.in_point)
0028     end = int(animation.out_point)
0029     if format is None:
0030         fn = fp if isinstance(fp, str) else fp.name
0031         format = os.path.splitext(fn)[1][1:]
0032     fmt = formats4cc[format]
0033     video = cv2.VideoWriter(fp, fmt, animation.frame_rate, (animation.width, animation.height))
0034 
0035     for i in range(start, end+1):
0036         _log_frame(format, i, end)
0037         file = io.BytesIO()
0038         export_png(animation, file, i)
0039         file.seek(0)
0040         video.write(cv2.cvtColor(numpy.array(Image.open(file)), cv2.COLOR_RGB2BGR))
0041 
0042     _log_frame(format)
0043     video.release()