Put a text on a video (python + ffmpeg)

This script let’s you put a text on a video. It uses ffmpeg, a free tool to manipulate video. First go to ffmpeg.org and install it, then use this code. You can put the text in the middle with a black box with an opacity of 0.5, or you can put it where you want. You pass the text as argument when you call the text() function.

import os

def text(text, x="center"):
    if x == "center":
        w = "(w-text_w)/2"
        h = "(h-text_h)/2"
    elif x == "left":
        w = "10"
        h = "h - 30"
    else:
        w = "0"
        y = "0"
    os.system(f"""ffmpeg -i merola.mp4 -vf drawtext="fontfile=/path/to/font.ttf: \
    text={text}: fontcolor=white: fontsize=24: box=1: [email protected]: \
    boxborderw=5: x={w}: y={h}" -codec:a copy output.mp4""")

def text_bottom_left():
    os.system("""ffmpeg -i merola.mp4 -vf drawtext="fontfile=/path/to/font.ttf: \
    text={text}: fontcolor=white: fontsize=24: box=1: [email protected]: \
    boxborderw=5: x=0: y=h-30" -codec:a copy output.mp4""")

text("Python's great")

 


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.