Skip to main content

PHP Media Handler Pro

PHP Media Handler Pro is a utility script & ffmpeg wrapper for publishing multi format, multi level videos and audio processing, post watermark on videos, grab single or multiple thumbs, set meta information to videos to make it compatible for streaming on the web and perform various advance operations using various tools (ffmpeg, mp4box, flvtool, segmentor etc) on the fly in php applications.

It allows you to perform complex operations just by simply write few lines of code.


Features

List of core features of php media handler pro script.

  • Process almost every type of ffmpeg commands to process videos, audio files and images
  • Advance script for multi format (mp4, webm, wmv, mp3 etc), multi level (240p, 360p, 480p etc) media processing in single step.
  • Grab single or multiple thumbs from videos.
  • Split videos into small clips.
  • Set meta information to mp4, flv videos in order to make it streameable on the web.
  • Parsing useful information from media files
  • Support both windows and linux based os
  • and more

Example Code

Example 1: Sample code below will fetch information e.g duration from media files.

include_once("mediahandler.php");
$servicePath =  $rootPath . "ffmpeg/"; // ffmpeg path
// sample code
$mhandler = new MediaHandler();
$mhandler->servicePath = $servicePath;
$mhandler->inputPath = $rootPath . "contents/source/";
$mhandler->fileName = "sample.mp4";
$info = $mhandler->getInfo();
// read more about $info object click here
if($info->errorcode > 0)
{
	echo "Error Code: " . $info->errorcode . ", Message: " . $info->errorMessage;
	exit;
}
$duration = $info->duration; // output like 2:04
$duration_sec = $info->duration_sec; // output in seconds like 124
Example 2: Sample code for publishing mp4 videos and setting meta information using php media handler pro
include_once("mediahandler.php");
// increase execution time
ini_set('max_execution_time', 3600);
$servicePath =  $rootPath . "ffmpeg/"; // use proper ffmpeg path in linux
$mp4boxPath =  $rootPath . "mp4box/"; // use proper mp4box path in linux
$mhandler = new MediaHandler();
$mhandler->servicePath = $servicePath;
$mhandler->inputPath = $rootPath . "contents/source/"; // source folder
$mhandler->outputPath = $rootPath . "contents/published/"; // published folder
$mhandler->fileName = "sample.avi"; // source file
$mhandler->outputfileName = "sample.mp4"; // published file
// preset file
$presetPath = escapeshellarg($rootPath . "ffmpeg/presets/libx264-ipod640.ffpreset");
$mhandler->parameters = "-fpre " . $presetPath . " -s 640x380 -b:v 500k -bufsize 500k -b:a 128k -ar 44100 -c:v libx264 -y"; // ffmpeg command
$info = $mhandler->Process();
// read more about $info object click here
if($info->errorcode > 0)
{
	echo "Error Code: " . $info->errorcode . ", Message: " . $info->errorMessage;
	exit;
} 
// SET Meta Information for Published MP4 Video
$mhandler->servicePath = $mp4boxPath;
$mhandler->command = '-isma -hint ' .escapeshellarg($mhandler->outputPath . "" . $mhandler->outputfileName);
$vinfo = $mhandler->setMeta();
if($vinfo->errorcode > 0)
{
	echo "Error Code: " . $vinfo->errorcode . ", Message: " . $vinfo->errorMessage;
	exit;
}
echo "FFMPEG Output: " . $info->ffmpegOutput; 
echo returnOutput($info);

Example 03 Post watermark on video.

// post watermark
$watermarkPath = "watermark.png"; // watermark.png must be placed in folder where this page (.php) located
// Post Watermark on Top Left of Video
//$mhandler->parameters .= " -vf \"movie = " . $watermarkPath . " [watermark]; [in][watermark] overlay =10:10 [out]\"";
// Post Watermark on Top Right of Video
//$mhandler->parameters .= " -vf \"movie = " . $watermarkPath . " [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]\"";
// Post Watermark on Bottom Left of Video
//$mhandler->parameters .= " -vf \"movie = " . $watermarkPath . " [watermark]; [in][watermark] overlay=10:main_h-overlay_h-10 [out]\"";
// Post Watermark on Bottom Right of Video
$mhandler->parameters .= " -vf \"movie = " . $watermarkPath . " [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]\"";
$info = $mhandler->Process();

Example 04 Grab multiple thumbnails from start to end of video.

