PHPVOID Just another WordPress weblog Thu, 18 Jan 2007 16:48:24 +0000 en WordPress Comments System YUI + YUI-EXT (tribute to Jack Slocum) Thu, 18 Jan 2007 16:22:22 +0000 sigurd PHP AJAX First of all I’d like to say that all you see here is idea and implementation of Jack Slocum. I just make some fixes into design and add some hacks into WP. Thank you Jack for developing YUIEXT and for your contribution to open source.
The idea of comment system is quite easy. Every wordpress post consists of blocks. Why people should leave comments below post, when each block can be comment separately. In addition simple dialogs can be used for leaving comments whis is more users friendly. After publishing his work Jack got over 2000 comments to his post. People just want to see – how it works :) But it was not available for community so I decided to change this :)
  1. /**
  2. * Copyright 2006 Jack Slocum
  3. * Want to use this code on your site? Of course you can!
  4. * This code, like all code on is
  5. * free to modify and distribute as you wish.
  6. *
  7. */
The implementation of this comment system is pretty easy. It consists of two main objects – Cblock and Commentable. Cblock cares about storing info and comments for given block, Commentable handles all other routines such as creating list of blocks.
For using this comment system WordPress should be hacked a bit.
First of all database need to be tuned so it can store comment block id in addition to post id. For this we need to midify comments table.
  1. ALTER TABLE ` wp_comments` ADD `comment_content_index` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0';
  2. ALTER TABLE ` wp_comments` ADD INDEX ( `comment_content_index` ) ;
Note that in your WordPress table can have different name depending on prefix. Now we can store block information but wordpress don’t know how to do it. So wp-includes/ comment-functions.php need to be modified.
In line 81
  1. $result = $wpdb->query("INSERT INTO $wpdb->comments
  2. (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id)
  4. ('$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id')");
Should be replaced by
  1. $result = $wpdb->query("INSERT INTO $wpdb->comments
  2. (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id,comment_content_index)
  4. ('$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id','$comment_content_index')    ");
The next changed file will be wp-comments-post.php for sending proper value for saving and return comment instead redirect, so comment you post will appear on page without refreshing it.
Below line 24 add
  1. $comment_content_index = (int)$_POST['contentIndex'];
Line 50 form
  1. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID');
Change to
  1. $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID','comment_content_index');
And finally replace all below line 61 by
  1. $comment = get_comment($comment_id);?> <li id="comment-<?php comment_ID() ?>"> <div class="cheader"> <div class="cuser" /> <div class="commentmetadata">at <span title="Select Block" class="csel">&nbsp;</span></div> </div> </li>
Omit any line brakes in HTML because your comment will not display properly after adding. The next and the final change applies to output. In original WordPress blocks are identified bytag. In Jack’s blog blocks are identified with
tag, so after some experiments I decided do not change it.
In file wc-includes/template-functions-post.php in function the_content under
  1. $content = str_replace(']]>', ']]&gt;', $content);
  1. content = str_replace('<blockquote>', '<div class="content"><blockquote>', $content);
  2. $content = str_replace('</blockquote>', '</blockquote></div>', $content);
  3. $content = str_replace('<p>', '<div class="content">', $content);
  4. $content = str_replace('</p>', '</div>', $content);
This replacement also can be done via plugin using the_content filter.That’s all WordPrees code modifications. All others regards to theme. Main changes should be made in single.php(or page.php depending on theme). General HTML structure to made comment system work should be like below so it can be applied to any theme.
  1. ......
  2. <div class="comments-enabled" id="content">
  3. <div class="content">Heading …</div>
  4. <div class="content">Block 1</div>
  5. <div class="content">Block 2</div>
  6. …</div>
  7. ...
The changes to comments.php are too big for providing them here. There are another files which should be added to wordpress such as styles, images, javascript, YUI and YUI-EXT. Anyway I pack it together with YUI and YUI-EXT using default wordpress theme. Just install and it’s ready to use. Of course it need some customization to make theme look as you wish. If you have some questions just ask them in comments or send me to email: sigurd at
PS: I used yui-ext.0.40-alpha because Jack add some nice themes there such as Vista and WordPress 2.0.6
PSS: Thanks to Jack Slocum again :)
New design Thu, 18 Jan 2007 11:19:11 +0000 sigurd Different I make a redesign of my blog. Previous design was pretty ugly so i use "Deep Red" theme by aNieto2k.
Comment system works normaly as im previous design, so it'll released shortly.
Video watermarking with FFMpeg and PHP Mon, 15 Jan 2007 14:01:47 +0000 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, vide files and other resources. FFMpeg support 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 chouse 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/ To enable hook in FFMpeg in needs to be loaded with –vhook key.
  1. ffmpeg -i /tmp/test.avi –vhook "/usr/local/lib/vhook/ –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.
  1. ffmpeg -i /tmp/test.avi –vhook "/usr/local/lib/vhook/ –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 u want to add some animated gif or another video file to existing?
For this I chouse 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.
  1. ffmpeg -i /tmp/test.avi –vhook "/usr/local/lib/vhook/ –f pip.avi" /tmp/out.mpg
As u 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.
  1. 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 grater 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.
  1. 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”.
  1. exec('(ffmpeg -i /tmp/test.avi  -vstats>> /dev/null) 3>&1 1>&2 2>&3'$results, $status);
  2. foreach ($results as $resultLine) {
  3. if (preg_match("/Duration: (\d+):(\d+):(\d+\.\d+)/", $resultLine, $regs))
  4. {
  5. $duration = 3600*$regs[1] + 60*$regs[2] + $regs[3];
  6. }
  7. }
PS: Note that I use idea and implementation of Jack Slocum’s blog comment system. In next posts I’ll release it :)