DevSnips.com Code Snippet Repository
search:    


Navigation
  Home
About
Library
Contact
 
Snippet Library
  ColdFusion   338  
  ASP   201  
  PHP   101  
  HTML   11  
  JavaScript   77  
  XML   2  
  CSS   5  
  SQL   13  
  JSP   2  
  C#   1  
  ASP.NET   0  
  Submit a Code Snippet
 
Blog Archive
  September 2007
August 2007
July 2007
June 2007
May 2007
November 2006
October 2006
Search Archives
 
Random Affiliates
  BioMetric Base
Uno-Code
Tom Morris
ReviewMe!

Want to become an affiliate?
Read more...


Privacy Policy
© 2012

Blog Archive

 
Frame capture with PHP and mplayer

So you wanna show screen snaps of your uploaded movies like YouTube, etc?? I was presented with this request from a client, and I thought it would be great to implement it and share what I learned. It's quite easy really.

My environment for this is a Linux server with Apache and PHP installed. Also, there are some caveats here. First off, we need to have mplayer installed on the server. Mplayer is the tool to view the movie and export a frameshot. The next caveat is the ability to run system commands or exec() with PHP. Depending on your hosting platform, that may be removed for security, and you might have trouble accessing the mplayer binary if PHP is running in safe_mode.

If anyone knows of a PECL wrapper or PHP configure option to enable mplayer hooks via PHP, please let me know. At the time of this writing, I was having trouble finding anything like that. I personally, would like to not use exec() or system() in my programming, since that can provide a potential security issue down the road. If you do use those system function please use this function [ escapeshellcmd() ] to sanitize the command passed to it.

The process is quite simple. Administrator uploads a movie that is one of your defined extensions (wmv, mov, etc). Once the movie is uploaded, you pass arguments to mplayer to open the uploaded movie. You also specify how many seconds into the movie you want the frameshot take. Once the time line has been met a frameshot and PNG file is created. Next you would size and optimize the PNG to fit your thumbnail format.


$filepath			= "/path/to/yourdomain/";
$fileName			= "NameOfFileAfterUpload.mov";
$mplayerPath		= "/usr/bin/mplayer";
$args				= " -vo png -ss 0:05 -frames 2 -nosound -really-quiet";
$path				= $filepath."files/videos/".$fileName;
if(file_exists($path)){
	exec($command);
	$frameName		= "00000002.png";
	if(file_exists($filepath."admin/video/".$frameName)){
		// resize and move file with gd functions
		// delete the original frame when we're done
		unlink($filepath."admin/video/".$frameName);
	}else{
		// handle error -- frameshot does NOT exist
	}
}else{
	// handle error -- uploaded video does not exist
}

The frameshot will be created in the directory where the script is being executed. You may need to change permissions of this directory to be writable by Apache. The frame name will always be named 00000002.png.

As you can see the script is really straight forward. I've eliminited the upload portion and validation. Remember to validate your file and ensure it's the correct file type before working with it. I have a great serverside validation function that I'll be putting together for everyone... since $_FILES['fieldName']['type'] cannot be trusted!

Thanks for reading!


Submitted on 06/07/07 at 4:53PM
Post Comment | Comments: 0
Bookmark to:
Add 'Frame capture with PHP and mplayer' to Del.icio.us Add 'Frame capture with PHP and mplayer' to digg Add 'Frame capture with PHP and mplayer' to FURL Add 'Frame capture with PHP and mplayer' to blinklist Add 'Frame capture with PHP and mplayer' to reddit Add 'Frame capture with PHP and mplayer' to Feed Me Links Add 'Frame capture with PHP and mplayer' to Technorati Add 'Frame capture with PHP and mplayer' to Yahoo My Web Add 'Frame capture with PHP and mplayer' to Newsvine Add 'Frame capture with PHP and mplayer' to Socializer Add 'Frame capture with PHP and mplayer' to Ma.gnolia Add 'Frame capture with PHP and mplayer' to Stumble Upon Add 'Frame capture with PHP and mplayer' to Google Bookmarks Add 'Frame capture with PHP and mplayer' to RawSugar Add 'Frame capture with PHP and mplayer' to Squidoo Add 'Frame capture with PHP and mplayer' to Spurl Add 'Frame capture with PHP and mplayer' to BlinkBits Add 'Frame capture with PHP and mplayer' to Netvouz Add 'Frame capture with PHP and mplayer' to Rojo Add 'Frame capture with PHP and mplayer' to Blogmarks Add 'Frame capture with PHP and mplayer' to Shadows Add 'Frame capture with PHP and mplayer' to Simpy Add 'Frame capture with PHP and mplayer' to Co.mments Add 'Frame capture with PHP and mplayer' to Scuttle

Go Back








Advertisements

Mozy.com, and insurance policy for your Hard Drive