<p>Try this :</p>
<p>ffmpeg -y -i input.mp4 -vf &quot;drawbox=enable='between(t,2,6)':y=ih-ih/4:color=black@0.5:width=iw:height=56:t=max&quot;,\ &quot;drawtext=enable='between(t,2,6)':fontfile=roboto.ttf:y=h-h/4+10:x=20:text='Test Heading':fontcolor=white:fontsize=16,drawtext=enable='between(t,2,6)':fontfile=roboto.ttf:y=h-h/4+30:x=20:text='Test sub heading':fontcolor=white:fontsize=12&quot; -acodec copy output.mp4</p>
<p><a href="https://i.sstatic.net/y8XYt.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/y8XYt.png" alt="This is how the output should look like, Try playing with black@0.5 to increase or decrease the transparency " /></a></p>

CC BY-SA 3.0
2015-11-18T18:18:21.677Z
2015-11-18T18:56:47.017Z
2020-06-17T09:53:03.607Z
LostPuppy
<p>I'm trying to create a transparent box with some text in it and then overlay that onto a (bigger) existing video.</p>

<p>I know I can do the text directly over the video, but I need the text bounded inside an invisible box.</p>

<p>My problem is that the transparent box ( color=#00000000:size=400x100 ) causes the text that I put in it to be transparent too, no matter what fontcolor or shadowcolor or boxcolor (with opaque alpha) I try to give it. Is this a bug or am I doing it wrong (TM)? Is there an alternative way?</p>

<pre><code>ffmpeg \
 -i "capture2.mkv" -t 5 \
 -filter_complex "
   color=Blue:size=400x100 [canvas];
   [canvas]drawtext='
             :fontcolor=Red
             :fontsize=30
             :fontfile=/usr/share/fonts/dejavu/DejaVuSerif.ttf
             :text=Header blabla (long text should be clipped)
             ' [text];
   [0:v][text] overlay=
  " -strict -2 -y output.mkv
</code></pre>

<p>The above works fine: blue box with red text on it. But as soon as I try to make the blue box transparent, the text starts fading too. The 2nd screenshot shows Blue@0.3</p>

<p><img src="https://i.sstatic.net/t6CIt.png" alt="Blue box with text"></p>

<p><img src="https://i.sstatic.net/vzikT.png" alt="Blue@0.3 box with faded text"></p>

<p>Complete output:</p>

<pre><code>ffmpeg version 2.4.6 Copyright (c) 2000-2015 the FFmpeg developers
  built on Feb  1 2015 02:48:47 with gcc 4.9.2 (GCC) 20141101 (Red Hat 4.9.2-1)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-frei0r --enable-gnutls --enable-libass --enable-libcdio --enable-libdc1394 --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-opencl --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
  libavutil      54.  7.100 / 54.  7.100
  libavcodec     56.  1.100 / 56.  1.100
  libavformat    56.  4.101 / 56.  4.101
  libavdevice    56.  0.100 / 56.  0.100
  libavfilter     5.  1.100 /  5.  1.100
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  0.100 /  3.  0.100
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  0.100 / 53.  0.100
Input #0, matroska,webm, from 'capture2.mkv':
  Metadata:
    ENCODER         : Lavf56.4.101
  Duration: 00:58:11.53, start: 0.000000, bitrate: 764 kb/s
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 1024x768, SAR 1:1 DAR 4:3, 30 fps, 30 tbr, 1k tbn, 60 tbc (default)
    Metadata:
      ENCODER         : Lavc56.1.100 libx264
No pixel format specified, yuv444p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0x1508840] using SAR=1/1
[libx264 @ 0x1508840] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.1 Cache64
[libx264 @ 0x1508840] profile High 4:4:4 Predictive, level 3.1, 4:4:4 8-bit
[libx264 @ 0x1508840] 264 - core 142 r2495 6a301b6 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=4 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, matroska, to 'output.mkv':
  Metadata:
    encoder         : Lavf56.4.101
    Stream #0:0: Video: h264 (libx264) (H264 / 0x34363248), yuv444p, 1024x768 [SAR 1:1 DAR 4:3], q=-1--1, 30 fps, 1k tbn, 30 tbc (default)
    Metadata:
      encoder         : Lavc56.1.100 libx264
Stream mapping:
  Stream #0:0 (h264) -&gt; drawtext
  drawtext -&gt; Stream #0:0 (libx264)
