Trying to make a short video of a background image with text and some background audio. It’s functional, but the right side of each line of my text is always clipped a few pixels. What am I missing?
Here’s my python code (which includes the ffmpeg command):
font_size = 100
text='Example Text Example Text n Example Text'
font_path = 'Bangers-Regular.ttf'
image_path = 'background.jpg'
audio_path = 'audio.wav'
duration = 15 #or whatever the audio's length is
output_path = 'output.mp4'
font_path_escaped = font_path.replace("\", "\\\\").replace("C:", "C\:")
lines = text.split('n')
num_lines = len(lines)
line_height = font_size + 10
start_y = (1080 - line_height * num_lines) // 2
filter_complex = []
for i, line in enumerate(lines):
y_position = start_y + i * line_height
filter_complex.append(
f"drawtext=text='{line}':fontfile='{font_path_escaped}':fontsize={font_size}:"
f"x=((w-text_w)/2):y={y_position}:"
"fontcolor=white:borderw=6:bordercolor=black"
)
filter_complex_string = ','.join(filter_complex)
command = [
'ffmpeg',
'-loop', '1',
'-i', image_path,
'-i', audio_path,
'-filter_complex', filter_complex_string,
'-map', '[v]',
'-map', '1:a',
'-c:v', 'hevc_nvenc',
'-c:a', 'aac',
'-pix_fmt', 'yuv420p',
'-t', str(duration),
'-shortest',
'-loglevel', 'debug',
'-y',
output_path
]
subprocess.run(command, check=True)
print(f"Video created successfully: {output_path}")
and a frame from the outputted video: