Skip to content

Instantly share code, notes, and snippets.

@imjosh
Created June 10, 2024 14:48
Show Gist options
  • Save imjosh/4ecfde27b59b1f7a8966a6a996cdacf9 to your computer and use it in GitHub Desktop.
Save imjosh/4ecfde27b59b1f7a8966a6a996cdacf9 to your computer and use it in GitHub Desktop.
ffmpeg text scrolling
  • Input:

    • -f lavfi -i color=c=black:s=1280x720:d=%duration%: Generates a video of a black color with a resolution of 1280x720 and a duration specified by the duration variable (140 seconds in this case).
  • Video Filter (-vf):

    • "drawtext=fontfile=C\\:/path/to/font.ttf:: Specifies the font file to be used for the text.
    • textfile=C\\:/path/to/input.txt:: Specifies the file containing the text to be overlaid on the video.
    • fontcolor=white:: Sets the font color to white.
    • fontsize=%fontsize%:: Sets the font size using the fontsize variable (38 in this case).
    • x=(w-text_w)/2+%xoffset%:: Sets the horizontal position of the text. It centers the text horizontally by calculating (w-text_w)/2 and adjusts it with the xoffset variable. If xoffset is 0, the text will be exactly centered.
    • y=h*0.8-(t/%duration%)*(h*0.6+text_h):: Sets the vertical position of the text. Initially, the text is positioned at 80% of the frame height. Over time, it moves upwards. t is the current time, and duration is the total duration, so (t/%duration%) is the fraction of the time elapsed. The text moves upwards by (t/%duration%)*(h*0.6+text_h) over the duration of the video.
    • box=1:: Enables a box around the text.
    • boxcolor=black@0.5:: Sets the box color to black with 50% opacity.
    • boxborderw=5: Sets the width of the box border to 5.
  • Output:

    • -codec:a copy output.mp4: Specifies that the output file should be output.mp4 and the audio codec should be copied from the input (although in this case, there is no audio input, so this part is redundant).

This script is useful for creating a video with text that moves vertically over time, starting at a centered horizontal position, with customizable duration, font size, and horizontal offset.

set duration=140
set fontsize=38
set xoffset=0
ffmpeg -f lavfi -i color=c=black:s=1280x720:d=%duration% -vf ^
"drawtext=fontfile=C\\:/path/to/font.ttf: ^
textfile=C\\:/path/to/input.txt: ^
fontcolor=white: ^
fontsize=%fontsize%: ^
x=(w-text_w)/2+%xoffset%: ^
y=h*0.8-(t/%duration%)*(h*0.6+text_h): ^
box=1: ^
boxcolor=black@0.5: ^
boxborderw=5" -codec:a copy output.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment