1 <?php
2 /*
3 Video Upload and thumbnail generator with ffmpeg. This code is written by John Anderson of Vermont Internet Design. For support please contact chillininvt@yahoo.com. I provide support at the rate of $50 per hr.
4
5 */
6
7 // size input prevents buffer overrun exploits.
8 function sizeinput($input, $len){
9 (int)$len;
10 (string)$input;
11 $n = substr($input, 0,$len);
12 $ret = trim($n);
13 $out = htmlentities($ret, ENT_QUOTES);
14 return $out;
15 }
16 //Check the file is of correct format.
17 function checkfile($input){
18 $ext = array('mpg', 'wma', 'mov', 'flv', 'mp4', 'avi', 'qt', 'wmv', 'rm');
19 $extfile = substr($input['name'],-4);
20 $extfile = explode('.',$extfile);
21 $good = array();
22 $extfile = $extfile[1];
23 if(in_array($extfile, $ext)){
24 $good['safe'] = true;
25 $good['ext'] = $extfile;
26 }else{
27 $good['safe'] = false;
28 }
29 return $good;
30 }
31 $user_id = $_SESSION['table_id'];
32 // if the form was submitted process request if there is a file for uploading
33 if($_POST && array_key_exists("vid_file", $_FILES)){
34 //$uploaddir is for videos before conversion
35 $uploaddir = 'uploads/videos/';
36 //$live_dir is for videos after converted to flv
37 $live_dir = 'uploads/live/';
38 //$live_img is for the first frame thumbs.
39 $live_img = 'uploads/images/';
40 $seed = rand(1,2009) * rand(1,10);
41 $upload = $seed. basename($_FILES['vid_file']['name']);
42 $uploadfile = $uploaddir .$upload;
43 $vid_title = sizeinput($_POST['vid_title'], 50);
44 $vid_desc = sizeinput($_POST['vid_description'], 200);
45 $vid_cat = (int)$_POST['vid_cat'];
46 $vid_usr_ip = $_SERVER['REMOTE_ADDR'];
47 $safe_file = checkfile($_FILES['vid_file']);
48 if($safe_file['safe'] == 1){
49 if (move_uploaded_file($_FILES['vid_file']['tmp_name'], $uploadfile)) {
50 echo "File is valid, and was successfully uploaded.<br/>";
51 $base = basename($uploadfile, $safe_file['ext']);
52 $new_file = $base.'flv';
53 $new_image = $base.'jpg';
54 $new_image_path = $live_img.$new_image;
55 $new_flv = $live_dir.$new_file;
56 //ececute ffmpeg generate flv
57 exec('ffmpeg -i '.$uploadfile.' -f flv -s 320x240 '.$new_flv.'');
58 //execute ffmpeg and create thumb
59 exec('ffmpeg -i '.$uploadfile.' -f mjpeg -vframes 1 -s 150x150 -an '.$new_image_path.'');
60 echo 'Thank You For Your Video!<br>';
61 //create query to store video
62 $sql = 'INSERT INTO videos (vid_cat_id, vid_user, vid_title, vid_desc, vid_file_name, image_file, vid_usr_ip) VALUES(\''.$vid_cat.'\', \''.$user_id.'\', \''.$vid_title.'\', \''.$vid_desc.'\', \''.$new_file.'\', \''.$new_image.'\', \''.$vid_usr_ip.'\')';
63 $mysql = new mysqli('localhost', 'user', 'Pass&d', 'databasename');
64 echo '<img src="'.$new_image_path.'" /><br/>
65 <h3>'.$vid_title.'</h3>';
66 mysqli_query($mysql, $sql) or die(mysqli_error($mysql));
67 } else {
68 echo "Possible file upload attack!\n";
69 print_r($_FILES);
70 }
71
72 }else{
73
74 echo 'Invalid File Type Please Try Again. You file must be of type
75 .mpg, .wma, .mov, .flv, .mp4, .avi, .qt, .wmv, .rm';
76
77 }
78 }
79 ?>
80 <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
81 <p align="left">Please upload Your video. Thumbnails of your videos are based on the first frame of your video. <br /><h3>Please allow up to a minute for your video to upload. </h3>
82 </p>
83 <table width="600" border="0" align="center" cellpadding="2" cellspacing="2">
84 <tr>
85 <td width="260" align="left" colspan="3"><div align="center">
86 <h3>Upload your Video ! </h3>
87 </div></td>
88 </tr>
89 <tr>
90 <td width="260" align="left"> </td>
91 <td width="326" align="left"> </td>
92 </tr>
93 <tr>
94 <td align="left">Title Of Video : </td>
95 <td align="left"><input name="vid_title" type="text" id="vid_title" /></td>
96 </tr>
97 <tr>
98 <td align="left">File: .mov, .avi, .wma , .mpeg : </td>
99 <td align="left"><input name="vid_file" type="file" id="vid_file" /></td>
100 </tr>
101 <tr>
102 <td align="left">Description:</td>
103 <td align="left"><textarea name="vid_description" id="vid_description"></textarea></td>
104 </tr>
105 <tr>
106 <td align="left">Category:</td>
107 <td align="left"><select name="vid_cat">
108 <option value="1" selected="selected">Video</option>
109 <option value="2">Cat1</option>
110 <option value="3">Cat2</option>
111 <option value="4">Cat3</option>
112 <option value="5">Cat4</option>
113 </select>
114 </td>
115 </tr>
116 <tr>
117 <td> </td>
118 <td><input type="submit" name="Submit" value="Upload Video" /></td>
119 </tr>
120 </table>
121
122 </form>