Add Overlay Text to Video using PHP-FFMpeg

Updated on December 5, 2019

I recently learned how to add an audio track to the video using PHP-FFMpeg. Now I wanted to add overlay text to video. The FFmpeg command-line tool accepts drawtext filter using the libfreetype library.  The catch is, everything that’s possible in the FFmpeg command-line is not possible in PHP-FFMpeg, but this one is definitely possible. Let’s see how to do that now.

Before that, have a look at the image below.

Add Overlay Text to Video

Note

You must have FFMpeg installed on your system and should have been configured with –enable-libfreetype

.

Step 1: How to verify FFMpeg configured with libfreetype library

#ffmpeg  -version | grep libfreetype
configuration: --prefix=/usr --extra-version='0york0~18.04.1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-nonfree --enable-libfdk-aac --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared

Step 2: Create a custom filter in PHP-FFMpeg

I couldn’t find any direct implementation for adding an overlay text to the video in PHP-FFMpeg or any relevant documentation. So I’ll create a custom filter to use drawtext filter in PHP-FFMpeg as shown below:

$command = "text='$text': fontfile=OpenSans-Regular.ttf: fontcolor=red: fontsize=80: box=1: boxcolor=black@0.5: boxborderw=5: x=(w-text_w)/2: y=(h-text_h)/2:";
$video->filters()->custom("drawtext=$command");

Step 3: Add overlay text to video using PHP-FFMpeg

Below is the complete code of adding an overlay text to the video. Make sure the movie.mp4 and php-ffmpeg are available in the path where this file is located.

<?php

require_once 'vendor/autoload.php';
$ffmpeg = FFMpeg\FFMpeg::create();
$format = new FFMpeg\Format\Video\X264();
$format->setAudioCodec("aac");
$videoFile='movie.mp4';
$captionStaticFilePath=$_SERVER['DOCUMENT_ROOT'].'/';
$outputFile='movie_output.mp4';
$text="Techglimpse.com";
$command = "text='$text': fontfile=OpenSans-Regular.ttf: fontcolor=red: fontsize=80: box=1: boxcolor=black@0.5: boxborderw=5: x=(w-text_w)/2: y=(h-text_h)/2:";

try{
        $video = $ffmpeg->open($captionStaticFilePath.$videoFile);
        $video->filters()->custom("drawtext=$command");
        $video->save($format, $captionStaticFilePath.$outputFile);
        die('done');
}catch(Exception $e){
        echo $e->getMessage();die;
}

?>

Love it? Let me know your comments.

Was this article helpful?

Related Articles

Comments Leave a Comment

  1. Really appreciable.Thank you so much. Have any other FFmpeg tutorial?

Leave a Comment