Press [q] to stop, [?] for help
frame=  150 fps= 46 q=-1.0 Lsize=      41kB time=00:00:04.93 bitrate=  67.8kbits/s    
video:39kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.940397%
[libx264 @ 0x1508840] frame I:1     Avg QP:18.30  size: 30586
[libx264 @ 0x1508840] frame P:38    Avg QP:23.35  size:    71
[libx264 @ 0x1508840] frame B:111   Avg QP:34.33  size:    56
[libx264 @ 0x1508840] consecutive B-frames:  1.3%  0.0%  0.0% 98.7%
[libx264 @ 0x1508840] mb I  I16..4: 35.1% 49.7% 15.2%
[libx264 @ 0x1508840] mb P  I16..4:  0.1%  0.1%  0.0%  P16..4:  0.1%  0.0%  0.0%  0.0%  0.0%    skip:99.6%
[libx264 @ 0x1508840] mb B  I16..4:  0.0%  0.2%  0.0%  B16..8:  0.4%  0.0%  0.0%  direct: 0.0%  skip:99.4%  L0:58.3% L1:41.7% BI: 0.0%
[libx264 @ 0x1508840] 8x8 transform intra:58.4% inter:46.2%
[libx264 @ 0x1508840] coded y,u,v intra: 7.6% 7.0% 6.9% inter: 0.0% 0.0% 0.0%
[libx264 @ 0x1508840] i16 v,h,dc,p: 76% 21%  4%  0%
[libx264 @ 0x1508840] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 25% 12% 64%  0%  0%  0%  0%  0%  0%
[libx264 @ 0x1508840] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 43% 20% 10%  4%  5%  5%  4%  4%  4%
[libx264 @ 0x1508840] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x1508840] ref P L0: 41.0%  8.3% 42.0%  8.7%
[libx264 @ 0x1508840] ref B L0: 51.9% 46.4%  1.6%
[libx264 @ 0x1508840] ref B L1: 97.0%  3.0%
[libx264 @ 0x1508840] kb/s:63.22
</code></pre>

