Wednesday, May 30, 2007

PHP- Upload and convert avi, wmv, mov and mpg videos to FLV

Upload and convert avi, wmv, mov and mpg videos to FLV using PHP

1.First you have to create a page from where the user will upload a video lets
call it uploadvideo.php.

follow the code below..


<form name=”frm” action=”uploadvideopro.php” method
=”post” enctype="multipart/form-data" >

<input name="x_URL" type="file" class="form1"
size="26">

<input type="submit" name="submit" value="upload"
>

</form>




2.Secondly on the process page i.e. uploadvideopro.php . follow the following
code..




/***************Load FFMPEG *********************************/



$extension = "ffmpeg";

$extension_soname = $extension . "." . PHP_SHLIB_SUFFIX;

$extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;





// load extension

if (!extension_loaded($extension)) {

dl($extension_soname) or die("Can't load extension $extension_fullname\n");

}



/***********************************************************/




/*****************Get the path to Extention ****************/



$array_path = explode("/",$_SERVER['SCRIPT_FILENAME']);

$dynamic_path = "";

for ($i=0;$i<sizeof($array_path)-1;$i++)

if($array_path[$i]!="")

$dynamic_path =$dynamic_path."/".$array_path[$i];


/**********************************************************/



/******************set folders*****************************/

$flvpath = "flvfiles/";

$moviepath = "movies/" ;

chmod($moviepath,0777);

chmod($flvpath,0777);

/*********************************************************/



/******************Upload and convert video *****************************/




if(isset($_FILES["x_URL"]))

