013dab398efb9173300bdb5eeeae7dd5750d20ca
[living-lab-site.git] / application / helpers / av_info_helper.php
1 <?php
2
3 /*
4  * These functions retrieve information about audio and video files.
5  * Depends on MediaInfo CLI program (http://mediainfo.sourceforge.net/)
6  * 
7  * @author Călin-Andrei Burloiu
8  */
9
10 /**
11  *
12  * @param string $params mediainfo parameters, including the input file name
13  * passed as in shell
14  * @return string mediainfo standard output
15  */
16 function exec_mediainfo($params)
17 {
18         // If file does not exist it exists with code 1. If file is not a valid
19         // audio/video file it exists with code 0 and outputs nothing.
20         $h = popen('mediainfo ' . $params . ' 2>&1', 'r');
21
22         $r = fgets($h, 512);
23         $r = trim($r);
24         
25         if (pclose($h) > 0 || empty($r))
26                 return FALSE;
27         
28         return $r;
29 }
30
31 /**
32  * Returns duration in hours, minutes and seconds for an audio/video file.
33  *
34  * @param string $file_name 
35  * @return array an associative array with keys 'h', 'min', 's'
36  */
37 function get_av_duration($file_name)
38 {
39         $output = exec_mediainfo(
40                         '--Inform="General;%Duration/String3%" "'. $file_name. '"');
41         
42         if (!$output)
43                 return FALSE;
44         
45         $toks = explode(':', $output);
46         $res['h'] = intval($toks[0]);
47         $res['min'] = intval($toks[1]);
48         $res['s'] = floatval($toks[2]);
49         
50         return $res;
51 }
52
53 /**
54  * Returns video width size in pixels.
55  * 
56  * @param string $file_name
57  * @return int
58  */
59 function get_video_width($file_name)
60 {
61         $output = exec_mediainfo(
62                         '--Inform="Video;%Width%" "'. $file_name. '"');
63         
64         if (!$output)
65                 return FALSE;
66         
67         return intval($output);
68 }
69
70 /**
71  * Returns video height size in pixels.
72  * 
73  * @param string $file_name
74  * @return int
75  */
76 function get_video_height($file_name)
77 {
78         $output = exec_mediainfo(
79                         '--Inform="Video;%Height%" "'. $file_name. '"');
80         
81         if (!$output)
82                 return FALSE;
83         
84         return intval($output);
85 }
86
87 /**
88  * Returns Display Aspect Ration (DAR) of a video.
89  * 
90  * @param string $file_name
91  * @return string a ratio represented a two integers separated by a colon
92  */
93 function get_video_dar($file_name)
94 {
95         $output = exec_mediainfo(
96                         '--Inform="Video;%DisplayAspectRatio/String%" "'. $file_name. '"');
97         
98         if (!$output)
99                 return FALSE;
100         
101         return $output;
102 }
103
104 /* End of file av_info_helper.php */
105 /* Location: ./application/helpers/av_info_helper.php */