Home   Search Articles   All Articles   Submit an Article Random Article   Contact Us
  Thu Apr 17th,2014 10:25 am Welcome Guest, Please LOGIN to your account or SIGNUP now
  Search Articles
 
Keyword
Exact phrase
All words (AND)
Any word (OR)
 
 
  Categories
  Media Server
  Action Script
  Apps
  Basic Hosting...
  Breeze
  Flash and XML
  Flash Live Streaming
  Flash Media Server
  Flash Players - PC
  Flash Players - PHP...
  Flash Players -...
  Flash Players -...
  Flash Press Releases
  Flash Video (FLV)...
  Flex
  FLV Coding
  FLV Encoding
  FLV Help
  FLV Hosting...
  FLV Software
 
  Authors
  shakespere
  hosting unleashed
  charlton Ting
  flv player maker
  flash flv
  Kevin Kelleher
  peter adre
  Mark tailer
  Delcious
  flvguru
More Authors List 
 
  Subscribe Articles
 
Email Address
Subscribe
Unsubscribe
 
 
  Our Sponsors
 
 
   
 
 Article
 Using FFMpeg to create YouTube clone script

So this entry, I’m going to get a bit complicated. I was working on a project where administrators needed to upload Windows Media formatted videos (WMV), and wanted them converted to Flash video (FLV). This would duplicate what YouTube or GoogleVideo is currently doing. The browser would then use a Flash player to play the streamed videos. Also, I would need to capture a frame for the preview. I thought this tutorial might be a good resource for others that might be looking for this functionality for their affiliate sites, etc.

Now, I must discuss my environment a bit. This tutorial will cover Linux (specifically Gentoo Linux), using PHP and a Apache web server. Also, you will need some vital packages (ffmpeg-php, ffmpeg and flvtool2). I’m guessing that a few web hosts our there won’t have any of these packages available. This where the argument of getting a dedicated server or virtual server shines. If you had your own server you could install and configure it whatever way you want. I personally prefer dedicated servers. You can pickup dedicated hosting for approximately $100-$300/month depending on what server specifications you’re looking for. All of my servers are around the $150/month rate and work great for me.

ffmpeg-php is a fantastic PHP extension, that ties into ffmpeg and can return fantastic and incredibly useful information of a raw video file (such as frame rate, bitrate and can also grab a GD image handle of a frame in the video).

ffmpeg is a audio/video converter (make sure it’s compiled with amr, encode, ogg, oss, sdl, truetype, vorbis, zlib).

flvtool2 is a manipulation tool for Flash Video files, but we need to use it to embed META data extracted from our WMV file.

You’ll also need to be sure that PHP is compiled with GD support.

I’m going to skip the upload piece to the server. If you need more information on how to get the source WMV file to the server, let me know.

Here is the main chunk of code to worry about. I’ll explain this after the code snippet.

<?php
function
makeMultipleTwo ($value){
$sType = gettype($value/2);
if($sType == "integer"){
return $value;
} else {
return ($value-1);
}
}
$flvtool2 = "/usr/bin/flvtool2";
$ffmpeg = "/usr/bin/ffmpeg";
$FileName = escapeshellcmd($FileName);
$justName = explode(".",$FileName);
$wmvName = "/path/to/videos/".$FileName
$flvPath = "/path/to/videos/".$flvName;
$ffmpegObj = new ffmpeg_movie($wmvName);
$srcWidth = makeMultipleTwo($ffmpegObj->getFrameWidth());
$srcHeight = makeMultipleTwo($ffmpegObj->getFrameHeight());
$srcFPS = $ffmpegObj->getFrameRate();
$srcAB = intval($ffmpegObj->getAudioBitRate()/1000);
$srcAR = $ffmpegObj->getAudioSampleRate();
$frameSnap = $ffmpegObj->getFrame(3);
$snapWidth = $frameSnap->getWidth();
$snapHeight = $frameSnap->getHeight();
$imageSnap = $frameSnap->toGDImage();
$thumbname = $justName[0].".jpg"; // just get the first part of the name
$imagePath = "/path/to/videos/".$thumbname;
imagejpeg($imageSnap, $imagePath, 100);
$cmd = $ffmpeg . " -i " . $wmvName . " -ar " . $srcAR . " -ab " . $srcAB . " -acodec mp3 -f flv -deinterlace -nr 500 -s 320×240 -aspect 4:3 -r ".$srcFPS." -b 270k -me_range ".$srcFPS." -i_qfactor 0.71 -g 500 " . $flvPath . " | " . $flvtool2 . " -U stdin " . $flvPath;
exec($cmd, $errArr);
unset($justName);
imagedestroy($imageSnap);
unset($frameSnap);
unset($ffmpegObj);
?>

