Video watermarking with FFMpeg and PHP

January 15th, 2007 by sigurd · Programming

Nowadays internet video is spread all over the web. But very often people ask how to watermark their video and get thumbnails of video files.
When I've got such task I decided make it with PHP and FFMpeg.

There are two general cases of watermarks. First is just text watermark. The second is various pictures, video files and other resources. FFMpeg supports both :) But it does not support watermarking by itself. Video which is processed can be hooked up with external libraries.

For text watermarking FFMpeg has hook called drawtext. It's included in FFMpeg distribution so there is no need to download it. I will not describe how to work with FFMpeg and choose different encoding options. All this you can read in documentation.

For using hook you must know full path to it. In my case it was /usr/local/lib/vhook/drawtext.so. To enable hook in FFMpeg it needs to be loaded with –vhook key.

ffmpeg -i /tmp/test.avi –vhook "/usr/local/lib/vhook/drawtext.so –f /usr/share/fonts/default/TrueType/verdana.ttf –t watermarktext" /tmp/out.mpg

Note that all hook params is included in "". Also drawtext requires two additional parameters – full path to TrueType font file(-f key) and watermark text(-t key). You can also set the position of the text in pixels using –x and –y keys.

ffmpeg -i /tmp/test.avi –vhook "/usr/local/lib/vhook/drawtext.so –f /usr/share/fonts/default/TrueType/verdana.ttf –x 10 –y 10 –t watermarktext" /tmp/out.mpg

This command will draw watermark on video file test.avi, with text "watermarktext", start drawing in 10x10 position and output it to out.mpg. This hook is easy to use, it's quite fast, but it can only draw text on video and that's all.

And what about if you want to add some animated gif or another video file to existing?
For this I chose PiP(Picture in Picture) hook. It's modification of watermark hook made by Mihail Stoyanov. Of course you can use standard hook, but it has much less functionality than new one. I use it in spite of it doesn't included yet in the FFMpeg source tree and you need to compile it by yourself. You can download PiP source here.

ffmpeg -i /tmp/test.avi –vhook "/usr/local/lib/vhook/pip.so –f pip.avi" /tmp/out.mpg

As you can see it's pretty easy to use. There is only one required parameter – file which will be used as watermark(-f key). But it has many useful options such as start position of PiP(-x,-y keys), PiP's height and width(-h,-w keys), and of course modes of watermarking(-m key). There are three modes and two of them use threshold option(-t key). You can read more about options and how them work in authors notes. Also there are a lot of PiP usage examples.

For now I use some hooks to watermark videos, but to create thumbnail you do not need any.

ffmpeg -i /tmp/test.avi –f mjpeg –ss 0.001 –t 0.001 –s qcif /tmp/out.jpeg

-f key is used for setting jpeg format, -ss is the time shift, in this case start position of movie, -t is duration of capturing. Note that if duration is greater than 0.001 only the last frame will be captured and saved.

It often happens that you need get video file duration. It easily can be done via command line, but you can get some problems in PHP.

ffmpeg -i /tmp/test.avi -vstats

Above is the command for getting video file stats. The problem is that FFMpeg requires an output file so this command always produce error. So the PHP exec function will not return FFMpeg output because it goes into STDERR. This can be fixed by command line "hack".

exec('(ffmpeg -i /tmp/test.avi -vstats>> /dev/null) 3>&1 1>&2 2>&3', $results, $status); foreach ($results as $resultLine) { if (preg_match("/Duration: (\d+):(\d+):(\d+\.\d+)/", $resultLine, $regs)) { $duration = 3600*$regs[1] + 60*$regs[2] + $regs[3]; } }

PS: Note that I use idea and implementation of Jack Slocum's blog comment system. In next posts I'll release it :)

Comments

sigurd January 16th, 2007 at 9:44 am

Welcome to all to my blog :)