update external plugin UI handling
[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                 default:
20                         break;
21         }
22 }
23
24 $infile='/tmp/test.avi';
25 $w=80; $h=60; $f=0;
26 $fr=0; $df=0;
27 $so=0; $ar=4.0/3.0;
28 $fmt='rgb';
29
30 if (isset($_REQUEST['format'])) {
31         switch ($_REQUEST['format']) {
32         case 'jpeg':
33         case 'jpg':
34                 $fmt='jpeg';
35                 break;
36         case 'png':
37                 $fmt='png';
38                 break;
39         case 'rgba':
40                 $fmt='rgba';
41                 break;
42         case 'rgb':
43                 $fmt='rgb';
44                 break;
45         default:
46                 break;
47         }
48 }
49
50 if (isset($_REQUEST['w']))
51   $w=intval(rawurldecode($_REQUEST['w']));
52 if (isset($_REQUEST['h']))
53   $h=intval(rawurldecode($_REQUEST['h']));
54 if (isset($_REQUEST['frame']))
55   $f=intval(rawurldecode($_REQUEST['frame']));
56 if (isset($_REQUEST['file']))
57   $infile=rawurldecode($_REQUEST['file']);
58
59 if (!is_readable($docroot.$infile)) {
60         header('HTTP/1.0 404 Not Found', true, 404);
61         exit;
62 }
63
64 $fn=escapeshellarg($docroot.$infile);
65
66 #$fr=`$exe_ffprobe $fn 2>&1 | awk '/Video:/{printf "%f\\n", $11}'`;
67 $nfo=shell_exec("$exe_ffprobe $fn 2>&1");
68 if (preg_match('@Video:.* ([0-9.]+) tbr,@m',$nfo, $m))
69         $fr=floatval($m[1]);
70 if ($fr<1) exit;
71
72 if (preg_match('@Duration: ([0-9:.]+),@m',$nfo, $m)) {
73         $d=preg_split('@[\.:]@',$m[1]);
74         $dr=0;
75         $dr+=intval($d[0])*3600;
76         $dr+=intval($d[1])*60;
77         $dr+=intval($d[2]);
78         $dr+=floatval($d[3]) / pow(10,strlen($d[3]));
79         $df=$fr*$dr;
80 }
81 if (preg_match('@start: ([0-9:.]+),@m',$nfo, $m)) {
82         $so=floatval($m[1]);
83 }
84 if (preg_match('@DAR ([0-9:]+)\]@m',$nfo, $m)) {
85         $d=explode(':',$m[1]);
86         $ar=floatval($d[0]/$d[1]);
87 }
88 else if (preg_match('@Video:.* ([0-9]+x[0-9]+),@m',$nfo, $m)) {
89         $d=explode('x',$m[1]);
90         $ar=floatval($d[0]/$d[1]);
91 }
92
93 if ($mode=='info') {
94         # Protocol Version number
95         # FPS
96         # duration (in frames)
97         # start-offset (in seconds)
98         # aspect-ratio
99         echo "1\n$fr\n$df\n$so\n$ar\n";
100         exit;
101 }
102
103 if ($df<1 || $f>$df ) exit;
104 $st=floor(1000.0*$f/$fr)/1000.0;
105
106 $wh=escapeshellarg($w.'x'.$h);
107 $ss=escapeshellarg($st);
108
109 header('Content-Type: image/'.$fmt);
110 passthru($exe_ffmpeg.' -loglevel quiet'
111               .' -i '.$fn.' -s '.$wh.' -ss '.$ss.' -vframes 1 '
112         .'-f image2 -vcodec png - 2>/dev/null'
113         .'| '.$exe_imagemagick.' - '.$fmt.':-');