{

$fileName = $_FILES["x_URL"]["name"];

$fileNameParts = explode( ".", $fileName );

$fileExtension = end( $fileNameParts );

$fileExtension = strtolower( $fileExtension );


if($fileExtension=="avi" || $fileExtension=="wmv" || $fileExtension=="mpeg"
|| $fileExtension=="mpg" || $fileExtension=="mov" )

{

if ( move_uploaded_file($_FILES["x_URL"]["tmp_name"],$ moviepath.$_FILES["x_URL"]["name"])
)

{




if( $fileExtension == "wmv" ) {


exec("ffmpeg -i ".$dynamic_path."/".$ moviepath."".$fileName."
-sameq -acodec mp3 -ar 22050 -ab 32 -f flv -s 320x240 ".$dynamic_path."/".$flvpath."myflv.flv");

}


if( $fileExtension == "avi" || $fileExtension=="mpg" ||
$fileExtension=="mpeg" || $fileExtension=="mov" ) {


exec("ffmpeg -i ".$dynamic_path."/".$ moviepath."".$fileName."
-sameq -acodec mp3 -ar 22050 -ab 32 -f flv -s 320x240 ".$dynamic_path."/".$flvpath."myflv.flv");

}


/******************create thumbnail***************/

exec("ffmpeg -y -i ".$dynamic_path."/".$moviepath."".$fileName."
-vframes 1 -ss 00:00:03 -an -vcodec png -f rawvideo -s 110x90 ".$dynamic_path."/".$flvpath."myflv.png");



}

else

{

die("The file was not uploaded");

}


}



else

{

die("Please upload file only with avi, wmv, mov or mpg extension!");

}

}

else

{

die("File not found");

}






Disclaimer: Any code or advice given is for instructional purposes only. We will not be responsible for any loss or damage caused by using this script.

36 comments:

guangming said...

Shop a great selection of Nike Air Maxauthentic Nike shoes& wholesale nike shoeswith reasonable price for the entire families at nike-shoes-max.com.nike shoes Nike Shox R4Nike Jordan100% quality guaranteed and smooth customer service.UGG Women's Classic Cardy Boots 5819 are available with colorful knit uppers (composed of a wool blend) and a sheepskin sock liner for extra comfort.ugg boots It is detailed with three oversized wood buttons, allowing it to be styled buttoned up, australia uggslouched down, slightly unbuttoned, or completely cuffed down. They have a light and flexible EVA outsole along with a suede heel guard provides durable wear all season long. That is why it is one of several styles that have been all time favorites with women.

sally21c said...

The factors you need to be aware of zigtech, the two most important is the durability and Comfort of the zigtech reebok. Paying for high price for Moncler from a reputable brand is not a bad idea As They must have used better quality materials that will withstand The Elements is zigtech shoes. A well manufactured jacket Will show at Signs of Reebok Easytoneeasytone and easytone shoes should be easy to use.

sally21c said...

Moncler jackets online mall,we offer Moncler coats,Moncler outlet with high quality.We also supply Moncler doudoune besides Moncler jackenMoncler online here,This is Moncler world.

sally21c said...

Moncler men and Moncler women clothing-Moncler online here,The Moncler jackets simple line of the Moncler outlet that you adopt——buy a Moncler that it has gained.

sally21c said...

We sell Moncler jackets,Moncler coats in different styles at Moncler outlet.Moncler clothing is leading a trend,moncler jacken and Moncler coats you can get online.

sally21c said...

Moncler 2010 new clothing:Moncler coats,discount Moncler online.Moncler is famous for Moncler jackets,booming development of Moncler outlet.Discount Moncler sale of moncler jacken.

sally21c said...

Look for some spyder ski suit and Spyder Jacket. Your decision of purchasing spyder ski wear,spyder pants and ski jacket will be within your budget,for we have discounts in spyder

sally21c said...

There are lots of spyder jacket.You can choose ski pants online and spyder ski wear in different styles.Our spyder brands contain spyder pants as well as ski suit

UGG Boots said...

Nike Air Max 90
Wholesale Nike Shoes
Christian Louboutin Boots
Sexy High Heels
Christian Louboutin High heels
Yves Saint Laurent High heels
Tory Burch High heels
Christian Louboutin High Heel

Nilesh said...

Hello,

I am using this script but problem is i can not convert the .avi or .mpg file which are big then 10MB.

Can you please let us know what do to do for big file convert?

Its really urgent. I am trying to find the solution last 2 days.

Thanks,

tweetwhistle said...

great tutorial. Thanks!

sandeep@W3logics said...

I loved the way you narrate the post… Thanks for that
Best Buy Promo Codes for all bestbuy products, Bestbuy provides great discount deals to its products.Best Buy Promo Codes @ BestBuyPromoCodes.net
http://bestbuypromocode.net

sandeep@W3logics said...

I loved the way you narrate the post… Thanks for that
Best Buy Promo Codes for all bestbuy products, Bestbuy provides great discount deals to its products.Best Buy Promo Codes @ BestBuyPromoCodes.net
http://bestbuypromocodes.net

DanDan Hasan said...

Web Sites

Salsan Jose said...

thank you very much for the code.
What is Windows Registry?

ankur said...
This comment has been removed by the author.
ankur said...
This comment has been removed by the author.
ankur said...

The requested URL /project/user/”uploadvideopro.php” was not found on this server
i found this error

Troy Jeffrey Childers said...

Virginia Beach Roofing

Troy Jeffrey Childers said...

Virginia Beach Roofing

Alan said...

Everything seems to work all up until line 82 of uploadvideopro.php

I should also ask if this is for linux or can I use it with xampp localhost and Windows?

I have ffmpeg flvtool etc.

Alan said...

Everything seems to work all up until line 82 of uploadvideopro.php

I should also ask if this is for linux or can I use it with xampp localhost and Windows?

I have ffmpeg flvtool etc.

Vikas Rana said...

This code is not working

MV said...

when i run this code i got an error..
the error is
Parse error: parse error, expecting `T_VARIABLE' or `'$'' in C:\wamp\www\mypages\uploadvideopro.php on line 78

ITArticle said...

Very useful code. I have implemented this in my blog. Thanks for the post: turn off Windows 8 Metro Live Tile

Siteezy said...

Great Post


http://siteezy.com/

Siteezy said...
This comment has been removed by the author.
aven peter said...

Excellent pieces. Keep posting such kind of information on your blog.
And now, i can transfer my wmv videos. that is great.
convert wmv files to mov format on mac

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

I try to follow your code but there's an error message when I try to apply in my website at http://www.MyProds.com
I try to another website of my friend at http://www.AndroidDrawer.com but still the same. Error message Parse error: parse error, expecting `T_VARIABLE' or `'$''

SummitTechnology said...

I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…
CD Manufacturing

Promo Disc said...

Writing a blog can be difficult, but even with the odds, you’ve managed to get quite an audience. I highly recommend that you consider writing an ebook one day.
DVD distribution

Promo Disc said...

Thanks for one’s marvelous posting! I truly enjoyed reading it, you are a great author.I will be sure to bookmark your blog and may come back from now on.
DVD replication

Nelson Smith said...

thank you for the great knowledge, you tricks well for me.
best keylogger

Abagail neo said...

just find a good video converter to help to do so, such as aiseesoft video converter for mac is such a tooll which I have been using for a long time for convert avi to m4v for editing and replay. just have a try at: http://www.ivideotool.com/ . good luck!!