$ffmpegObj is a object we’re creating from ffmpeg-php. Here we pass the path to where our original source WMV file is. ffmpeg-php then analyzes the file and we have various methods to extract information. The makeMultipleTwo function is there, since the width and height has to be a multiple of two (this was discussed at a great resource here). Along with the width and height information, we wan thte frame rate, audio sample rate and audio bit rate. Those are found by using the following methods (getFrameRate(), getAudioBitRate() and getAudioSampleRate()).

Next use the getFrame() method, which will grab a frame snap from the video. In this example, we’re grabbing the 3rd frame. This also returns a new object which we’re calling $frameSnap. We can access various methods within this new object (getWidth() which is the width of the image, getHeight() which is the height of the image, and toGDImage() which will return us a GD handle for the image). Once we have the GD handle for the image, we can simply write the image to the file system with imagejpeg() which is a method in the GD library.

At this point we have information about the WMV file and a frame snap for the preview image. We need to to convert the file to FLV now. Our next step is to create a command string which we’ll need to execute.

<?php
$cmd = $ffmpeg . " -i " . $wmvName . " -ar " . $srcAR . " -ab " . $srcAB . " -acodec mp3 -f flv -deinterlace -nr 500 -s 320×240 -aspect 4:3 -r ".$srcFPS." -b 270k -me_range ".$srcFPS." -i_qfactor 0.71 -g 500 " . $flvPath . " | " . $flvtool2 . " -U stdin " . $flvPath;
exec($cmd, $errArr);
?>

This converts the file to FLV, but also ‘pipes’ the output to FLVtool2 with the ‘|’. ffmpeg converts the file, but then we use flvtool2 to overwrite the file, but add important META information for FLV.

If your PHP environment is in safe_mode you will run into a problem with the ‘|’ in the command string. Since safe_mode runs all commands through escapeshellcmd() which will try to escape that character. To get around this is semi-tough if you do not have shell access to the server. I run safe_mode on my servers, so what I did was create a bash script with the command string in it, and passed values via exec() to my bash script. The ‘|’ pipe command is in the bash script, so it was not being escaped by PHP. Any time you’re dealing with exec() and shell scripts, it’s incredibly important to check and sanitize user input!!!!!

This is a tricky project, but can be quite cool when you get it working. The next piece is to get a flash player to play your FLV file. I personnally like this one:

http://www.jeroenwijering.com/?item=JW_FLV_Player

< div align="center">
<script type="text/javascript" src="lib/swfobject.js"></script>
<p id="player1" style="padding:4px 4px 4px 4px; border:#999999 1px solid; width:323px;"><br><strong>ERROR</strong><br>Looks like you do not have the Flash plugin installed, or you are using a older version (7 is the minimum required)<br><a href="http://www.macromedia.com/go/getflashplayer"><u>Get the Flash Player</u></a> to see this player/movie.<br><br></p>
<script type="text/javascript">
var s0 = new SWFObject("lib/flvplayer.swf","single","323","263","7");
s0.addParam("allowfullscreen","true");
s0.addVariable("showdownload","true");
s0.addVariable("link","<?=$webroot?>/videos/<?=$FLVName?>"); // wmv might be an option
s0.addVariable("file","<?=$webroot?>/videos/<?=$FLVName?>");
s0.addVariable("image","<?=$webroot?>/videos/<?=$ThumbName?>");
s0.addVariable("smoothing","false");
s0.addVariable("backcolor","0xFFFFFF");
s0.addVariable("largecontrols","false");
s0.addVariable(‘width’,’323′); // reset width/height for IE issues and cache/refresh
s0.addVariable(‘height’,’263′);
s0.write("player1");
</script>
< /div>

UPDATE – 1/2/2009

I found this hosting company that offers and promotes FLV hosting. Could be a very good solution for some of you! Hope this helps!

Added By Admin Email Anonymous
Category FLV Video Author Mike McGuinness
Click Here to add this article to your favorite list. Add To Favorites Click Here to print this article. Print This Article
Click Here to email this article to someone you think will like it. Email Article To A Friend Click Here to post some comments for this article. Post Comments
 
Rate This Article (17)
Added On Fri Jun 17th,2011 
 
 
  Login Here
 
Username
Password
Signup Now
Forgot password
 
 
  Top Rated
 
ESPN Chooses RTMP...
Adobe FLash Video...
Yet another free...
HOW TO STREAM LIVE...
Creating a video...
How to add add text...
 
 
  Most Popular
 
Embed Flash FLV
Updated 2011 How to...
Free CCPlayer...
Free Online FLV...
Free Youtube Player...
Choose Managed...
 
 
  Editor's Pick
 
Free Youtube Player...
Free FLV Players...
Adding Caption...
ESPN Chooses RTMP...
live stream trtmp o...
 
 
  Sponsor
 

S

P

O

N

S

O

R

A

D

 
   
 
   
 HOME | LOGIN | SIGNUP
SUBMIT AN ARTICLE | SEARCH ARTICLES | ALL ARTICLES
TERMS OF USE | PRIVACY POLICY | LEGAL POLICY | CONTACT US