PHP MediaToolkit – A PHP Wrapper for basic FFMPEG functions

Posted on December 19th, 2011

This is a quick little class/ utility I developed for one of my new start-up projects. Basically it is a simple PHP class that simplifies FFMPEG, this class does not require you to have PHP-FFMPEG installed or setup on your server. All you need is Mencoder and FFMPEG and the location of the bin file. This class allows you to call simple PHP functions to convert vids, create thumbnails, add watermarks, extract audio and more. The simple toolkit became handy in development for me, and I hope other users will find this class useful too. It’s free and open source to use freely on any project.

Also since FFMPEG does not allow watermarks in the latest version for security reasons, I create a neat little function that uses mencoder to add a subtitle that works as a watermark to your vid. If there’s any issues you run in with when using the class, just post a comment below. I can’t promise consistent fixes or mods, but I will try if requested.

Here’s how to use it:

1
2
3
4
<?php
$toolkit = new Media_Toolkit;
// Will return the command line output of the media's info as an array
$toolkit->get_media_info("sample.avi");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
class Media_Toolkit {
   var $version = "1.0";
   var $supported_formats = array("mp4", "mov", "m2v", "3gp", "3g2", "m4v", "flv", "wmv", "avi", "mpg");
   var $ffmpeg = "/usr/bin/ffmpeg";
   var $mencoder = "/usr/bin/mencoder";
   
   function __construct(){}
   
   function dump($array)
   {
      for($i=0;$i<count($array);$i++){
         echo $i."> ".$array[$i]."<br>";
      }
   }
   
   function get_media_format($media){
      $format = substr($media, -3);
      return $format;
   }
   
   function get_media_info($media){
      /* Returns an array of the vid info */
      exec($this->ffmpeg.' -i '.$media.' 2>&1', $output);  
      return $output;
   }
   
   function get_media_duration($media){
      $media_info = $this->get_media_info($media);
      for($i=0;$i<count($media_info);$i++){
         if(strstr($media_info[$i], "Duration:")){
            $key = $i;
            break;
         }
      }
      $duration = explode(",", $media_info[$key]);
      return $duration;
   }
   
   function get_media_audio($media){
      /* Extracts sound from the media file and output's the sound as mp3 */
      exec($this->ffmpeg.' -i '.$media.' -vn -ar 44100 -ac 2 -ab 192 -f mp3 audio.mp3 2>&1', $output);
      return $output;
   }
   
   function get_media_screenshot($media, $frame_size="320x240", $seek="00:00:02"){
      /* Extracts a screenshot from the vid */
      $media_type = $this->get_media_format($media);
      exec($this->ffmpeg.' -i '.$media.' -an -s '.$frame_size.' -ss '.$seek.' -r 1 -y -an '.$media.'.jpg 2>&1', $output);
      return $output;
   }
   
   function get_media_clip($media, $start="00:00:10", $finish="00:00:10", $output_format=NULL){
      /* Extracts a media clip from the vid */
      if($output_format==NULL){
         $output_format = $this->get_media_format($media);
      }
      exec($this->ffmpeg.' -i '.$media.' -ss '.$start.' -t '.$finish.' '.$media.'.clip.'.$output_format.' 2>&1', $output);
      return $output;
   }
   
   function watermark_media($media, $watermark, $position="bottom_right", $tool="mencoder"){
      /* Adds a watermark to a media file */
      if($tool=="vhook"){
         if($position=="top_left"){ $pos = "10:10"; } else if($position=="top_right"){ $pos = "main_w-overlay_w-10:10"; } else if($position=="bottom_left"){ $pos = "10:main_h-overlay_h-10"; } else if($position=="bottom_right"){ $pos = "main_w-overlay_w-10:main_h-overlay_h-10"; }
         if(is_file($watermark)){
            exec($this->ffmpeg.' -i '.$media.' -vhook "movie='.$watermark.' [watermark]; [in][watermark] overlay='.$pos.' [out]" output_'.$media.' 2>&1', $output);
         } else {
            exec($this->ffmpeg.' -i '.$media.' -vhook "/usr/local/lib/vhook/drawtext.so -f /usr/share/fonts/truetype/msttcorefonts/arial.ttf -x 5 -y 5 -t '.$watermark.'" output_'.$media.' 2>&1', $output);
         }
      } else if($tool=="mencoder"){
         $media_type = $this->get_media_format($media);
         $media_info = $this->get_media_info($media);
         $watermark = "1\n00:00:00,000 --> 99:99:99,999\n$watermark";
         file_put_contents("watermark.srt", $watermark);
         if($media_type=="avi"){
            exec($this->mencoder.' '.$media.' -o output_'.$media.' -ovc xvid -xvidencopts fixed_quant=3 -oac copy -sub "watermark.srt" -font "verdana.ttf" -subpos 98 2>&1', $output);
         } else {
            exec($this->mencoder.' -sub watermark.srt -subpos 98 -subfont-text-scale 4 -utf8 -oac mp3lame -lameopts cbr=128 -ovc lavc -lavcopts vcodec=mjpeg -vf scale=320:-2,expand=:240:::1 -o '.$media.' '.$media.' 2>&1', $output);
         }
         unlink("watermark.srt");
      }
      return $output;
   }
   
   function convert_media($media, $output_format, $frame_size="320x240"){
      /* Converts a media from one format to another that is specified */
      $media_type = $this->get_media_format($media);
      $converted_media = str_replace(".".$media_type, ".".$output_format, $media);
      if($output_format=="flv"){
         exec($this->ffmpeg.' -i '.$media.' -sameq -ab 56 -ar 44100 -b 200 -r 15 -s '.$frame_size.' -f flv '.$converted_media.' 2>&1', $output);
      }
      return $output;
   }
}

One Comment on “PHP MediaToolkit – A PHP Wrapper for basic FFMPEG functions”

1 g2x3k said at 7:13 am on February 14th, 2012:

well i came up with this

http://pastie.org/3380929


Leave a Reply

    To syntax highlight code just use [cc lang="whatever language here"]your code here[/cc]