CC BY-SA 3.0
2015-02-20T22:47:14.980Z
Do you really need to automate this or is it a one time thing?
timonsku
CC BY-SA 3.0
2015-02-20T23:08:57.257Z
yeah automate. Part of a script for making presentation videos. Thanks.
Jannes
CC BY-SA 3.0
2015-02-22T05:39:24.077Z
Please show the complete console output from your command.
llogan
CC BY-SA 3.0
2015-02-22T23:08:50.543Z
Why do you need to keep the text in an invisible box? Can you provide an image displaying what you are trying to achieve?
llogan
CC BY-SA 3.0
2015-02-23T05:07:04.873Z
Yeah, I'm not clear on exactly what the desired output is, either.  An alpha mask for something else you want to do with your video?  I don't think h.264 stores alpha.  (http://stackoverflow.com/questions/5055962/how-to-create-an-h264-video-with-an-alpha-channel-for-use-with-html5-canvas).  Or at least, x264 doesn't support it.  with ffmpeg, `-pix_fmt yuva420p` gives: `Incompatible pixel format`, and auto-converts back to `yuv420p`.
Peter Cordes
CC BY-SA 3.0
2015-02-23T05:09:50.080Z
Is this a case of the XY problem? http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem.  Tell us what you're actually trying to accomplish, along with the details of how you're trying.
Peter Cordes
CC BY-SA 3.0
2015-02-23T11:26:17.863Z
@PeterCordes pix_fmt: I had seen that, but as far as I understand actual encoding only happens at the end to create the output. XY: I'm very new at this whole video editing stuff, so that's very possible. I'll try to be more clear. At the same time I'm trying to look for the minimal most general example.
Jannes
CC BY-SA 3.0
2015-02-23T11:53:38.907Z
I simplified the example a little and included some screenshots to make it more visual. Hope this helps.
Jannes
CC BY-SA 3.0
2015-02-23T13:29:59.760Z
Ah, ok now I see where the alpha is coming into it, and why you're using a box.  The problem you're actually trying to solve is how to clip text to a bounding box with `-vf drawtext`, and you don't need an alpha channel in the output video.  google didn't find another way to do this, and this should work (after we find whatever tweak is needed).
Peter Cordes
CC BY-SA 3.0
2015-02-23T13:37:07.443Z
I think what's happening is that drawtext isn't writing the alpha channel, so overlay just sees a rectangle with uniform alpha.  The opacity controls for drawtext just affect how it blends its text into the frames it draws on.
Peter Cordes
CC BY-SA 3.0
2015-02-23T13:41:23.533Z
@PeterCordes Sorry for being confusing earlier. I had been googling and trying lots of things myself, including vaguely related settings but can't really find anything. Which is a bit surprising to me as I expect more people to want this effect. Unless there is another way to do the same thing and I'm just trying the complex way.
Jannes
CC BY-SA 3.0
2015-02-23T13:57:40.237Z
Probably you should ask how to clip drawtext to a bounding box on the ffmpeg-user mailing list.  I might get back to this tomorrow, but for now, I should get to sleep.
Peter Cordes
CC BY-SA 3.0
2015-02-23T21:13:42.603Z
What about using Imagemagick to create images with the text as bitmaps, and then overlaying it?
stib
CC BY-SA 3.0
2015-02-23T21:59:15.153Z
@stib yeah had been thinking about something like that. In this case I'm going to make it a rolling banner though, so I need some movement. Maybe a webpage that I record
Jannes
CC BY-SA 3.0
2015-02-23T22:02:41.827Z
I asked on the ffmpeg-user mailing list as well: https://ffmpeg.org/pipermail/ffmpeg-user/2015-February/025457.html
Jannes
<ffmpeg>
ffmpeg drawtext clipping to a bounding box
Jannes
# ffmpeg drawtext clipping to a bounding box

(Asked by: username_0 on 2015-02-20)

I'm trying to create a transparent box with some text in it and then overlay that onto a (bigger) existing video.
I know I can do the text directly over the video, but I need the text bounded inside an invisible box.
My problem is that the transparent box ( color=#00000000:size=400x100 ) causes the text that I put in it to be transparent too, no matter what fontcolor or shadowcolor or boxcolor (with opaque alpha) I try to give it. Is this a bug or am I doing it wrong (TM)? Is there an alternative way?
ffmpeg \
 -i "capture2.mkv" -t 5 \
 -filter_complex "
   color=Blue:size=400x100 [canvas];
   [canvas]drawtext='
             :fontcolor=Red
             :fontsize=30
             :fontfile=/usr/share/fonts/dejavu/DejaVuSerif.ttf
             :text=Header blabla (long text should be clipped)
             ' [text];
   [0:v][text] overlay=
  " -strict -2 -y output.mkv

The above works fine: blue box with red text on it. But as soon as I try to make the blue box transparent, the text starts fading too. The 2nd screenshot shows Blue@0.3


Complete output:
ffmpeg version 2.4.6 Copyright (c) 2000-2015 the FFmpeg developers
  built on Feb  1 2015 02:48:47 with gcc 4.9.2 (GCC) 20141101 (Red Hat 4.9.2-1)
  configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-frei0r --enable-gnutls --enable-libass --enable-libcdio --enable-libdc1394 --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-opencl --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
  libavutil      54.  7.100 / 54.  7.100
  libavcodec     56.  1.100 / 56.  1.100
  libavformat    56.  4.101 / 56.  4.101
  libavdevice    56.  0.100 / 56.  0.100
  libavfilter     5.  1.100 /  5.  1.100
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  0.100 /  3.  0.100
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  0.100 / 53.  0.100
Input #0, matroska,webm, from 'capture2.mkv':
  Metadata:
    ENCODER         : Lavf56.4.101
  Duration: 00:58:11.53, start: 0.000000, bitrate: 764 kb/s
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 1024x768, SAR 1:1 DAR 4:3, 30 fps, 30 tbr, 1k tbn, 60 tbc (default)
    Metadata:
      ENCODER         : Lavc56.1.100 libx264
No pixel format specified, yuv444p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0x1508840] using SAR=1/1
[libx264 @ 0x1508840] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.1 Cache64
[libx264 @ 0x1508840] profile High 4:4:4 Predictive, level 3.1, 4:4:4 8-bit
[libx264 @ 0x1508840] 264 - core 142 r2495 6a301b6 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=4 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, matroska, to 'output.mkv':
  Metadata:
    encoder         : Lavf56.4.101
    Stream #0:0: Video: h264 (libx264) (H264 / 0x34363248), yuv444p, 1024x768 [SAR 1:1 DAR 4:3], q=-1--1, 30 fps, 1k tbn, 30 tbc (default)
    Metadata:
      encoder         : Lavc56.1.100 libx264
Stream mapping:
  Stream #0:0 (h264) -> drawtext
  drawtext -> Stream #0:0 (libx264)
Press [q] to stop, [?] for help
frame=  150 fps= 46 q=-1.0 Lsize=      41kB time=00:00:04.93 bitrate=  67.8kbits/s    
video:39kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.940397%
[libx264 @ 0x1508840] frame I:1     Avg QP:18.30  size: 30586
[libx264 @ 0x1508840] frame P:38    Avg QP:23.35  size:    71
[libx264 @ 0x1508840] frame B:111   Avg QP:34.33  size:    56
[libx264 @ 0x1508840] consecutive B-frames:  1.3%  0.0%  0.0% 98.7%
[libx264 @ 0x1508840] mb I  I16..4: 35.1% 49.7% 15.2%
[libx264 @ 0x1508840] mb P  I16..4:  0.1%  0.1%  0.0%  P16..4:  0.1%  0.0%  0.0%  0.0%  0.0%    skip:99.6%
[libx264 @ 0x1508840] mb B  I16..4:  0.0%  0.2%  0.0%  B16..8:  0.4%  0.0%  0.0%  direct: 0.0%  skip:99.4%  L0:58.3% L1:41.7% BI: 0.0%
[libx264 @ 0x1508840] 8x8 transform intra:58.4% inter:46.2%
[libx264 @ 0x1508840] coded y,u,v intra: 7.6% 7.0% 6.9% inter: 0.0% 0.0% 0.0%
[libx264 @ 0x1508840] i16 v,h,dc,p: 76% 21%  4%  0%
[libx264 @ 0x1508840] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 25% 12% 64%  0%  0%  0%  0%  0%  0%
[libx264 @ 0x1508840] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 43% 20% 10%  4%  5%  5%  4%  4%  4%
[libx264 @ 0x1508840] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x1508840] ref P L0: 41.0%  8.3% 42.0%  8.7%
[libx264 @ 0x1508840] ref B L0: 51.9% 46.4%  1.6%
[libx264 @ 0x1508840] ref B L1: 97.0%  3.0%
[libx264 @ 0x1508840] kb/s:63.22

