Align text in top middle of any photo/video frame in ffmpeg - ffmpeg

I would like to know how to align the text in any sized photo/video frame in the top middle. I understand how to center the text on the x axis in any frame, so how do I make it always be at the top as well?
Heres what I've been doing so far:
ffmpeg -i input.jpg -vf "drawtext=font='Impact': text='Test Text': fontcolor=white: borderw=3: fontsize-75: x=(w-tw)/2:y=((h-text_h)/2)" output.jpg

By top-middle, I assume you mean horizontally centered and vertically centered within the top-third of the frame. That means the text has to be vertically centered around 1/6th the height. So, x=(w-tw)/2:y=h/6-th/2

Related

FFMPEG change videos size

How do i change a video that is 1920x1080 to 1080x1920, and then "shrink" it so that it fills only half of the 1080x1920 screen size?
here is what i mean:
1920x1080 original
needs to be changed to
1080x1920 that covers 50% of screen space only
Use the scale filter, then the pad filter. The following command first scales the video from width x height to height x (width/2), then adds padding at the bottom, preserving the width and duplicating the height. The setsar filter changes the aspect ration for the output video. iw and ih stand for input width and height.
ffmpeg -i input.mp4 -vf "scale=ih:iw/2,setsar=1,pad=0:ih*2" output.mp4

Continuously rotate image centered on background

Is it possible to continuously rotate an image or series of images and output as a video with ffmpeg? Rotating image should be centered over a background.
See example video.
Use the rotate filter to rotate the image, the color filter as a background, the overlay filter to place image over background, and the format filter to ensure widely compatible chroma subsampling for H.264 video.
ffmpeg -f lavfi -i color=c=00ff00:s=1280x720:d=10 -loop 1 -i image.png -filter_complex "[1]rotate=angle=PI*t:fillcolor=none:ow='hypot(iw,ih)':oh=ow[fg];[0][fg]overlay=x=(W-w)/2:y=(H-h)/2:shortest=1:format=auto,format=yuv420p" -movflags +faststart output.mp4
The speed is controlled by the rotate filter: specifically the angle parameter. Examples:
90 degrees clockwise per second: angle=90*PI/180*t
1 degree clockwise per frame: angle=PI/180*n
1 complete rotation (2 radians) per second counter-clockwise: angle=-2*PI*t
See the rotate filter documentation for more examples.
If you want GIF instead of MP4 see How do I output GIF using ffmpeg?

Animated watermark moving on the edges of the movie - ffmpeg

I wanna overlay a picture on a video, as a watermark. How do I insert an animated watermark that randomly moves from side to side.
For example,
A watermark, placed on top/ upper-left corner, moves randomly to the top/upper-right corner and freezes there for five seconds before moving down to the lower- right corner.
I don't want the watermark to have a cross movement and move from the upper-right corner to the lower-left corner.
Here is an example of my code, using which the watermark randomly jumps to a corner each 200 frames without animate:
ffmpeg -i "source.mp4" -i "watermark.png" -filter_complex "[1:v]scale=50:-1[a]; [0:v][a]overlay=x='st(0,floor(random(n)*2)+1);if(eq(mod(n-1,200),0), if(eq(ld(0),1),0, main_w-overlay_w ) ,x)':y='st(0,floor(random(n)*2)+1);if(eq(mod(n-1,200),0),if(eq(ld(0),1),0, main_h-overlay_h ),y)'" -codec:a copy "out.mp4"

Replace transparent pixels (alpha) with black in ffmpeg?

I have an image with alpha channel indicating what the background is.
How do I de-alpha-y (remove the alpha channel) and make the background pure black with ffmpeg?
Basic syntax would be
ffmpeg -i in.png -filter_complex "color=black,format=rgb24[c];[c][0]scale2ref[c][i];[c][i]overlay=format=auto:shortest=1,setsar=1" out.png
What this does is generate a black canvas and resizes it to the image size. Then the image is overlaid on top. The opaque pixels from the image are retained but the transparent pixels reveal the canvas underneath.

Extend + scale a portrait video so the aspect ratio stays and the black borders become a blurry version of the video

I have a 720 x 1280 pixels video. How do I extend and scale it to 1920 x 1080 such that the aspect ratio is being kept and the black borders are a blurry version of the video, so you don't really notice that it's just a small vertical stripe.
Example
I started with
ffmpeg -i video720x1280.mp4 -vf "scale=608:1080,pad=width=1920:height=1080:x=656:y=0:color=black" video1920x1080.mp4
Use
ffmpeg -i video720x1280.mp4
-filter_complex
"[0]scale=hd1080,setsar=1,boxblur=20:20[b];
[0]scale=-1:1080[v];[b][v]overlay=(W-w)/2" video1920x1080.mp4
Adjust the two values in the boxblur filter for blur strength. Higher = more blurring.

Resources