Please contact (802) 224 - 6525 for price quotes and programming support.
Follow this link for a resume of php and Mysql Programing experience
Digg This!

0 Members and 1 Guest are viewing this topic. « previous next »

Pages: [1] Go Down Print
Author Topic: PHP ffmpeg Upload Script  (Read 9477 times)
John Anderson
Vermont Internet Design Admin
Administrator
Full Member
*****

Karma: 0
Offline Offline

Gender: Male
Posts: 238



View Profile WWW
« on: May 30, 2008, 08:45:46 AM »

Code:
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($input0,$len);
12  
 $ret trim($n);
13  
 $out htmlentities($retENT_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 $seedbasename($_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">&nbsp;</td>
91      <td width="326" align="left">&nbsp;</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>&nbsp;</td>
118     <td><input type="submit" name="Submit" value="Upload Video" /></td>
119   </tr>
120 </table>
121 
122 </form>
Logged

John Anderson
Vermont Internet Design Admin
Administrator
Full Member
*****

Karma: 0
Offline Offline

Gender: Male
Posts: 238



View Profile WWW
« Reply #1 on: June 10, 2008, 10:25:14 AM »

If you would like help with installing ffmpeg or using this script properly feel free to contact me for web development services at chillininvt@yahoo.com
Logged

edge
Guest
« Reply #2 on: December 29, 2008, 08:01:17 AM »

deleted
« Last Edit: January 03, 2009, 08:53:33 AM by edge » Logged
John Anderson
Vermont Internet Design Admin
Administrator
Full Member
*****

Karma: 0
Offline Offline

Gender: Male
Posts: 238



View Profile WWW
« Reply #3 on: December 30, 2008, 02:00:06 PM »

That is kind of a basic example. Are you trying to grab a 3 different frames of the video as three different thumbs? Or are you trying to grab a smaller video out of your upload file. You can use ffmpeg to do any of those operations. Please post your code and I will prolly be more capable of helping you.
Logged

John Anderson
Vermont Internet Design Admin
Administrator
Full Member
*****

Karma: 0
Offline Offline

Gender: Male
Posts: 238



View Profile WWW
« Reply #4 on: December 30, 2008, 02:04:46 PM »

Some of your codecs may not run with a standard ffmpeg command as used in this example. If you go to this page http://www.vermontinternetdesign.com/index.php?topic=525.0
 It there is a post I put on there that shows you how to run ffmpeg command with sudo. Some codecs may not be installed or may not be able to function properly through php. Using sudo it will allow you to run the ffmpeg command as the root user and those commands that only work on the command line will work. I dont know how it will work on a server without sudo. ffmpeg uses graphics resources on your server that only the root user has access to.
Logged

edge
Guest
« Reply #5 on: December 30, 2008, 08:38:07 PM »

deleted
« Last Edit: January 03, 2009, 08:53:23 AM by edge » Logged
John Anderson
Vermont Internet Design Admin
Administrator
Full Member
*****

Karma: 0
Offline Offline

Gender: Male
Posts: 238



View Profile WWW
« Reply #6 on: December 31, 2008, 01:27:43 PM »

I am still unclear .. You are trying to have it output 3 seperate images.
Code:
1 exec('ffmpeg  -i '.$uploadfile.' -vcodec mjpeg -ss 16  -vframes 1 -f image2 -qscale 7 -s 190x120 -an '.$new_image_path.'');

whatever is assigned to $new_image_path is going to be where the new frame is stored. So after the above line is executed on the next 2 commands its going to cause a problem. It will rewrite the next 2 videos. If you dont change the value of $new_image_path before the next call to the function it will overwrite your previous output. However you can get ffmpeg to successively output a string of images from inside your movie. You can also try setting out a counter so each time an image is out it change the filename and not overwrite the last image you created.

 I guess what I would do is append a number to each of the commands like this.

Code:
1 exec('ffmpeg  -i '.$uploadfile.' -vcodec mjpeg -ss 00:00:01.100  -vframes 1 -f image2 -qscale 7 -s 190x120 -an img1_'.$new_image_path.'');
2 exec('ffmpeg  -i '.$uploadfile.' -vcodec mjpeg -ss 00:02:05.440  -vframes 1 -f image2 -qscale 7 -s 190x120 -an img2_'.$new_image_path.'');
3 exec('ffmpeg  -i '.$uploadfile.' -vcodec mjpeg -ss -ss 00:09:13.500  -vframes 1 -f image2 -qscale 7 -s 190x120 -an img3_'.$new_image_path.'');

However when doing that you may also want to create a piece of code that will take the length of the video as the input than calcuate the the halfway point the 2/3 point ect. Because if people are outputing videos of variable lengths they will be taking the frames from different points in the video. so the 00:00:07.500 would also need to be dynamic because they specify where in the video that the frames are going to be comming from. If you want to take the first frame the middle frame and the last frame.  You will need to make a script that will specify in time format where in the video those frames are going to be comming from. You can aslo use something like this.

Code:
1 ffmpeg -y -i '.$upload_file.' -ss 00:00:20 -vframes 3 -an imagename_%d.jpg

Where ffmpeg will automatically generate the image names starting at a specific frame for a specified amount of frames.

Logged

edge
Guest
« Reply #7 on: January 01, 2009, 09:16:56 AM »

deleted
« Last Edit: January 03, 2009, 08:53:46 AM by edge » Logged
John Anderson
Vermont Internet Design Admin
Administrator
Full Member
*****

Karma: 0
Offline Offline

Gender: Male
Posts: 238



View Profile WWW
« Reply #8 on: January 01, 2009, 09:08:02 PM »

  Prolly has something to do with your ffmpeg installation if you cannot get it to output multiple images. Your ffmpeg is prolly maxing out your memory limits or upload size restrictions. Sorry if I am not of much help I have been busy building a flash xml ajax based application so people can design custom items. I would be more than willing to let you hire me and I will write out a script that will solve the problem. I think you almost got it your pretty close. ffmpeg is very system independent without knowing exactly how your running ffmpeg it may cause an error. The syntax may not be 100% correct being that I didnt actually test it I just offered an example. your could also try running your command streaming proceedure. Thats basically how I run ffmpeg it would take me all night to offer n example but its the best way to use it. Actually the second example will not need to output to the php script. You will need to set up a variable that you know the name. imagename_1.jpg imagename_2.jpg imagename_3.jpg as long as you know the image name and the place you are putting it you can reference the image like that. All you need to do is test that the command was successfuly exectued.
if(exec("command here")){
  echo'Successful';
}else{
  echo'failed';
}
the function also returns data like this.

exec("ffmpeg command here", $return);
print_r($return);


  I have built several websites that utilizes ffmpeg and its a very delicate program that behaves different on every server that you put it on. Depending on the type of movie that you are trying to take the frames from you may also not have the proper codec needed to take the video out.
Logged

edge
Guest
« Reply #9 on: January 02, 2009, 12:32:37 AM »

deleted
« Last Edit: January 03, 2009, 08:53:57 AM by edge » Logged
John Anderson
Vermont Internet Design Admin
Administrator
Full Member
*****

Karma: 0
Offline Offline

Gender: Male
Posts: 238



View Profile WWW
« Reply #10 on: January 03, 2009, 06:32:07 AM »

If you think that your ffmpeg is installed by using an yum install ffmpeg or apt-get ffmpeg than your wrong ffmpeg is not installed in like like that its installed with ./configure && make && make install with various switches in the configure command. You prolly don't have your symbolic links correctly addressing the correct locations.
Logged

Pages: [1] Go Up Print 
« previous next »
Jump to:  

Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Christmas2006 design by Bloc
Page created in 0.601 seconds with 32 queries.