Web Hosting Talk







View Full Version : PHP script to convert Video files to Flash files


DelPierro
01-28-2007, 03:32 AM
I am getting a blank white page when I attempt to upload a file, and no files is being uploaded, nor converted.


<?php
ini_set( "max_execution_time", "3600" ); // sets the maximum execution time of this script to 1 hour.
$fileName = $_FILES['message']['name']; // get client side file name


if( $fileName ) {
// Check File Type
$fileNameParts = explode( ".", $fileName ); // seperate the name from the ext
$fileExtension = end( $fileNameParts ); // part behind last dot
$fileExtension = strtolower( $fileExtension ); // reduce to lower case

if( !$fileExtension == "mpg" && !$fileExtension == "avi" && !$fileExtension == "mpeg" && !$fileExtension == "wmv" && !$fileExtension == "rm" && !$fileExtension == "dat" ) {
die( "Invalid Video Format." );
}
// Check File Size
$fileSize = $_FILES['message']['size']; // size of uploaded file
if( $fileSize == 0 ) {

die( "Sorry. The upload of $fileName has failed. The file size is 0." );
} else if( $fileSize > 10240000 ) { //10 MB
die( "Sorry. The file $fileName is larger than 10MB. Advice: reduce the file quality and upload again." );

} else {
$uploadDir = '/home/coconut/www/conversion/uploaded/'; // Where the temp file will go
$uploadFile = str_replace( " ", "", $uploadDir . $_FILES['message']['name'] ); // Get rid of spaces in the filename

$finalDir = '/home/coconut/www/conversion/converted/'; // Where the final file will go
$finalFile = str_replace( " ", "", $finalDir . $fileNameParts[0] . ".flv" ); // Get rid of spaces in the filename

if ( !move_uploaded_file( $_FILES['message']['tmp_name'], $uploadFile ) ) {
echo "Possible file upload attack! Here's some debugging info:\n";
echo( $_FILES );
}


$encode_cmd = "/usr/bin/ffmpeg -i $uploadFile -acodec mp3 -ar 22050 -ab 32 -f flv -author \"Clip Author\" -copyright \"Clip Copyright\" $finalFile";
exec( $encode_cmd );

unlink( $uploadFile );
chmod( $finalFile, 0644 );
}
}
?>



The upload form:


<form action="convert.php" method="post" enctype="multipart/form-data"> <input type="file" name="audio" size="42"> <input type="submit" name="audio_submit" value="Submit"> </form>


Thanks

Disclaimer: I did not write this script, rather have modified it to server my needs.

EDIT: I have FFMPEG installed on my box.

jamessampford
01-28-2007, 06:08 AM
put several echo statements into the php code, and see if what your submitting is working in anyway

for example, the first one you could do is


echo "$fileName";


the rest - look through the code to see where you should be getting values, if nothing, you might be able to trace why.

if that makes you none the wiser - did it work in the first place, with little modications?

If it had a different upload page, try use that instead, and see where you have gone wrong.

mitchlrm
01-28-2007, 02:35 PM
Yea put some echos in put also look at the upload error message codes. See http://www.php.net/manual/en/features.file-upload.errors.php php has a default upload size of 4mb which isn't that large for video. You may be running into that limit.

P-nut
01-28-2007, 05:53 PM
Instead of using

echo $_FILES

try using

print_r($_FILES)

$_FILES is an array - similar to $_POST - and if you're getting an error with the $_FILES array I don't believe you'll be able to see the debugging info with an echo.

horizon
01-28-2007, 10:13 PM
if( !$fileExtension == "mpg" && !$fileExtension == "avi" && !$fileExtension == "mpeg" && !$fileExtension == "wmv" && !$fileExtension == "rm" && !$fileExtension == "dat" ) {
die( "Invalid Video Format." );
}


You could replace that by stating:


$extension_array = array("mpg", "avi", "mpeg", "wmv", "rm", "dat");

if (!in_array($extension_array)) {
die("Invalid Video Format.");
}



Much shorter if statement this way. :)

xgoth3
02-02-2007, 04:28 PM
Thak you horizon for letting us know that people learn here... you are the best example.. but come on... what you're suggesting is way too far from the reason of this thread.... Congratulations anyway, i would have put the file types on an array too.

Now, returning to the subject:

DelPierro, you have to place a hidden field on your form called "MAX_FILE_SIZE", this field MUST preceed the file input field (http://www.php.net/manual/en/features.file-upload.php) otherwise the file doesn't get uploaded on many PHP configurations.

horizon
02-02-2007, 07:32 PM
My post above was only as a suggestion.

lonea
02-02-2010, 07:20 AM
echo $encode_cmd then try to run that cmd in shell and see what happen

mattle
02-02-2010, 09:21 AM
echo $encode_cmd then try to run that cmd in shell and see what happen

Or, use exec()'s optional arguments (the extra bit here just routes STDERR to STDOUT so you trap error messages as well):


exec("$encode_cmd 2>&1", $output, $status);

if ($status)
{
print_r($output);
die("Exited with status: $status");
}The problem with running the command in the shell is that your user account will have a different environment than the web server, so you may not catch permission issues, etc.

mattle
04-15-2010, 02:04 PM
nvrmind...can't believe I got duped into replying...

csparks
02-01-2011, 12:40 AM
what does your form initialization string look like? (From the form you are uploading the file from). You need to ensure you have enctype="multipart/form-data", so for example


<form enctype="multipart/form-data" method="post" action="path/to/file.php">
...
</form>

coderiser
02-02-2011, 12:43 AM
dont try and use and entire script you found somewhere upload the file or select a file that is uploaded and then once thats covered

just run your ffmpeg convert in the shell_exec function and your done

shell_exec('ffmpeg convert goes here');

I would suggest you run the ffmpeg via command line before your place it in your script so you can verify you have that part right and you can try the following to make sure your shell_exec is functioning on your server correctly

<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>

praveenkv1988
02-02-2011, 03:22 AM
If you are getting blank page, it appears that the script was terminated before the upload is complete. Check your script execution time.