A/V info successfuly retrieves uploaded video information
authorCălin-Andrei Burloiu <calin.burloiu@gmail.com>
Thu, 16 Feb 2012 08:39:38 +0000 (10:39 +0200)
committerCălin-Andrei Burloiu <calin.burloiu@gmail.com>
Thu, 16 Feb 2012 08:39:38 +0000 (10:39 +0200)
application/controllers/video.php
application/helpers/video_helper.php

index 366b92f..6a5c19e 100644 (file)
@@ -23,9 +23,12 @@ class Video extends CI_Controller {
                //phpinfo();
        }
        
-       public function test()
+       public function test($fn)
        {
-               var_dump($this->session->userdata('user_id'));
+               $fn = "data/upload/$fn";
+               $this->load->helper('video');
+               
+               var_dump(get_av_info($fn));
        }
        
        /**
@@ -143,15 +146,13 @@ class Video extends CI_Controller {
                        $this->load->helper('video');
                        $this->config->load('content_ingestion');
                        
-                       $file_name = $this->uploaded_file;
-                       $av_info = get_av_info($file_name);
                        $name = urlencode(str_replace(' ', '-',
                                        $this->input->post('video-title')));
                        $category_id = $this->input->post('video-category');
                        
                        // Prepare formats
                        $formats = $this->config->item('formats');
-                       $prepared_formats = prepare_formats($formats, $av_info,
+                       $prepared_formats = prepare_formats($formats, $this->av_info,
                                        $this->config->item('elim_dupl_res'));
                        
                        // Add video to DB.
@@ -159,14 +160,14 @@ class Video extends CI_Controller {
                                        $this->input->post('video-title'),
                                        $this->input->post('video-description'),
                                        $this->input->post('video-tags'),
-                                       $av_info['duration'],
+                                       $this->av_info['duration'],
                                        $prepared_formats['db_formats'], $category_id, $user_id);
                        
                        // Send a content ingestion request to
                        // CIS (Content Ingestion Server).
                        $this->_send_content_ingestion($activation_code,
-                                       $file_name,
-                                       $name, $av_info['size'],
+                                       $this->uploaded_file,
+                                       $name, $this->av_info['size'],
                                        $prepared_formats['transcode_configs']);
                        
                        $this->load->helper('message');
@@ -361,8 +362,14 @@ class Video extends CI_Controller {
 
                        if ($this->upload->do_upload('video-upload-file'))
                        {
-                               $this->uploaded_file = $this->upload->data();
-                               $this->uploaded_file = $this->uploaded_file['file_name'];
+                               $upload_data = $this->upload->data();
+                               $this->uploaded_file = $upload_data['file_name'];
+                               
+                               $this->load->helper('video');
+                               $this->av_info = get_av_info($upload_data['full_path']);
+                               if (!$this->av_info)
+                                       return FALSE;
+                               
                                return TRUE;
                        }
                        else
index 67ede1a..a172183 100644 (file)
@@ -100,11 +100,50 @@ function get_closest_res($haystack, $needle, $access_function = NULL)
        return $i_min;
 }
 
+/**
+ * "Private" function used by get_av_info which returns the value from a
+ * "key=value" formatted string.
+ * 
+ * @param string $str_key_value a string formatted as key=value
+ * @return string
+ */
+function _parse_value($str_key_value)
+{
+       return trim(substr(
+                       $str_key_value, 
+                       strpos($str_key_value, '=') + 1,
+                       strlen($str_key_value)
+                       ));
+}
+
+/**
+ * Formats the floating number of seconds to a string with format [HH:]mm:ss .
+ * 
+ * @param float $secs
+ * @return string 
+ */
+function format_duration($secs)
+{
+       $secs = intval(round($secs));
+       
+       $h = intval(floor($secs / 3600));
+       $m = intval(floor(($secs % 3600) / 60));
+       $s = $secs % 3600 % 60;
+       
+       $duration = sprintf('%02d', $m) . ':' . sprintf('%02d', $s);
+       
+       if ($h > 0)
+               return sprintf('%02d', $h) . ':' . $duration;
+       
+       return $duration;
+}
+
 /**
  * Returns information about an Audio/Video file.
  * 
  * @param string $file_name Audio/Video file
- * @return dictionary a dictionary of audio/video properties with keys:
+ * @return dictionary FALSE on error or a dictionary of audio/video properties
+ * with the following keys otherwise:
  * <ul>
  *   <li>width</li>
  *   <li>height</li>
@@ -115,10 +154,60 @@ function get_closest_res($haystack, $needle, $access_function = NULL)
  */
 function get_av_info($file_name)
 {
-       // TODO use ffprobe to return width, height, DAR, duration and size of a video
+       $h = popen('ffprobe -show_streams -show_format "'
+                       . $file_name . '" 2> /dev/null', 'r');
+
+       $tag = NULL;
+       
+       while ( ($r = fgets($h, 512)) !== FALSE)
+       {
+               // Match tags.
+               if (preg_match('/^\[FORMAT\]/', $r))
+               {
+                       $tag = 'FORMAT';        
+                       continue;
+               }
+               if (preg_match('/^\[STREAM\]/', $r))
+               {
+                       $tag = 'STREAM';
+                       continue;
+               }
+               
+               if ($tag == 'FORMAT')
+               {
+                       // Duration
+                       if (preg_match('/^duration=/', $r))
+                               $duration = format_duration(floatval(_parse_value($r)));
+                       
+                       // Size
+                       if (preg_match('/^size=/', $r))
+                               $size = intval(_parse_value ($r));
+               }
+               
+               if ($tag == 'STREAM')
+               {
+                       // Width
+                       if (preg_match('/^width=/', $r))
+                               $width = intval(_parse_value($r));
+                       
+                       // Height
+                       if (preg_match('/^height=/', $r))
+                               $height = intval(_parse_value($r));
+                       
+                       // DAR
+                       if (preg_match('/^display_aspect_ratio=/', $r))
+                               $dar = _parse_value($r);
+               }
+       }
+       
+       if (pclose($h) > 0)
+               return FALSE;
+       
+       return array('width'=>$width, 'height'=>$height, 'dar'=>$dar,
+               'duration'=>$duration, 'size'=>$size);
        
-       return array('width'=> 1440, 'height'=> 1080, 'dar'=> '16:9',
-                       'duration'=> '00:10', 'size'=> 5568748);
+//     return array('width'=> 1440, 'height'=> 1080, 'dar'=> '16:9',
+//                     'duration'=> '00:10', 'size'=> 5568748);
 }
 
 /**