Skip to content

Instantly share code, notes, and snippets.

@coding-youtuber
Created February 20, 2021 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coding-youtuber/104896b498a7350ee67d138ac1ff2cc5 to your computer and use it in GitHub Desktop.
Save coding-youtuber/104896b498a7350ee67d138ac1ff2cc5 to your computer and use it in GitHub Desktop.
FFmpegを用いて動画にテロップをつける
import os
import subprocess
# Pythonからコマンド実行する例
def add_telop(self, text, index, input_file_path):
file_name = os.path.basename(input_file_path)
output_video_path = os.path.join(self.telopped_working_dir, "{}.mp4".format(index))
cmd = """
ffmpeg -y -i {} \
-filter_complex "drawtext=fontfile=/usr/share/fonts/truetype/freefont/FreeSerif.ttf: text='{}':\
x=100: y=50: fontsize=35: fontcolor=yellow@0.9: box=1: boxcolor=red@0.9" {}
""".format(input_file_path, text, output_video_path)
# print(cmd)
subprocess.check_output(cmd, shell=True)
# -i inputファイルパスを指定
# -y 上書き保存
# -filter_complex フィルターを指定
# ffmpeg -y -i 元の動画ファイルパス \
# -filter_complex "フィルターの内容を文字列で定義" \
# 出力先の動画ファイルパス
# inputのファイルパス、出力先、フォントのファイルパスは環境に応じて書き換えてください
ffmpeg -y -i /Users/naoyashiga/youtube/hiroyuki/videos/source.mp4 \
-filter_complex "drawtext=fontfile=/Users/naoyashiga/Library/Fonts/ラノベPOP.otf: text='こんにちは':\
x=300: y=200: fontsize=100: fontcolor=blue@0.9: box=1: boxcolor=white@0.9" \
/Users/naoyashiga/youtube/hiroyuki/videos/output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment