python-lottie  0.6.11+deved4e6c7
A framework to work with lottie files and telegram animated stickers (tgs)
video.py
Go to the documentation of this file.
1 import io
2 import os
3 
4 import cv2
5 import numpy
6 from PIL import Image
7 
8 from .cairo import export_png
9 from .gif import _log_frame
10 from .base import exporter
11 from ..parsers.baseporter import ExtraOption
12 
13 
14 ## @see http://www.fourcc.org/codecs.php
15 formats4cc = {
16  "avi": cv2.VideoWriter_fourcc(*"XVID"),
17  "mp4": cv2.VideoWriter_fourcc(*'MP4V'),
18  #"mp4": cv2.VideoWriter_fourcc(*'X264'),
19  "webm": cv2.VideoWriter_fourcc(*'VP80'),
20 }
21 
22 
23 @exporter("Video", list(formats4cc.keys()), [
24  ExtraOption("format", default=None, help="Specific video format", choices=list(formats4cc.keys())),
25 ], [], "video")
26 def export_video(animation, fp, format=None):
27  start = int(animation.in_point)
28  end = int(animation.out_point)
29  if format is None:
30  fn = fp if isinstance(fp, str) else fp.name
31  format = os.path.splitext(fn)[1][1:]
32  fmt = formats4cc[format]
33  video = cv2.VideoWriter(fp, fmt, animation.frame_rate, (animation.width, animation.height))
34 
35  for i in range(start, end+1):
36  _log_frame(format, i, end)
37  file = io.BytesIO()
38  export_png(animation, file, i)
39  file.seek(0)
40  video.write(cv2.cvtColor(numpy.array(Image.open(file)), cv2.COLOR_RGB2BGR))
41 
42  _log_frame(format)
43  video.release()
def export_png(animation, fp, frame=0, dpi=96)
Definition: cairo.py:17
def export_video(animation, fp, format=None)
Definition: video.py:26