-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoHandler.php
More file actions
308 lines (266 loc) · 9.67 KB
/
VideoHandler.php
File metadata and controls
308 lines (266 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
require_once '/includes/connectClass.php';
require_once '/home/pi/vendor/autoload.php';
/**
* @author Rick Stover
* This code will have empty functions, which are meant as suggestions as to what
* a file handler class would be.
*/
class VideoHandler
{
// the ID of the file in the database
private $id;
// directory location of the converted video
private $video_file;
// directory location of the uploaded video
private $input_video_file;
// directory location of the converted gif
private $gif_file;
// directory location of the still frame
private $still_file;
// url of the converted video
private $video_file_url;
// url of the converted gif
private $gif_file_url;
// url of the still frame
private $still_file_url;
// the x resolution of the video
private $width;
// the y resolution of the video
private $height;
// length of the video
private $duration;
// instance of the Connect class that will handle SQL Queries
private $db;
// name of the table storing the file information
private $table;
/**
* The empty constructor should provide default values for the file and
* initialize the ffmpeg and db objects.
*/
public function __construct()
{
$url='HTTP_TYPE'."://".'HTTP_ROOT'.substr(__DIR__, strlen($_SERVER['DOCUMENT_ROOT'])).'/';
$dir=$_SERVER['DOCUMENT_ROOT'];
$this->table="VideoFiles";
$this->id=0;
$this->duration=0;
$this->width=0;
$this->height=0;
$this->gif_file=$dir.DIRECTORY_SEPARATOR."/gif/";
$this->gif_file_url=$url."gif/";
$this->still_file=$dir.DIRECTORY_SEPARATOR."img/";
$this->still_file_url=$url."still/";
$this->video_file=$dir.DIRECTORY_SEPARATOR."output/";
$this->input_video_file=$dir.DIRECTORY_SEPARATOR."input/";
$this->video_file_url=$url."output/";
$this->db=new Connect("tiger","Tigers17");
$this->width=1080;
$this->height=720;
}
/**
* The catchFile method should catch a single file from the $_FILE associative
* array.
* It will then do the following:
* -move the temporary file to the input folder
* -validate the file - only videos here!
* -convert the input video to h.264
* -create the GIF
* -create the still
* -insert new row into database
*
* @param
* string id - the key of the $_FILE assoc array relating to the form
* id of the submitted file. Used in $_FILE[$id]["tmp_name"], etc.
*/
public function catchFile($id)
{
$tmp_name=$_FILES[$id]["tmp_name"];
// file name with extension
$file = $_FILES[$id]["name"];
// name without extension
$file_name = pathinfo($file, PATHINFO_FILENAME);
$tmp = explode('.', $_FILES[$id]["name"]);
$ext = end($tmp);
$date = date("Y-m-d H:i:s");
$uploadtime = strtotime($date);
$this->input_video_file.=$uploadtime.".".$ext;
$this->video_file .= $uploadtime . ".mp4";
$this->gif_file .= $uploadtime . ".gif";
$this->still_file .= $uploadtime . ".jpeg";
if($this->moveTmp($tmp_name)){
if($this->isValid()){
$this->convertVideoFormat();
$this->loadFromFile();
$this->createGIF();
$this->createStill();
$this->insertVideo();
$output="The video was uploaded and converted successfully!";
}
else{
$this->removeVideo($this->input_video_file);
$output="The file was not a video.";
}
}
else{
$output="There was a problem moving the video.";
}
return $output;
}
/**
* isValid uses ffprobe to get codec_type of uploaded video, checks
* the codec type. If it is video, return true. Else, delete the
* file and return false.
*/
public function isValid()
{
$ffprobe = FFMpeg\FFProbe::create();
$codec=$ffprobe->streams($this->input_video_file)->videos()->first();
if($codec!=null)
return true;
return false;
}
/**
* This uses the move_uploaded_file to put the temporary file in the
* input directory for conversion.
*/
public function moveTmp($tmp_name)
{
$moved = move_uploaded_file($tmp_name, $this->input_video_file);
return $moved;
}
/**
* This method will remove the physical copy of the file.
*/
public function removeVideo($filePath){
if ( file_exists(realpath($filePath)) ) {
if( @unlink(realpath($filePath)) !== true )
throw new Exception('Could not delete file: ' . $this->filePath);
}
return true;
}
/**
* convertVideo converts the video from whatever format it was into an mp4
* file using $ffmpeg.
*/
public function convertVideoFormat()
{
$ffmpeg=FFMpeg\FFMpeg::create();
$format = new FFMpeg\Format\Video\X264('aac');
$video = $ffmpeg->open($this->input_video_file);
/* This section of code will be useful when we are trimming and resizing.
* It is unneccessary when we are just changing the file format.
* $clip = $video->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(0),
FFMpeg\Coordinate\TimeCode::fromSeconds($this->duration));
$clip->filters()
->resize(new FFMpeg\Coordinate\Dimension($this->width,$this->height), FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_INSET, true);
$ext="mp4";
$output = "$this->output_dir/$this->video_file";
$clip
->save($format, $output);*/
$video->save($format,$this->video_file);
$this->removeVideo($this->input_video_file);
}
/**
* createGIF should only convert the video into a GIF file using $ffmpeg.
*/
public function createGIF()
{
$ffmpeg=FFMpeg\FFMpeg::create();
$video = $ffmpeg->open($this->video_file);
//This section of code will display debug info
// $ffmpeg->getFFMpegDriver()->listen(new \Alchemy\BinaryDriver\Listeners\DebugListener());
//$ffmpeg->getFFMpegDriver()->on('debug', function ($message) {
// echo $message."\n";
//});
$video
->gif(FFMpeg\Coordinate\TimeCode::fromSeconds(0), new FFMpeg\Coordinate\Dimension(320, 240), 3)
->save($this->gif_file);
}
/**
* createStill should only convert the video into a still using $ffmpeg.
*/
public function createStill()
{
$ffmpeg=FFMpeg\FFMpeg::create();
$video = $ffmpeg->open($this->video_file);
//This section of code will display debug info
// $ffmpeg->getFFMpegDriver()->listen(new \Alchemy\BinaryDriver\Listeners\DebugListener());
//$ffmpeg->getFFMpegDriver()->on('debug', function ($message) {
// echo $message."\n";
//});
$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(0));
$frame->save($this->still_file);
}
/**
* insertVideo should generate an SQL QUery based on the stored file
* information, then insert it into the database using $db.
*/
public function insertVideo()
{
$sql="INSERT INTO ".$this->table.
" (File, Still, Gif, ResX, ResY, Seconds) VALUES".
" ('".$this->video_file."', '".$this->still_file."', '".$this->gif_file."', ". $this->width.
", ".$this->height.", ".$this->duration.")";
$this->db->runQuery($sql);
}
/**
* deleteVideo should generate an SQL Query based on the file ID
* information, then delete it into the database using $db.
*/
/**
public function deleteVideo($id="")
{
$this->deleteVideo($this->id);
}
/**
* loadFromFile will use FFProbe and FFMpeg to load the file information
* into the class variables.
*/
public function loadFromFile(){
$ffprobe = FFMpeg\FFProbe::create();
$video_dimensions = $ffprobe
->streams($this->video_file) // extracts streams informations
->videos() // filters video streams
->first() // returns the first video stream
->getDimensions(); // returns a FFMpeg\Coordinate\Dimension object
$this->width = $video_dimensions->getWidth();
$this->height = $video_dimensions->getHeight();
$this->duration = $ffprobe->format($this->video_file)->get('duration');
}
/**
* loadFromAssoc should load the file information from an associative
* array, like the result for an SQL query.
*
* @param assoc_array $assoc
* is the result of a database query
*/
public function loadFromAssoc($assoc)
{}
/**
* loadFromID should load the file information from an SQL query based on
* the file ID.
* Once the query is run, it should call loadFromAssoc
*/
public function loadFromID()
{}
/**
* displayEditCell should return a table cell that contains a GIF of the file,
* sized appropriately, and links to the edit page for the file
*/
public function displayEditCell()
{}
/**
* displayDeleteCell should return a table cell that contains a GIF of the file,
* sized appropriately, and links to the delete page for the file
*/
public function displayDeleteCell()
{}
/**
* displayCell should display the thumbnail of the video in a table cell, and link to a
* pop-up of the video in a new window.
*/
public function displayCell()
{
}
}