Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlaying text on video with required angle using FFMPEG

Tags:

ffmpeg

I am trying to overlay some text on video using ffmpeg. I am able to overlay text by the bellow command.

ffmpeg -i input1.mp4 -filter_complex "[0:v]transpose=2[anticlockwiserotated];[anticlockwiserotated]drawtext=fontfile=../../public/fonts/Roboto-Regular-webfont.ttf: text='Test Text':x=100: y=50: fontsize=36: fontcolor=white:[textapplied];[textapplied]transpose=1" output_video.mp4

It is allowing me to overlay horizontally or vertically only.

But I want to append it with some angle like 45 degrees.

For that if I modify the command as

ffmpeg -i input1.mp4 -filter_complex "[0:v]rotate=45*PI/180[anticlockwiserotated];[anticlockwiserotated]drawtext=fontfile=../../public/fonts/Roboto-Regular-webfont.ttf: text='Test Text':x=100: y=50: fontsize=36: fontcolor=white:[textapplied];[textapplied]rotate=315*PI/180" output_video.mp4

By this I am getting overlay video as:

enter image description here

Because in this first I am rotating video to 45 degrees, appending text and bringing it back to original position. So I am loosing borders.

Please suggest me the best way to overlay text with required angle on video.

Thanks in advance.

like image 226
Harsha Vardhan Avatar asked Jan 05 '23 17:01

Harsha Vardhan


1 Answers

Basic method is to generate text on a blank canvas, then an alpha layer for the text, rotating the result and overlaying that on the main video.

In the command below, a should be replaced by the angle. The co-ordinates for the drawtext are used in the overlay instead. Depending on the length of your text, some of it may get clipped if you've rotated it anticlockwise. So check and adjust the Y offset accordingly.

ffmpeg -i input1.mp4 -filter_complex 
        "color=black:100x100[c];
         [c][0]scale2ref[ct][mv];
         [ct]setsar=1,drawtext=fontfile=../../public/fonts/Roboto-Regular-webfont.ttf:
             text='Test Text':fontsize=36:fontcolor=white,split[text][alpha];
         [text][alpha]alphamerge,rotate=a:ow=rotw(a):oh=roth(a):c=black@0[txta];
         [mv][txta]overlay=x='min(0,-H*sin(a))+100':y='min(0,W*sin(a))+50':shortest=1"
      output_video.mp4
like image 109
Gyan Avatar answered Feb 23 '23 11:02

Gyan