Created
January 21, 2021 19:09
-
-
Save kiview/8714d31ae46b2ce63c05bbd9d3f86968 to your computer and use it in GitHub Desktop.
Add overlay text to videos and concat them to single video using ffmpeg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os, glob | |
def write_text(input, output, text): | |
cmd = f"ffmpeg -i {input} -vf drawtext=\"fontfile=/path/to/font.ttf: text='{text}': fontcolor=white: fontsize=42: box=1: [email protected]: boxborderw=5: x=(w-text_w)/2: y=100\" -codec:a copy {output}" | |
print(cmd) | |
os.system(cmd) | |
def merge_videos(folder): | |
with open('./input.txt', 'a') as input_file: | |
for f in sorted(os.listdir(folder)): | |
input_file.write(f"file '{folder}/{f}\n") | |
cmd = f"ffmpeg -f concat -i input.txt -codec:a copy concat.mp4" | |
print(cmd) | |
os.system(cmd) | |
text_dir_path = "texted" | |
try: | |
os.mkdir(text_dir_path) | |
except FileExistsError as e: | |
print("Directory already exists") | |
for file in glob.glob("*.mp4"): | |
output_file = f"{text_dir_path}/{file}" | |
overlay_text = os.path.splitext(file)[0] | |
overlay_text = overlay_text[(overlay_text.find('_') + 1):] | |
write_text(file, output_file, overlay_text) | |
merge_videos(text_dir_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment