AA,
HI,
Dear how r u?

I want to scroll / move an image horizontally as watermark over the video with smooth speed as long as the movie is running.

I am using wamp , php 5 , ffmpeg , imlib2.dll , windows xp ,
I am using code as under but image is scrolled with un smooth speed.I want to scroll the image smoothly from right to left:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Vid Encoding</title>
</head>

<body>
<?php
$water_mark='Muhammad Asim Dewan Relax Solutions';
$water_mark_width=strlen($water_mark);
$font_file="Fonts\\arial.ttf";
$font_size="15.0";
///  MAKE POINTS EQUAL TO THE PIXELS.   WE ARE USING GD2 SO WE ARE USING POINTS. BUT OUTPUT WE NEED IN PIXELS AS IMAGREATE FUNCTION TAKES.
$font_size_in_pixels=($font_size/1.33);
//  HANDLING IMAGE SIZE ACCORDING TO THE STRING OF CHARACTERS.
if($water_mark_width<30)
{
$image_width=($water_mark_width*$font_size_in_pixels)+22;
}
elseif($water_mark_width>=30 && $water_mark_width<50)
{
$image_width=($water_mark_width*$font_size_in_pixels)+27;
}
else
{
$image_width=($water_mark_width*$font_size_in_pixels)+30;
}
////////////////////////////
$image_height=50;
$image1=imagecreate($image_width,$image_height);
$image_background_color=imagecolorallocate($image1, 0, 0, 0);

$colorTransparent = imagecolortransparent($image1,$image_background_color);  // transparent the given image for the specified color from background


$image_text_color=imagecolorallocate($image1,253,198,45);//text color which will be putting on image
imagettftext($image1,$font_size,0,0,30,$image_text_color,$font_file,$water_mark);   /// used for putting text on image ie making image form the given text
//save image png
//$watermark_image=imagecreatefrompng($image1);
// imagestring()      does not take font family  or font file as parameter
$text_to_show="water_mark.png";
imagepng($image1,$text_to_show);  // saving png image on the disk
//header ("Content-type: image/png");   // do not output an image over the browser while you  are going to save that image.
?>
<?php
//phpinfo();

//include('ffmpeg_movie.php');
$ffmpegpath="ffmpeg\\ffmpeg";
$input_file="asimf.flv";
$output_file="water_c.flv";
$font_path="Fonts\\arial.ttf/20";
//$x_axis="W*(0.8-0.99*tan(N/50/15*PI))";
//$x_axis="W*(0.8-0.99*tan(N/50/25-PI))";
 $x_axis="W*(0.8-0.80*tan(N/150*PI/4))";
//;$x_axis="W*(0.8-0.70*tan(N/150*PI/3.50))";
$y_axis="H*(0.75+0*cos(N/1*PI))";

//practice
//$x_axis="w+(0.8-1.0*tan(N))";
//$y_axis="H*(0.8+0*cos(N/1*PI))";
//
$output_width = "320";
$output_height = "240";
$ab = "128";
$r = "23";
$b="259000";
$ar="44100";
$color_r="210";
$color_g="155";
$color_b="200";
$space=" asim";
$font_family="Fonts\\arial.ttf/20";
//$texts_to_show="DEWANGUSENET"; 		FOR TEXT WATERMARK UNCOMMENT IT

//$text_to_show="dollarlarge.png";

/*
echo W*(0.5+0.25*sin(N/100*PI))-w/2;
echo $y_axis;
*/

//vhook for image water mark
$vhook_path='"'."vhook\\imlib2.dll"." -x ".$x_axis." -y ".$y_axis." -i ".$text_to_show.'"';
//vhook for text water mark
//$vhook_path='"'."/root/ffmpeg/ffmpeg/vhook/imlib2.so -R 210 -G 155 -B 200 "." -x ".$x_axis." -y ".$y_axis." -F ".$font_path." -t ".$texts_to_show.'"';