**Tags:** ffmpeg

> **Comments:**
> **username_1**: Do you really need to automate this or is it a one time thing?  
> **username_0**: yeah automate. Part of a script for making presentation videos. Thanks.  
> **username_2**: Please show the complete console output from your command.  
> **username_2**: Why do you need to keep the text in an invisible box? Can you provide an image displaying what you are trying to achieve?  
> **username_3**: Yeah, I'm not clear on exactly what the desired output is, either.  An alpha mask for something else you want to do with your video?  I don't think h.264 stores alpha.  (http://stackoverflow.com/questions/5055962/how-to-create-an-h264-video-with-an-alpha-channel-for-use-with-html5-canvas).  Or at least, x264 doesn't support it.  with ffmpeg, `-pix_fmt yuva420p` gives: `Incompatible pixel format`, and auto-converts back to `yuv420p`.  
> **username_3**: Is this a case of the XY problem? http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem.  Tell us what you're actually trying to accomplish, along with the details of how you're trying.  
> **username_0**: @PeterCordes pix_fmt: I had seen that, but as far as I understand actual encoding only happens at the end to create the output. XY: I'm very new at this whole video editing stuff, so that's very possible. I'll try to be more clear. At the same time I'm trying to look for the minimal most general example.  
> **username_0**: I simplified the example a little and included some screenshots to make it more visual. Hope this helps.  
> **username_3**: Ah, ok now I see where the alpha is coming into it, and why you're using a box.  The problem you're actually trying to solve is how to clip text to a bounding box with `-vf drawtext`, and you don't need an alpha channel in the output video.  google didn't find another way to do this, and this should work (after we find whatever tweak is needed).  
> **username_3**: I think what's happening is that drawtext isn't writing the alpha channel, so overlay just sees a rectangle with uniform alpha.  The opacity controls for drawtext just affect how it blends its text into the frames it draws on.  
> **username_0**: @PeterCordes Sorry for being confusing earlier. I had been googling and trying lots of things myself, including vaguely related settings but can't really find anything. Which is a bit surprising to me as I expect more people to want this effect. Unless there is another way to do the same thing and I'm just trying the complex way.  
> **username_3**: Probably you should ask how to clip drawtext to a bounding box on the ffmpeg-user mailing list.  I might get back to this tomorrow, but for now, I should get to sleep.  
> **username_4**: What about using Imagemagick to create images with the text as bitmaps, and then overlaying it?  
> **username_0**: @username_4 yeah had been thinking about something like that. In this case I'm going to make it a rolling banner though, so I need some movement. Maybe a webpage that I record  
> **username_0**: I asked on the ffmpeg-user mailing list as well: https://ffmpeg.org/pipermail/ffmpeg-user/2015-February/025457.html  

---

## 1 answers

### Answer by username_5 on 2015-11-18. Score: 3 | Accepted answer

Try this :

ffmpeg -y -i input.mp4 -vf "drawbox=enable='between(t,2,6)':y=ih-ih/4:color=black@0.5:width=iw:height=56:t=max",\ "drawtext=enable='between(t,2,6)':fontfile=roboto.ttf:y=h-h/4+10:x=20:text='Test Heading':fontcolor=white:fontsize=16,drawtext=enable='between(t,2,6)':fontfile=roboto.ttf:y=h-h/4+30:x=20:text='Test sub heading':fontcolor=white:fontsize=12" -acodec copy output.mp4

[![This is how the output should look like, Try playing with black@0.5 to increase or decrease the transparency ](https://i.sstatic.net/y8XYt.png)](https://i.sstatic.net/y8XYt.png)


---

