python で動画に淵付き文字を挿入する方法

参考

Python: OpenCVで動画に字幕をつける
Pythonで縁取り/影付き/透過/ぼかし文字を入れる方法

コード

固定値で入れてしまっていますが「storoke_with」が文字の淵の大きさで、「stroke_fill」が文字の淵の色のようです。

def insert_text_on_video(input_file_path, output_file_path, text, text_rgb, font_size, x, y):
    """
    動画にテキストを挿入して保存する。
    """
    cap = cv2.VideoCapture(input_file_path)
    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
    out = cv2.VideoWriter(output_file_path, fourcc, fps, (width, height))

    while True:
        ret, frame = cap.read()
        if ret:
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            pil_image = Image.fromarray(frame_rgb)
            draw = ImageDraw.Draw(pil_image)
            font = ImageFont.truetype('/usr/share/fonts/ipa-gothic/ipag.ttf', font_size)
            draw.text((x, y), text, fill=text_rgb, font=font, stroke_width=10,
                    stroke_fill=(255, 255, 255, 255))
            rgb_image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
            out.write(rgb_image)
        else:
            break
    cap.release()
    out.release()
YouTube