//echo $vhook_path."<br/>";
///    COMMAND EXECUTED IN DOS
/*C:\ffmpeg>ffmpeg -i Blaaskarwei.mpeg -ar 22050 -ab 32 -f flv -s 320x240 video.flv*/

/*				FUNCTION PARAMETER DETAILS
			-i 		input file name
			-f 		output format

		-ab 	audio bit rate in bit/s
		-r		frame rate in Hz for video
		-s 		video output dimension / frame size / height and width / video resolution
		-b  	video bit rate in kbit/s
		-ar		audio sampling rate (in Hz) 

*/
//	WORKING ENCODING COMMAND FOR ANY FORMATE FORMATE TO FLV (from AVI   ,  from MPEG  ) encoding:
//echo $ffmpegpath." -i ".$input_file." -r ".$r." -ab ".$ab." -ar ".$ar." -b ".$b." -f flv -s ".$output_width."x".$output_height." ".$output_file;
//exec($ffmpegpath." -i ".$input_file." -r ".$r." -ab ".$ab." -ar ".$ar." -b ".$b." -f flv -s ".$output_width."x".$output_height." ".$output_file);

//	DOS WORKING watermark COMMAND FOR ANY FORMATE FORMATE TO FLV ( AVI   ,  MPEG  ) watermark image:

//C:\ffmpeg\bin>ffmpeg -i a.avi -vhook "c:/wamp/www/videos_proj/vid_encoding/ffmpeg/vhook/watermark.dll -m 1 -f logo.jpg" -ar 44100  testsss.flv

//	DOS WO0RKING WATERMARK COMMAND FOR FLV TO FLV AND MPEG TO FLV watermark text:

//C:\ffmpeg\bin>ffmpeg -i asimkkk.flv -vhook "c:\wamp\www\videos_proj\vid_encoding\ffmpeg\vhook\imlib2.dll  -R 210 -G 155 -B 200 -F arial.ttf/20 -x 10+0.5*N -y 10+0.25*N -t Hello" dewankkk.flv

//	WORKING WATERMARK COMMAND FOR FLV TO FLV AND MPEG TO FLV watermark text:
//echo $ffmpegpath." -i ".$input_file." -vhook ".$vhook_path." ".$output_file;

exec($ffmpegpath." -i ".$input_file." -vhook ".$vhook_path." ".$output_file);


?>


