class_docstr: A Clip that serves as "subtitle track" in videos.
  
  One particularity of this class is that the images of the
  subtitle texts are not generated beforehand, but only if
  needed.
  
  Parameters
  ----------
  
  subtitles
    Either the name of a file as a string or path-like object, or a list
  
  encoding
    Optional, specifies srt file encoding.
    Any standard Python encoding is allowed (listed at
    https://docs.python.org/3.8/library/codecs.html#standard-encodings)
  
  Examples
  --------
  
  >>> from moviepy.video.tools.subtitles import SubtitlesClip
  >>> from moviepy.video.io.VideoFileClip import VideoFileClip
  >>> generator = lambda text: TextClip(text, font='Georgia-Regular',
  ...                                   font_size=24, color='white')
  >>> sub = SubtitlesClip("subtitles.srt", generator)
  >>> sub = SubtitlesClip("subtitles.srt", generator, encoding='utf-8')
  >>> myvideo = VideoFileClip("myvideo.avi")
  >>> final = CompositeVideoClip([clip, subtitles])
  >>> final.write_videofile("final.mp4", fps=myvideo.fps)
class_docstr_tok: ["A","Clip","that","serves","as","``","subtitle","track","''","in","videos",".","One","particularity","of","this","class","is","that","the","images","of","the","subtitle","texts","are","not","generated","beforehand",",","but","only","if","needed",".","Parameters","--","--","--","--","--","subtitles","Either","the","name","of","a","file","as","a","string","or","path-like","object",",","or","a","list","encoding","Optional",",","specifies","srt","file","encoding",".","Any","standard","Python","encoding","is","allowed","(","listed","at","https",":","\/\/docs.python.org\/3.8\/library\/codecs.html","#","standard-encodings",")","Examples","--","--","--","--",">",">",">","from","moviepy.video.tools.subtitles","import","SubtitlesClip",">",">",">","from","moviepy.video.io.VideoFileClip","import","VideoFileClip",">",">",">","generator","=","lambda","text",":","TextClip","(","text",",","font='Georgia-Regular","'",",","...","font_size=24",",","color='white","'",")",">",">",">","sub","=","SubtitlesClip","(","``","subtitles.srt","''",",","generator",")",">",">",">","sub","=","SubtitlesClip","(","``","subtitles.srt","''",",","generator",",","encoding='utf-8","'",")",">",">",">","myvideo","=","VideoFileClip","(","``","myvideo.avi","''",")",">",">",">","final","=","CompositeVideoClip","(","[","clip",",","subtitles","]",")",">",">",">","final.write_videofile","(","``","final.mp4","''",",","fps=myvideo.fps",")"]
code: def __init__(self, subtitles, make_textclip=None, encoding=None):
      VideoClip.__init__(self, has_constant_size=False)
  
      if not isinstance(subtitles, list):
          # `subtitles` is a string or path-like object
          subtitles = file_to_subtitles(subtitles, encoding=encoding)
  
      # subtitles = [(map(convert_to_seconds, times), text)
      #              for times, text in subtitles]
      self.subtitles = subtitles
      self.textclips = dict()
  
      if make_textclip is None:
  
          def make_textclip(txt):
              return TextClip(
                  txt,
                  font="Georgia-Bold",
                  font_size=24,
                  color="white",
                  stroke_color="black",
                  stroke_width=0.5,
              )
  
      self.make_textclip = make_textclip
      self.start = 0
      self.duration = max([tb for ((ta, tb), txt) in self.subtitles])
      self.end = self.duration
  
      def add_textclip_if_none(t):
          """Will generate a textclip if it hasn't been generated asked
          to generate it yet. If there is no subtitle to show at t, return
          false.
          """
          sub = [
              ((text_start, text_end), text)
              for (
                  (text_start, text_end),
                  text,
              ) in self.textclips.keys()
              if (text_start <= t < text_end)
          ]
          if not sub:
              sub = [
                  ((text_start, text_end), text)
                  for ((text_start, text_end), text) in self.subtitles
                  if (text_start <= t < text_end)
              ]
              if not sub:
                  return False
          sub = sub[0]
          if sub not in self.textclips.keys():
              self.textclips[sub] = self.make_textclip(sub[1])
  
          return sub
  
      def make_frame(t):
          sub = add_textclip_if_none(t)
          return (
              self.textclips[sub].get_frame(t)
              if sub
              else np.array([[[0, 0, 0]]])
          )
  
      def make_mask_frame(t):
          sub = add_textclip_if_none(t)
          return (
              self.textclips[sub].mask.get_frame(t)
              if sub
              else np.array([[0]])
          )
  
      self.make_frame = make_frame
      hasmask = bool(self.make_textclip("T").mask)
      self.mask = (
          VideoClip(make_mask_frame, is_mask=True) if hasmask else None
      )
  
file_path: moviepy/moviepy/video/tools/subtitles.py
imports: import re
  import numpy
  from moviepy.decorators import convert_path_to_string
  from moviepy.tools import convert_to_seconds
  from moviepy.video.VideoClip import TextClip, VideoClip
label_desc: Use image node_id 1 to create a new SubtitlesClip object from inherited base classes: VideoClip with example: obj = SubtitlesClip(subtitles, make_textclip, encoding)