-
Notifications
You must be signed in to change notification settings - Fork 12
/
init.inc.php
128 lines (108 loc) · 4.12 KB
/
init.inc.php
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
<?php
/*
PHP Live Transcode -- HTML5 and Flash streaming with live transcoding.
Copyright (C) 2010 Matthias -apoc- Hecker <http://apoc.cc>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* Please read
http://sixserv.org/2010/11/30/live-transcoding-for-video-and-audio-streaming/
for more information. */
require 'config.inc.php';
/* just for internal use */
define('P_STDIN', 0);
define('P_STDOUT', 1);
define('P_STDERR', 2);
/* initialize smarty template library */
require SMARTY_LIBRARY;
$smarty = new Smarty();
$smarty->setTemplateDir(SMARTY_TEMPLATE);
$smarty->setCompileDir(SMARTY_COMPILE);
/* define some modifiers for bitrate conversion */
function kbyte($bits){
return round($bits/8/1024,2);
}
function kbit($bits){
return round($bits/1000,2);
}
$smarty->register_modifier("kbyte", "kbyte");
$smarty->register_modifier("kbit", "kbit");
/* just a very simple debug logger */
function dbg($message)
{
file_put_contents(DEBUG_LOG, time().": $message\n", FILE_APPEND);
}
/* test for ffmpeg and mplayer */
if(!preg_match('/MPlayer (\S+)/',shell_exec(MPLAYER),$mplayer_version)){
$smarty->assign('error', 'MPlayer not found. ('.MPLAYER.')');
$smarty->display('error.tpl.html');
exit;
}
$smarty->assign('mplayer_version', $mplayer_version[1]);
if(!preg_match('/FFmpeg (\S+)/',shell_exec(FFMPEG.' -version'),$ffmpeg_version)){
$smarty->assign('error', 'FFmpeg not found. ('.FFMPEG.')');
$smarty->display('error.tpl.html');
exit;
}
$smarty->assign('ffmpeg_version', $ffmpeg_version[1]);
/* check running ffmpeg instances */
$instances = `ps -A | grep ffmpeg | wc -l`;
$instances = (int)(trim($instances));
if ($instances > FFMPEG_MAX_INSTANCES){
$smarty->assign('error', 'There too many running ffmpeg instances. ('.$instances.'/'.FFMPEG_MAX_INSTANCES.')');
$smarty->display('error.tpl.html');
exit;
}
/* base path/url for js resources */
$smarty->assign('base_url', BASE_URL);
/* gather mediafile from path */
if(empty($_SERVER["PATH_INFO"])){
$smarty->assign('error', 'You must provide a mediafile.');
$smarty->display('error.tpl.html');
exit;
}
$mediafile = MEDIA_PATH . '/' . basename($_SERVER["PATH_INFO"]);
if(strstr($mediafile, '..') !== false){ /* directory traversal */
$smarty->assign('error', 'Illegal characters.');
$smarty->display('error.tpl.html');
exit;
}
$mediafilename = basename($mediafile);
preg_match('/\.([^\.]+)$/', $mediafilename, $match);
$mediaext = $match[1];
if(!file_exists($mediafile)){
$orig_mediafile = $mediafile;
/* look for existing mediafile without last extension */
$mediafile = preg_replace('/\.[^\.]+$/','',$mediafile);
if(!file_exists($mediafile)){
$smarty->assign('error', 'Mediafile does not exist. ('.htmlentities($orig_mediafile).')');
$smarty->display('error.tpl.html');
exit;
} /* continue in flow */
}
/* identify the mediafile with mplayer */
require 'classlib/MPlayerIdentify.class.php';
$mediainfo = new MPlayerIdentify($mediafile);
if(empty($mediainfo->audio_codec)){
$smarty->assign('error', 'MPlayer has not identified the file you provided. ('.htmlentities($mediafile).')');
$smarty->display('error.tpl.html');
exit;
}
/* assign mediafile information */
$smarty->assign('filename', basename($mediafile));
$smarty->assign('path', dirname($mediafile));
$smarty->assign('size', round(filesize($mediafile) / 1024 / 1024, 2));
$smarty->assign('mediainfo', $mediainfo);
if(empty($mediainfo->video_codec))
$mediatype = 'audio';
else
$mediatype = 'video';
$smarty->assign('mediatype', $mediatype);