Reputation: 1260
Below is my code where it uploads a file into a server and stores the names of each uploaded file into the db:
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
if ($_FILES['fileVideo']['error'] === UPLOAD_ERR_OK) {
$result = 0;
if( file_exists("VideoFiles/".$_FILES['fileVideo']['name'])) {
$parts = explode(".",$_FILES['fileVideo']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while( file_exists("VideoFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileVideo']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileVideo"]["tmp_name"],
"VideoFiles/" . $_FILES["fileVideo"]["name"]);
$result = 1;
}
else
{
move_uploaded_file($_FILES["fileVideo"]["tmp_name"],
"VideoFiles/" . $_FILES["fileVideo"]["name"]);
$result = 1;
}
$videosql = "INSERT INTO Video (VideoFile)
VALUES (?)";
if (!$insert = $mysqli->prepare($videosql)) {
// Handle errors with prepare operation here
}
//Assign the variable
$vid = 'VideoFiles/'.$_FILES['fileVideo']['name'];
//Dont pass data directly to bind_param store it in a variable
$insert->bind_param("s",$vid);
$insert->execute();
$id = $mysqli->insert_id;
if ($insert->errno) {
// Handle query error here
}
$insert->close();
}else{
echo "Upload was not successful";
}
?>
<script language="javascript" type="text/javascript">
window.top.stopVideoUpload(<?php echo $result; ?>,'<?php echo $id; ?>', '<?php echo $_FILES['fileVideo']['name']; ?>');
</script>
</body>
</html>
Now I am using a jwplayer and it requires video files to match video formats on this page:
So I need to be able to encode files automatically on the server when the file is uploaded into the server. I do not want the user to try and encode a video file manually by themseleves, I want it done automatically. But my question is how can I get automatic server side file encoding to work?
I have a demo showing how a video file is uploaded: DEMO
To use Demo:
Click on Add Question
button and file input will appear in table
Click on upload straightaway and you will see a simple validation stating which video file format are allowed (this is simply done by checking video file extension)
Browse for a video file, select and then click on Upload
and wait for upload to finish (I recommend a short video file for saving time). When file is uploaded it will display success message and video file is uploaded into server,
Upvotes: 0
Views: 285
Reputation: 33511
Many ways of doing that. The easiest and fastest in terms of programmer time is to use ffmpeg to directly encode the thing into something useable, right in your upload script (use exec
or system
to do so).
This imposes some problems though: your browser might drop in a timeout when the server is too busy processing the request. Also, you cannot really spread the load.
So, after the upload is succesfull, you call ffmpeg:
exec("ffmpeg -i $vid -vcodec libx264 -vpre default -crf 21 ".
"-acodec libfaac -ab 128k $vid-transcoded.mp4");
You will have to refer to the ffmpeg
website for all options (there are a lot of them). Also, jwplayer is quite popular, so there should be many resources on how to transcode it.
So, another solution is to have the server upload files in a to_be_processed
directory and have a background process (cronjob perhaps?) to process all files in that dir. That way, the browser request is done when the upload is done and you are in full control of processing resources. This, however, takes a considerate amount of programmer time. You will have to create the processing job, as well as do some administration work to let the user know how far the processing is.
Upvotes: 3