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!
Go Back
