-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencoder.php
More file actions
267 lines (248 loc) · 8.4 KB
/
Copy pathencoder.php
File metadata and controls
267 lines (248 loc) · 8.4 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
<?php
// advance media processing
// used to published multi format, multi level video and audio processing, settting meta information, grab thumbs, complex validation in one step.
require_once "mediahandler.php";
require_once "videoinfo.php";
require_once "templates.php";
class mhpEncoder {
public $rootPath = ""; // used for other services
public $servicePath = "";
public $sourcePath;
public $sourceFileName;
public $publishPath;
public $publishFileName;
public $thumbsDirectory;
// other options
public $postWatermark = false;
public $watermarkPath = "watermark.png"; // watermark.png must be located in folder where actual .php file exist (php file which call posting watermark command)
public $watermarkLocation = 0; // 0: top left, 1: top right, 2: bottom left, 3: bottom right
public $grabThumbs = true;
public $publishVideos = true; // if disabled, script will grab thumbs only
public $isMultipleThumbs = true; // grab single or multiple thumbs
public $totalThumbs = 15;
public $allowRollback = true; // remove all published contents in case of error occurs in any stage
public $deleteSource = false; // delete source video if publishing completed
public $itags = array();
// private properties
private $publishedFiles = array();
private $mp4boxPath;
private $flvtoolPath;
function __construct()
{
$this->mp4boxPath = $this->rootPath . "mp4box/MP4Box.exe"; // adjust with linux path
$this->flvtoolPath = $this->rootPath . "flvtool/flvtool2.exe"; // adjust in case of linux
}
public function Process()
{
$mhandler = new MediaHandler();
$settings = new MediaTemplates();
$info = new videoInfo();
$dinfo = new videoInfo(); // store first
$outputFileName = "";
$thumbFileName = "";
try
{
$fName = $this->sourceFileName;
if($this->publishFileName != "")
$fName = $this->publishFileName;
$outputFileName = substr($fName, 0, $mhandler->lastIndexOf($fName, "."));
if($this->publishVideos)
{
$countTags = count($this->itags);
if(count($countTags) == 0)
{
$info->errorcode = 203;
$info->errorMessage = "Please select atleast one output";
$this->rollbackProcess();
return $info;
}
$output = "";
for ($i = 0; $i <= $countTags - 1; $i++)
{
// reset media handler settings
$mhandler->servicePath = $this->servicePath;
$mhandler->inputPath = $this->sourcePath;
$mhandler->fileName = $this->sourceFileName;
$mhandler->outputPath = $this->publishPath;
$extension = $settings->returnExtension($this->itags[$i]);
$mhandler->outputfileName = $outputFileName . "_" . $this->itags[$i] . "" . $extension;
// load settings from template
$mhandler->parameters = implode(' ', $settings->returnSettings($this->itags[$i]));
if($this->postWatermark)
{
switch($this->watermarkLocation)
{
case 0: // top left
$mhandler->parameters .= " -vf \"movie = " . $this->watermarkPath . " [watermark]; [in][watermark] overlay =10:10 [out]\"";
break;
case 1: // top right
$mhandler->parameters .= " -vf \"movie = " . $watermarkPath . " [watermark]; [in][watermark] overlay=main_w-overlay_w-10:10 [out]\"";
break;
case 2: // botto left
$mhandler->parameters .= " -vf \"movie = " . $watermarkPath . " [watermark]; [in][watermark] overlay=10:main_h-overlay_h-10 [out]\"";
break;
case 3: // bottom right
$mhandler->parameters .= " -vf \"movie = " . $watermarkPath . " [watermark]; [in][watermark] overlay=main_w-overlay_w-10:main_h-overlay_h-10 [out]\"";
break;
}
}
$mhandler->parameters .= " -y"; // owerwrite
//$output .= "<br />" . $mhandler->parameters . "<br />" . $mhandler->outputfileName;
$info = $mhandler->Process();
if($info->errorcode > 0)
{
// rollback process
$this->rollbackProcess();
$info->errorMessage = $info->errorMessage . " - iTag: " . $this->itags[$i];
return $info;
}
// Validation of published file
$oFile = $mhandler->outputPath . '/' . $mhandler->outputfileName;
if(!file_exists($oFile))
{
$info->errorcode = 201;
$info->errorMessage = "Published file not found! - iTag: " . $this->itags[$i];
$this->rollbackProcess();
return $info;
}
if(filesize($oFile) < 10)
{
$info->errorcode = 202;
$info->errorMessage = "Published file corrupted - 0bytes - iTag: " . $this->itags[$i];
$this->rollbackProcess();
return $info;
}
// set meta information
$output = "no";
if($extension == ".mp4")
{
$vinfo = $this->setMP4Meta($mhandler->outputfileName);
if($vinfo->errorcode > 0)
{
$this->rollbackProcess();
return $vinfo;
}
}
else if($extension == ".flv")
{
$output = $this->setFLVMeta($mhandler->outputfileName);
/*$vinfo = $this->setFLVMeta($mhandler->outputfileName);
if($vinfo->errorcode > 0)
{
$this->rollbackProcess();
return $vinfo;
} */
}
$publishedFiles[] = $oFile;
if ($i == 0)
$dinfo = $info;
}
$info->errorcode = 555;
$info->errorMessage = $output;
}
// grab thumbs
if($this->grabThumbs)
{
$ghandler = new MediaHandler();
$mhandler->servicePath = $this->servicePath;
$mhandler->inputPath = $this->sourcePath;
$mhandler->fileName = $this->sourceFileName;
$mhandler->outputPath = $this->thumbsDirectory;
$mhandler->parameters = "-f image2 -s 120x100"; // normal options for grabbing thumbs
$mhandler->imageFormat = ".jpg";
if($this->isMultipleThumbs)
{
// multiple thumb grab option choosen
$mhandler->isMultiple = true;
$mhandler->totalThumbs = $this->totalThumbs;
$mhandler->startPosition = 3;
}
else
{
// single thumb
$mhandler->isMultiple = false;
// calculate video mid position
$startposition = 0;
$vinfo = $mhandler->getInfo(false);
if($vinfo->duration_sec >0)
$startposition = (int)($vinfo->duration_sec/2);
else
$startposition = 5;
$mhandler->startPosition = $startposition;
}
$ginfo = $mhandler->grabThumbs();
if($ginfo->errorcode >0)
{
$this->rollbackProcess();
$ginfo->errorMessage = "Thumbs failed to grab properly";
return $ginfo;
}
}
// remove original file
if($this->deleteSource)
{
$sFile = $this->sourcePath . '/' . $this->sourceFileName;
if(file_exists($sFile))
unlink($sFile);
}
}
catch (Exception $e)
{
$info->errorcode = 200; // uncatch
$info->errorMessage = $e->getMessage();
}
return $info;
}
function setFLVMeta($fileName)
{
$mhandler = new MediaHandler();
$mhandler->servicePath = $this->flvtoolPath;
$mhandler->fileName = $fileName;
$mhandler->command = '-U ' .escapeshellarg($this->publishPath . "" . $mhandler->fileName);
$info = $mhandler->setMeta();
return $info->errorcode . "<br />" . $info->ffmpegOutput . "<br />" . $mhandler->command;
}
function setMP4Meta($fileName)
{
$mhandler = new MediaHandler();
$mhandler->servicePath = $this->mp4boxPath;
$mhandler->fileName = $fileName;
$mhandler->command = '-isma -hint ' .escapeshellarg($this->publishPath . "" . $mhandler->fileName);
return $mhandler->setMeta();
}
// rollback all successuful stats in case of errors
function rollbackProcess()
{
if(!$this->allowRollback)
return;
// remove all published files
if(count($publishedFiles) > 0)
{
foreach($publishedFiles as $itm)
{
if(file_exists($itm))
unlink($itm);
}
}
// remove all thumbs if grabbed
$fName = $this->sourceFileName;
if($this->publishFileName != "")
$fName = $this->publishFileName;
$counter = 1;
$thumb_index = "";
for ($i = 0; $i <= $this->totalThumbs - 1; $i++)
{
if ($counter < 10)
$thumb_index = "00" . $counter;
else if ($counter >= 10 && $counter < 100)
$thumb_index = "0" . $counter;
else
$thumb_index = $counter;
}
$outputFileName = substr($fName, 0, $mhandler->lastIndexOf($fName, "."));
$oFile = $this->thumbsDirectory . "" . $outputFileName . "" . $thumb_index . ".jpg";
if(file_exists($oFile))
unlink($oFile);
}
}
?>