</body>
</html>
    3 months later

    If you can display the video in a Flash player, then it would be a lot easier to just do it in Flash. In general, I think PHP is not the right solution for what you want to do.

    Check out a video authoring forum and ask them how to best accomplish what you want.

      The problem with the Flash solution is that it doesn't truly place the watermark ON the video. That is, someone could intercept the video before Flash puts the watermark on and you've accomplished nothing.

      The correct way to do this is with a little bit of PHP and a whole lot of ffmpeg. There is a function in ffmpeg that will let you place a graphic at a specific X/Y coordinate on a specific frame in the video... so you can use PHP to keep calling ffmpeg and position the watermark wherever you want it to appear in each frame. Unfortunately, this will require a good understanding of ffmpeg - your best bet is to hire a consultant who knows ffmpeg well.

        etully wrote:

        Unfortunately, this will require a good understanding of ffmpeg - your best bet is to hire a consultant who knows ffmpeg well.

        I would have to disagree with that comment, but only because PHP is wonderful and allows for reusable code. If you can't work with this class, then I would hire a developer.

        The following code is not mine, is provided as is and without warranty.

          <?php
          
          /** FFMPEG CLASS
            *
            * This code is free software; you can redistribute it and/or
            * modify it under the terms of the GNU General Public License as
            * published by the Free Software Foundation; either version 2 of the
            * License, or (at your option) any later version.
            *
            * This code is distributed in the hope that it will be
            * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
            * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
            * GNU General Public License for more details.
            *
            * You should have received a copy of the GNU General Public License
            * along with this code; if not, write to the Free Software Foundation,
            * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
            *
            *   @package shr
            *   @version 0.1
            *    @authorxxx <xxx@xxx.net>
            * @copyright 2007 xxx
            *   @license http://www.opensource.org/licenses/gpl-license.php GPL
            */
          
          
          /** FFMpeg class */
          
          class ap_ffmpeg {
          
            /** Path to ffmpeg binary
              * @var string */
              protected $bin;
          
            /** Path to watermark.so vhook file
              * @var string */
              protected $watermark_so;
          
            /** File formats available for encoding
              * @var array */
              protected $encode_file_formats;
          
            /** File formats available for decoding
              * @var array */
              protected $decode_file_formats;
          
            /** Video codecs available for encoding
              * @var array */
              protected $encode_video_codecs;
          
            /** Video codecs available for decoding
              * @var array */
              protected $decode_video_codecs;
          
            /** Audio codecs available for encoding
              * @var array */
              protected $encode_audio_codecs;
          
            /** Audio codecs available for decoding
              * @var array */
              protected $decode_audio_codecs;
          
          
            /** Class constructor.
              * @param string $bin           FFMpeg binary location (e.g. /usr/bin/ffmpeg). If it's not given, the constructor will try to detect binary location automaticaly
              * @param string $watermark_so  Location of watermark.so file (e.g. /usr/lib/vhook/watermark.so). Skip this if you are not planinig to place watermark on the converted video
              * @return ap_ffmpeg */
          
          function __construct($bin="", $watermark_so="") {
          
              $bin = trim($bin);
              $watermark_so = trim($watermark_so);
          
              if (!$bin) {
                  $bin = trim(shell_exec("which ffmpeg 2>&1"));
          
                  if (substr($bin, 0, 1) != "/")
                      die("Cannot find FFMpeg executible");
          
              } else {
          
                  if (!preg_match('/\/ffmpeg$/', $bin)) {
                      if (substr($bin, -1) != "/")
                          $bin .= "/";
                      $bin .= "ffmpeg";
                  }
          
          //            if (!file_exists($bin))
          //                die("$bin not exists");
                  }
          
          //        if (!is_readable($bin))
          //            die("Cannot read $bin. Please check the permissions");
          
          //        if (!is_executable($bin))
          //            die("File $bin is not executible");
          
              if ($watermark_so) {
          
                  if (!preg_match('/\/watermark\.so$/', $watermark_so)) {
                      if (substr($watermark_so, -1) != "/")
                          $watermark_so .= "/";
                      $watermark_so .= "watermark.so";
                  }
          
          //            if (!file_exists($watermark_so))
          //                die("Watermark vhook $watermark_so not exists");
          
          //            if (!is_readable($watermark_so))
          //                die("Cannot read $watermark_so. Please check the permissions");
          
          //            if (!is_executable($watermark_so))
          //                die("File $watermark_so is not executible");
                  }
          
              $this->bin = $bin;
              $this->watermark_so = $watermark_so;
              $this->encode_file_formats = $this->get_avail_formats('^ .E ');
              $this->decode_file_formats = $this->get_avail_formats('^ D. ');
              $this->encode_video_codecs = $this->get_avail_codecs('^ .EV... ');
              $this->decode_video_codecs = $this->get_avail_codecs('^ D.V... ');
              $this->encode_audio_codecs = $this->get_avail_codecs('^ .EA... ');
              $this->decode_audio_codecs = $this->get_avail_codecs('^ D.A... ');
          }
          
          
            /** Fetch available file formats, matching with given regular expression
              * @param string $rexpr
              * @return array */
          
          protected function get_avail_formats($rexpr) {
              $cmd = "{$this->bin} -formats 2>&1 | grep -e '$rexpr' | cut -c 5-20";
              return explode("\n", trim(preg_replace('/ +\n/', "\n", shell_exec($cmd))));
          }
          
          
            /** Fetch available codecs, matching with given regular expression
              * @param string $rexpr
              * @return array */
          
          protected function get_avail_codecs($rexpr) {
              $cmd = "{$this->bin} -formats 2>&1 | grep -e '$rexpr' | cut -c 9-100";
              return explode("\n", trim(shell_exec($cmd)));
          }
          
          
            /** Returns file formats available for encoding
              * @return array */
          
          public function get_avail_encode_file_formats() {
              return $this->encode_file_formats;
          }
          
          
            /** Returns file formats available for decoding
              * @return array */
          
          public function get_avail_decode_file_formats() {
              return $this->decode_file_formats;
          }
          
          
            /** Returns video codecs available for encoding
              * @return array */
          
          public function get_avail_encode_video_codecs() {
              return $this->encode_video_codecs;
          }
          
          
            /** Returns video codecs available for decoding
              * @return array */
          
          public function get_avail_decode_video_codecs() {
              return $this->decode_video_codecs;
          }
          
          
            /** Returns audio codecs available for encoding
              * @return array */
          
          public function get_avail_encode_audio_codecs() {
              return $this->encode_audio_codecs;
          }
          
          
            /** Returns audio codecs available for decoding
              * @return array */
          
          public function get_avail_decode_audio_codecs() {
              return $this->decode_audio_codecs;
          }
          
          
            /** Grab video codec from the file
              * @param string $file_path
              * @return string */
          
          public function get_video_codec($file_path) {
              $file_path = str_replace('"', "\\\"", $file_path);
              $cmd = "{$this->bin} -i \"$file_path\" 2>&1 | grep \"  Stream #\" | grep \"Video: \" | awk '{print \$4}'";
              $codec = preg_replace('/^([^,]*),?$/', "$1", trim(shell_exec($cmd)));
              return $codec;
          }
          
          
            /** Grab audio codec from the file
              * @param string $file_path
              * @return string */
          
          public function get_audio_codec($file_path) {
              $file_path = str_replace('"', "\\\"", $file_path);
              $cmd = "{$this->bin} -i \"$file_path\" 2>&1 | grep \"  Stream #\" | grep \"Audio: \" | awk '{print \$4}'";
              $codec = preg_replace('/^([^,]*),?$/', "$1", trim(shell_exec($cmd)));
              return $codec;
          }
          
          
            /** Returns array($width, $height) with horizontal and vertical sizes
              * @param string $file_path
              * @return array */
          
          public function get_video_size($file_path) {
              $file_path = str_replace('"', "\\\"", $file_path);
              $cmd = "{$this->bin} -i \"$file_path\" 2>&1 | grep \"  Stream #\" | grep \"Video: \" | awk '{print \$6}'";
              $size = preg_replace('/^(\d+x\d+),?$/', "$1", trim(shell_exec($cmd)));
              $width = preg_replace('/^(\d+)x\d+$/', "$1", $size);
              $height = preg_replace('/^\d+x(\d+)$/', "$1", $size);
              return array($width, $height);
          }
          
          
            /** Returns video duration time (HH:MM:SS)
              * @param string $file_path
              * @return string */
          
          public function get_duration($file_path) {
              $file_path = str_replace('"', "\\\"", $file_path);
              $cmd = "{$this->bin} -i \"$file_path\" 2>&1 | grep \"Duration: \" | awk '{print \$2}' | cut -c 1-8";
              return shell_exec($cmd);
          }
          
          
            /** Check if the video in the file can be decoded
              * @param string $file_path
              * @return bool */
          
          public function is_video_decodable($file_path) {
              return in_array($this->get_video_codec($file_path), $this->decode_video_codecs);
          }
          
          
            /** Check if the audio in the file can be decoded
              * @param string $file_path
              * @return bool */
          
          public function is_audio_decodable($file_path) {
              return in_array($this->get_audio_codec($file_path), $this->decode_audio_codecs);
          }
          
          
            /** Capture a frame from a video. Returns TRUE on success or errors string on failure
                * @param string  $in_file    Input Video File
                * @param string  $out_file   Output Image File. Only jpeg output format supported
                * @param integer $width      Output Image Width
                * @param integer $height     Output Image Height
                * @param string  $time       Frame time. Example: 00:00:05
                * @param string  $add        Additional ffmpeg command parameters. Not required
                * @param string  $pipe       Additional pipe after ffmpeg command. Not required (should not begins with '|')
                * @param bool    $overwrite  Overwrite Output File Flag. Not required. (default = false)
                * @return mixed */
                  public function grab_frame($in_file, $out_file, $width, $height, $time, $add="", $pipe="", $overwrite=false) {
            
                if (!file_exists($in_file))
                    return "Grab Frame error: Input file $in_file not exists";
            
                if (!is_readable($in_file))
                    return "Grab Frame error: Cannot read from input file $in_file";
            
                if (!in_array($this->get_video_codec($in_file), $this->decode_video_codecs))
                    return "Grab Frame error: Cannot grab frame from $in_file. Incompatible video codec";
            
                if (file_exists($out_file)) {
            
                    if (!$overwrite)
                        return "Grab Frame error: File $out_file already exists";
            
                    elseif (!is_writable($out_file))
                        return "Grab Frame error: Cannot write to $out_file. Please check the permissions";
            
                } elseif (!touch($out_file))
                    return "Grab Frame error: Cannot write to $out_file. Please check the permissions";
            
                else
                    unlink($out_file);
            
                if (!is_int($width) || !is_int($height))
                    return "Grab Frame error: Invalid width or height. Integer values expected.";
            
                if (!preg_match('/^\d{2}\:\d{2}\:\d{2}$/', $time))
                    return "Grab Frame error: Invalid time - $time (correct example: \"00:00:05\")";
            
                $cmd = "{$this->bin} -i \"" . str_replace('"', "\\\"", $in_file) . "\" -f mjpeg -an -ss $time -r 1 -vframes 1 -s {$width}x$height";
            
                if ($add) $cmd .= " $add";
                $cmd .= " -y $out_file";
                if ($pipe) $cmd .= " - | $pipe";
                $cmd .= " 2>&1";
            
                $output = shell_exec($cmd);
            
                preg_match('/([^\n]+)\n?$/s', $output, $last);
                $last = $last[1];
            
                return preg_match('/^video:\d*\w{0,2}\s+audio:\d*\w{0,2}/', $last)
                  ? true
                  : trim($output);
            }
            
            
              /** Convert a file. Returns TRUE on success or errors string on failure.
                * @param string  $in_file          Input File
                * @param string  $out_file         Output File
                * @param string  $file_format      Output File Format
                * @param integer $width            Output Video Width
                * @param integer $height           Output Video Height
                * @param string  $watermark_img    Location of watermark image. Not required. Should not include spaces and quotes
                * @param string  $video_codec      Output Video Codec. Not required if it's covered by $file_format
                * @param integer $video_bitrate    Output Video Bitrate (bit/s). Not required. (default = 200 kb/s)
                * @param integer $video_framerate  Output Video Frame Rate (fps). Not required. (default = 25 fps)
                * @param string  $audio_codec      Output Audio Codec. Not required if it's covered by $file_format
                * @param integer $audio_bitrate    Output Audio Bitrate (kbit/s). Not required. (default = 64 kbit/s)
                * @param integer $audio_freq       Output Audio Frequency (Hz). Not required. (default = 44100 Hz)
                * @param string  $add              Additional ffmpeg command parameters. Not required
                * @param string  $pipe             Additional pipe after ffmpeg command. Not required (should not begins with '|')
                * @param bool    $overwrite        Overwrite Output File Flag. Not required. (default = false)
                * @return mixed */
            
            public function convert(
                $in_file, $out_file, $file_format, $width, $height,       // Required
                $watermark_img="",                                        // Watermark image
                $video_codec="", $video_bitrate="", $video_framerate="",  // For video
                $audio_codec="", $audio_bitrate="", $audio_freq="",       // For audio
                $add="", $pipe="", $overwrite=false                       // Misc
            ) {
            
                if (!file_exists($in_file))
                    return "Convert error: Input file $in_file not exists";
            
                if (!is_readable($in_file))
                    return "Convert error: Cannot read from input file $in_file";
            
                if (file_exists($out_file)) {
            
                    if (!$overwrite)
                        return "Convert error: File $out_file already exists";
            
                    elseif (!is_writable($out_file))
                        return "Convert error: Cannot write to $out_file. Please check the permissions";
            
                } elseif (!touch($out_file))
                    return "Convert error: Cannot write to $out_file. Please check the permissions";
            
                else
                    unlink($out_file);
            
                if (!in_array($file_format, $this->encode_file_formats))
                    return "Convert error: Cannot encode $file_format file format";
            
                if (!is_int($width) || !is_int($height))
                    return "Convert error: Invalid width or height. Integer values expected.";
            
                if ($watermark_img) {
            
                    if (!$this->watermark_so)
                        return "Convert error: watermark.so vhook file location is not defined";
            
                    if (!file_exists($watermark_img))
                        return "Convert error: Watermark image $watermark_img not exists";
            
                    if (!is_readable($watermark_img))
                        return "Convert error: Cannot read watermark image $watermark_img";
            
                    list($w, $h) = getimagesize($watermark_img);
            
                    if (!$w || !$h)
                        return "Convert error: $watermark_img is not an image";
                }
            
                if ($video_codec && !in_array($video_codec, $this->encode_video_codecs))
                    return "Convert error: Cannot encode $video_codec video codec";
            
                if ($video_bitrate && !is_int($video_bitrate))
                    return "Convert error: Invalid video bitrate - $video_bitrate. Integer expected";
            
                if ($video_framerate && !is_int($video_framerate))
                    return "Convert error: Invalid video framerate - $video_framerate. Integer expected";
            
                if ($audio_codec && !in_array($audio_codec, $this->encode_audio_codecs))
                    return "Convert error: Cannot encode $audio_codec audio codec";
            
                if ($audio_bitrate && !is_int($audio_bitrate))
                    return "Convert error: Invalid audio bitrate - $audio_bitrate. Integer expected";
            
                if ($audio_freq && !is_int($audio_freq))
                    return "Convert error: Invalid audio frequency - $audio_freq. Integer expected";
            
                $cmd = "{$this->bin} -i \"" . str_replace('"', "\\\"", $in_file) . "\" -f $file_format -s {$width}x$height";
                if ($watermark_img)     $cmd .= " -vhook \"{$this->watermark_so} -f $watermark_img\"";
                if ($video_codec)       $cmd .= " -vcodec $video_codec";
                if ($video_bitrate)     $cmd .= " -b $video_bitrate";
                if ($video_framerate)   $cmd .= " -r $video_framerate";
                if ($audio_codec)       $cmd .= " -acodec $audio_codec";
                if ($audio_bitrate)     $cmd .= " -ab $audio_bitrate";
                if ($audio_freq)        $cmd .= " -ar $audio_freq";
                if ($add)               $cmd .= " $add";
                $cmd .= " -y $out_file";
                if ($pipe)              $cmd .= " - | $pipe";
                $cmd .= " 2>&1";
            
                $output = shell_exec($cmd);
            
                preg_match('/([^\n]+)\n?$/s', $output, $last);
                $last = $last[1];
            
                return preg_match('/^video:\d*\w{0,2}\s+audio:\d*\w{0,2}/', $last)
                  ? true
                  : trim($output);
            }
            }
            
            ?>
              Write a Reply...