3

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 [email protected]

Blue box with text

Blue@0.3 box with faded text

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
15
  • Do you really need to automate this or is it a one time thing?
    – timonsku
    Feb 20, 2015 at 22:47
  • yeah automate. Part of a script for making presentation videos. Thanks.
    – Jannes
    Feb 20, 2015 at 23:08
  • Please show the complete console output from your command.
    – llogan
    Feb 22, 2015 at 5:39
  • 1
    Is this a case of the XY problem? 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. Feb 23, 2015 at 5:09
  • 1
    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. Feb 23, 2015 at 13:37

1 Answer 1

3

Try this :

ffmpeg -y -i input.mp4 -vf "drawbox=enable='between(t,2,6)':y=ih-ih/4:[email protected]: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.