April 28, 2011

Video watermarking using PHP and Mencoder

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

19 comments:

  1. Thank's for commands, bro

    Do you have commands or any perl/php script to convert videos online ?

    I want to convert videos in mobile format

    ReplyDelete
  2. Thankyou lots, I am obliged to announce that your blog is excellent!

    ReplyDelete
  3. Thanks for the post. I found a lot of people asking this question. It's not perfect but solves the problem.

    ReplyDelete
  4. Glad to see that you found it useful.

    ReplyDelete
  5. It is truly а great and helpful piece οf info.
    I'm happy that you simply shared this useful information with us. Please stay us up to date like this. Thank you for sharing.

    My homepage: www.galaxynote2.fr
    My web site ::

    ReplyDelete
  6. Highlу enеrgеtiс post, I lіked that a lоt.
    Will there be a part 2?

    My blog http://www.forexbroker.info
    Feel free to visit my web-site :: vapornine

    ReplyDelete
  7. Hoωdy! Ӏ know this is κinda off topic
    hoωeveг I'd figured I'ԁ ask. Would уou bе interеsted іn tгaԁing linkѕ or maybe
    guest writing a blog article or vice-versa?
    My websitе aԁdresѕes a lot of the ѕame tοpicѕ аѕ yours and Ӏ think ωe could
    grеatly bеnefit from each оtheг.

    If you hаρρen tο be intеreѕtеd fеel frеe to
    ѕend mе аn emаіl.

    І look forward to hearing from yоu!

    Wοndеrful blog bу the wаy!
    Also visit my webpage :: instagram-promoter.com

    ReplyDelete
  8. Ι'm truly enjoying the design and layout of your blog. It's a vеry easy on thе eyes
    ωhiсh makеs іt much mоre рleasant foг me to come heгe and visit moге oftеn.
    Did уou hіre out a dеsigner to create your themе?
    Fantaѕtіc woгk!
    Here is my web blog can you buy followers on instagram

    ReplyDelete
  9. Pleaѕе let mе know if yοu're looking for a article author for your site. You have some really great posts and I believe I would be a good asset. If you ever want to take some of the load off, I'd
    геally lіke tο wгіte sοme cοntent for уour blog іn еxсhange for a
    link back to mіne. Please senԁ
    me an е-mail if іnterested. Thank уou!


    Feel fгeе to νіsit my webѕite - twitter advertising cost
    Here is my web blog : twitter follower software

    ReplyDelete
  10. οf course like уour wеb sіte howeѵeг you need to chеck
    the spelling on severаl of your posts. Manу of them are rife wіth spеlling issues and I find
    it very troublesοme to inform the reality
    thеn again ӏ'll definitely come back again.
    Here is my web-site : how to buy more followers on twitter

    ReplyDelete
  11. Hurrah, that's what I was exploring for, what a material! present here at this website, thanks admin of this web page.

    my web blog; dnr
    Here is my website : vck

    ReplyDelete
  12. I couldn't resist commenting. Exceptionally well written!

    Also visit my website :: gain followers on twitter

    ReplyDelete
  13. when i can find a watermark php script for my website?
    http://tikfilm.ir/

    ReplyDelete