Overlaying text on video with required angle using

2019-05-30 05:01发布

问题:

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:

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.

回答1:

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


回答2:

One method is by using ASS or SRT subtitles with the FFmpeg subtitles or ass filters.

ffmpeg -i input -filter_complex "subtitles=diagonal.ass" output

SRT subtitles are much simpler than ASS and don't support rotation, but you can manually add it with the filter:

ffmpeg -i input -filter_complex "subtitles=diagonal.srt:force_style='Angle=45'" output

You can create and style the subtitles with Aegisub or manually.



标签: ffmpeg