include_once("mediahandler.php");
$servicePath =  $rootPath . "ffmpeg/"; // ffmpeg path
$mhandler = new MediaHandler();
$mhandler->servicePath = $servicePath;
$mhandler->inputPath = $rootPath . "contents/source/";
$mhandler->outputPath = $rootPath . "contents/thumbs/";
$mhandler->fileName = "sample.mp4"; // source video
$mhandler->outputfileName = "sample"; // thumb start index e.g sample_ goes to sample_001.jpg etc
$mhandler->parameters = "-f image2 -s 120x100"; // ffmpeg commands
// thumb options
$mhandler->isMultiple = true; // enable multiple thumbs
$mhandler->totalThumbs = 34;  // total no of thumbs to capture
$mhandler->startPosition = 5; // start position in seconds
$mhandler->imageFormat = ".jpg"; // image format
$info = $mhandler->grabThumbs();
// read more about $info object click here
if($info->errorcode > 0)
{
	echo "Error Code: " . $vinfo->errorcode . ", Message: " . $vinfo->errorMessage;
	exit;
} 
echo "thumbs grabbed";

Example 05 Grab single thumbnail from middle of video.

include_once("mediahandler.php");
$servicePath =  $rootPath . "ffmpeg/"; // ffmpeg path
$mhandler = new MediaHandler();
$mhandler->servicePath = $servicePath;
$mhandler->inputPath = $rootPath . "contents/source/";
$mhandler->outputPath = $rootPath . "contents/thumbs/";
$mhandler->fileName = "sample.mp4";
$mhandler->outputfileName = "sample";
$mhandler->parameters = "-f image2 -s 120x100";
// thumb options
$mhandler->isMultiple = false; // grab single thumbnail oly
// calculate mid duration of video
$startposition = 0;
$vinfo = $mhandler->getInfo(false);
if($vinfo->duration_sec >0)
  $startposition = (int)($vinfo->duration_sec/2);
else
  $startposition = 5;
$mhandler->startPosition = $startposition;
$mhandler->imageFormat = ".jpg";
$info = $mhandler->grabThumbs();
// read more about $info object click here
if($info->errorcode > 0)
{
	echo "Error Code: " . $vinfo->errorcode . ", Message: " . $vinfo->errorMessage;
	exit;
} 
echo "thumbnail captured";

Example 06 Split video in smaller clips.

include_once("mediahandler.php");
ini_set('max_execution_time', 3600); // increase execution time
$servicePath =  $rootPath . "ffmpeg/"; // ffmpeg path
$mhandler = new MediaHandler();
$mhandler->servicePath = $servicePath;
$mhandler->inputPath = $rootPath . "contents/source/";
$mhandler->outputPath = $rootPath . "contents/published/";
$mhandler->fileName = "sample.avi";
$mhandler->outputfileName = "sample.mp4";
// preset file
$presetPath = escapeshellarg($rootPath . "ffmpeg/presets/libx264-ipod640.ffpreset");
$mhandler->parameters = "-fpre " . $presetPath . " -vcodec libx264 -s 640x380 -b 500k -ab 128k -ar 44100 -y"; // ffmpeg command
$totalclips = 5; // total no of clips to be created
$length = 10; // length of each clip in seconds
$info = $mhandler->splitVideo($length, $totalclips);
// read more about $info object click here
if($info->errorcode > 0)
{
	echo "Error Code: " . $vinfo->errorcode . ", Message: " . $vinfo->errorMessage;
	exit;
} 
echo "thumbnail captured";

Advance Example

PHP Media Handler Pro provide advance media processing script which allow you to perform almost all normal operations in single steps using few lines of code.

Operations can be

  • Publish source video to 360p mp4 video
  • Publish source video to 720p mp4 video (for HD or paid view)
  • Publish source video to 480p webm video (for HD or paid view)
  • Set meta for mp4 videos to make it streamable on web.
  • Publish source video to mp3 audio
  • Grab 15 thumbs from start to end of video (for listing preview)
  • Post watermark on video
  • Fetch information at the end of operation

Below example will do all these operations by using single step.

include_once("encoder.php");
ini_set('max_execution_time', 3600); // set max execution time
$servicePath =  $rootPath . "ffmpeg/"; // ffmpeg path
$mhandler = new mhpEncoder();
$mhandler->rootPath = $rootPath;
$mhandler->servicePath = $servicePath;
$mhandler->sourcePath = $rootPath . "contents/source/";
$mhandler->publishPath = $rootPath . "contents/published/";
$mhandler->thumbsDirectory = $rootPath . "contents/thumbs/";
$mhandler->sourceFileName = "sample.avi";
$mhandler->publishFileName = "sample.mp4";
$mhandler->grabThumbs = true;
$itags = array();
// read more about encoding templates click here
$itags[] = 5; // 360p mp4 encoding
$itags[] = 7; // 720p mp4 encoding
$itags[] = 11; // 480p webm encoding
$itags[] = 14; // mp3 encoding
$mhandler->itags = $itags;
$mhandler->postWatermark = true; // enable post watermark
$mhandler->watermarkPath = "watermark.png";
$mhandler->watermarkLocation = 3; // watermark on bottom right
$info = $mhandler->Process();
if($info->errorcode > 0)
{
	echo "Error Code: " . $vinfo->errorcode . ", Message: " . $vinfo->errorMessage;
	exit;
} 
echo returnOutput($info);