LuaSession: allow multi-line commands and functions
[ardour.git] / tools / videotimeline / vsrv.php
1 <?php
2 $exe_ffmpeg='ffmpeg';
3 $exe_ffprobe='ffprobe';
4 $exe_imagemagick='convert';
5 $docroot='/'; # must be identical to ardour3->Edit->Preferences->Video->Docroot
6
7 $mode = '';
8 if (isset($_SERVER['PATH_INFO'])) {
9         switch($_SERVER['PATH_INFO']) {
10                 case '/status/':
11                 case '/status':
12                         echo 'status: ok, online.';
13                         exit;
14                         break;
15                 case '/info/':
16                 case '/info':
17                         $mode='info';
18                         break;
19                 case '/rc':
20                 case '/rc/':
21                         # TODO proper CSV encode (possible quotes in docroot)
22                         # TODO support optional plain text version
23                         echo '"'.$docroot.'",'.$_SERVER['SERVER_ADDR'].','.$_SERVER['SERVER_PORT'].',0,"/info /rc /status",""'."\n";
24                         echo 'status: ok, online.';
25                         exit;
26                         break;
27                 default:
28                         break;
29         }
30 }
31
32 $infile='/tmp/test.avi';
33 $w=80; $h=60; $f=0;
34 $fr=0; $df=0;
35 $so=0; $ar=4.0/3.0;
36 $fmt='rgb';
37
38 if (isset($_REQUEST['format'])) {
39         switch ($_REQUEST['format']) {
40         case 'jpeg':
41         case 'jpg':
42                 $fmt='jpeg';
43                 break;
44         case 'png':
45                 $fmt='png';
46                 break;
47         case 'rgba':
48                 $fmt='rgba';
49                 break;
50         case 'bgra':
51                 $fmt='bgra';
52                 break;
53         case 'rgb':
54                 $fmt='rgb';
55                 break;
56         default:
57                 break;
58         }
59 }
60
61 if (isset($_REQUEST['w']))
62   $w=intval(rawurldecode($_REQUEST['w']));
63 if (isset($_REQUEST['h']))
64   $h=intval(rawurldecode($_REQUEST['h']));
65 if (isset($_REQUEST['frame']))
66   $f=intval(rawurldecode($_REQUEST['frame']));
67 if (isset($_REQUEST['file']))
68   $infile=rawurldecode($_REQUEST['file']);
69
70 if (!is_readable($docroot.$infile)) {
71         header('HTTP/1.0 404 Not Found', true, 404);
72         exit;
73 }
74
75 $fn=escapeshellarg($docroot.$infile);
76
77 #$fr=`$exe_ffprobe $fn 2>&1 | awk '/Video:/{printf "%f\\n", $11}'`;
78 $nfo=shell_exec("$exe_ffprobe $fn 2>&1");
79 if (preg_match('@Video:.* ([0-9.]+) tbr,@m',$nfo, $m))
80         $fr=floatval($m[1]);
81 if ($fr<1) exit;
82
83 if (preg_match('@Duration: ([0-9:.]+),@m',$nfo, $m)) {
84         $d=preg_split('@[\.:]@',$m[1]);
85         $dr=0;
86         $dr+=intval($d[0])*3600;
87         $dr+=intval($d[1])*60;
88         $dr+=intval($d[2]);
89         $dr+=floatval($d[3]) / pow(10,strlen($d[3]));
90         $df=$fr*$dr;
91 }
92 if (preg_match('@start: ([0-9:.]+),@m',$nfo, $m)) {
93         $so=floatval($m[1]);
94 }
95 if (preg_match('@DAR ([0-9:]+)\]@m',$nfo, $m)) {
96         $d=explode(':',$m[1]);
97         $ar=floatval($d[0]/$d[1]);
98 }
99 else if (preg_match('@Video:.* ([0-9]+x[0-9]+),@m',$nfo, $m)) {
100         $d=explode('x',$m[1]);
101         $ar=floatval($d[0]/$d[1]);
102 }
103
104 if ($mode=='info') {
105         if (isset($_REQUEST['format'])) {
106                 switch ($_REQUEST['format']) {
107                 case 'csv':
108                         # protocol, width, height, aspect, fps, fps, duration
109                         echo "1,0,0,$ar,$fr,$df\n";
110                         exit;
111                         break;
112                 default:
113                         break;
114                 }
115         }
116         # Protocol Version number
117         # FPS
118         # duration (in frames)
119         # start-offset (in seconds)
120         # aspect-ratio
121         echo "1\n$fr\n$df\n$so\n$ar\n";
122         exit;
123 }
124
125 if ($df<1 || $f>$df ) exit;
126 $st=floor(1000.0*$f/$fr)/1000.0;
127
128 $wh=escapeshellarg($w.'x'.$h);
129 $ss=escapeshellarg($st);
130
131 header('Content-Type: image/'.$fmt);
132 passthru($exe_ffmpeg.' -loglevel quiet'
133               .' -i '.$fn.' -s '.$wh.' -ss '.$ss.' -vframes 1 '
134         .'-f image2 -vcodec png - 2>/dev/null'
135         .'| '.$exe_imagemagick.' - '.$fmt.':-');