DEVELOPMENT


upload snap

Generating thumbnail automatically from a video file, during uploading can be achieved by using ffmpeg player. Ffmpeg player is a freeware, that it is published under the GNU Lesser General Public License 2.1+ or GNU General Public License 2+. So, feel free to download the ffmpeg from its official link Download FFmpeg.exe.

After downloading the ffmpeg, copy the file into your server. Now, Let's begin with designing a form. Three input field is vital, to perform elementary upload operation. One input feild of the type file, other one of text which will store small descripton about the video. Last one of type hidden is just to impose a soft limit on server side, that is one will not get any warning message if any of the user try to test the limit of your form. These all input feild can be easily escaped by using spoofed form. However, no harmed in adding that.

INDEX.PHP
<html>
<head>
</head>
<body>
<form enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST"> 
Choose file to upload:<input name="upfile" type="file" />
<br/> About<input name="about" type="text" />
<br/> <input type="hidden" name="MAX_FILE_SIZE" value="200000000" />
<input type="submit" value="Upload file"/> 
</form>
    
Before proceeding to php part, let's first have a look on the database used,

database snap


1. id - unique id for each video.
2. name - title of the video.
3. user - user id of the user, who have uploaded the video.(if you are using authentiaction system for uploading the video.) user id- is the unique id assinged to each registered user, it is this id which tells us that who have uploaded the particular video.
user id- is the unique id assinged to each registered user, it is this id which tells us that who have uploaded the particular video. During the uploading process, videos detail with the user's information is also stored.
4. ext - extension of the video file.
5. date - time when the video was uploaded.
6. about - description about the video.

Now, time for some PHP...
<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") 
	{
	$c=0;
	$upfile=$_FILES['upfile']['name'];
	$about=$_POST["about"];
	$id=rand(1000,99999);
	$user="me";
 	
	
	
	$date=date('r');
	$thumb="$id."."jpg";

  	
	if($c==0)
	{
		$conn=@mysql_connect("localhost","root","") or die("could not connect to database");
		$db=@mysql_select_db("site1_db",$conn) or die("could not select database");

 		$result = mysql_query("SELECT * FROM video",$conn); 


		if (!is_dir("upload/$user")){
					mkdir("upload/$user");
					}

		$target_path = "upload/$user/";
		
		$allowedExts = array("mp4", "avi");
 		$temp = explode(".", $_FILES["upfile"]["name"]);
		$extension = end($temp);
		$target_path=$target_path . basename ($id.".".$extension);

		 if (($_FILES["upfile"]["size"] < 200000000) && in_array($extension, $allowedExts)) 
		{
 				  if ($_FILES["upfile"]["error"] > 0) 
				{
    						 echo "Return Code: " . $_FILES["upfile"]["error"] . "
"; } else { if(move_uploaded_file($_FILES['upfile']['tmp_name'],$target_path)) { include("thumbnail.php"); // this creates thumbnail $sql="insert into video(id,user,name,ext,about,date) values (\"$id\",\"$user\",\"$upfile\",\"$extension\",\"$about\",\"$date\")"; $res=mysql_query($sql,$conn) or die("could not execute "); if($res) { echo "The file is uploaded"; } else { echo "Error"; } } else { echo "Error"; } } } else { echo "Error"; } } } ?>
THUMBNAIL.PHP



<?php
$ffmpeg = 'ffmpeg.exe';
$image = "upload/$user/$id.jpg"; 
$video = "upload/$user/$id.$extension";
$interval = 5; //time to take screenshot at
$size = '320x240'; //thumbnail size
$cmd = "$ffmpeg -i $video -deinterlace -an -ss $interval -f mjpeg -t 1 -r 1 -y -s $size $image 2>&1";       //ffmpeg command
$return = `$cmd`;
?>

<
Leave a Comment  
Download Code(35)





All rights reserved, the content is copyrighted to the author.

RELATED POST