Showing posts with label php mencoder video watermarking. Show all posts
Showing posts with label php mencoder video watermarking. Show all posts

April 28, 2011

Video watermarking using PHP and Mencoder

19 comments:
This took me sometime to figure out, mostly because I'm a non linux based user. I had been working on a site that heavily interacted with user uploaded videos and this called for a need to watermark videos in order to prevent them from getting leeched.

Like any other guy, I firstly thought about using ffmpeg. My search for video watermarking using ffmpeg landed me to pages that talked about vhook module but unfortunately, newer versions of ffmpeg didn't had support for vhook and the video filters that had to replace vhook were/are still under development. ( see here )

My next move was to search for an alternative. I looked for CLI based tools like mencoder and even considered using wine ( bad idea though. ). After lots of research and googling, I finally figured out that mencoder could be used to add subtitles to vidoes and that was it.

I made myself a quickly subtitles file and named it watermark.srt, grabbed a font from my fonts dir and a video file.

I used following command to watermark the video

CLI code :

mencoder a_samp.avi -o b_samp.avi -ovc xvid -xvidencopts fixed_quant=3 -oac copy -sub "watermark.srt" -font "verdana.ttf" -subpos 98

Watch closely. -sub option defines the subtitile file to be used, -font option defines the font file to be used and -subpos defines the position of subtitles file. Rest of the options are for video format and output file name. In this code, we are converting a_samp.avi to b_samp.avi and while doing so, we are copying audio stream as it is but altering video stream using -ovc option. We're also applying subtitles file on the video using verdana font. Note that watermark.srt and verdana.ttf files must be present in current directory. ( Or specify their absolute path  )
( Read more on mencoder options. ) You can get more info about srt files here.

Sample srt file :

1
00:00:1,000 --> 00:00:20,000
Text1

2
00:00:21,000 --> 00:20:30,000
Text2

Explanation :

The srt file simply tells the player to display Text1 between 1 to 20 seconds and Text2 between 21 to 30 seconds and you can easily get a font. Use clear and simple fonts like verdana,arial etc


How to do this in PHP ?


Call a function to execute the command. Eg.shell_exec();exec();system();passthru();



Output :

Fig: Video after watermarking.

Links :

[1]. Download Mplayer / Mencoder
[2]. Mencoder documentation
[3]. Mencoder on wikipedia
Read More