Exemplo n.º 1
0
 function getVideoInfo($videoFile, $c, $cmdOut = '')
 {
     $ffmpeg_path = $c->ffmpeg_path;
     $data = array();
     if (!is_file($videoFile) && empty($cmdOut)) {
         return $data;
     }
     if (!$cmdOut) {
         //$cmd	= $this->converter . ' -v 10 -i ' . $videoFile . ' 2>&1';
         // Some FFmpeg version only accept -v value from -2 to 2
         $cmd = "{$ffmpeg_path}" . ' -i ' . $videoFile . ' 2>&1';
         $cmdOut = shell_exec($cmd);
     }
     if (!$cmdOut) {
         return $data;
     }
     preg_match_all('/Duration: (.*)/', $cmdOut, $matches);
     if (count($matches) > 0 && isset($matches[1][0])) {
         $parts = explode(', ', trim($matches[1][0]));
         $data['bitrate'] = intval(ltrim($parts[2], 'bitrate: '));
         $data['duration']['hms'] = substr($parts[0], 0, 8);
         $data['duration']['exact'] = $parts[0];
         $data['duration']['sec'] = $videoFrame = JTHelper::cFormatDuration($data['duration']['hms'], 'seconds');
         $data['duration']['excess'] = intval(substr($parts[0], 9));
     } else {
         if ($this->debug) {
             echo '<pre>FFmpeg failed to read video\'s duration</pre>';
             echo '<pre>' . $cmd . '<pre>';
             echo '<pre>' . $cmdOut . '</pre>';
         }
         return false;
     }
     $file_size = filesize($videoFile);
     $duration_in_sec = $file_size * 8 / ($data['bitrate'] * 1000);
     //echo $duration_in_sec;
     $duration = JTHelper::sec2hms($duration_in_sec);
     //echo $duration;
     $data['duration']['hms'] = $duration;
     $data['duration']['exact'] = $duration;
     $data['duration']['sec'] = round($duration_in_sec);
     $data['duration']['excess'] = intval(substr($duration, 9));
     //echo '<pre>';print_r($data);print_r($cmdOut);die();
     preg_match('/Stream(.*): Video: (.*)/', $cmdOut, $matches);
     if (count($matches) > 0 && isset($matches[0]) && isset($matches[2])) {
         $data['video'] = array();
         preg_match('/([0-9]{1,5})x([0-9]{1,5})/', $matches[2], $dimensions_matches);
         $data['video']['width'] = floatval($dimensions_matches[1]);
         $data['video']['height'] = floatval($dimensions_matches[2]);
         preg_match('/([0-9\\.]+) (fps|tb)/', $matches[0], $fps_matches);
         if (isset($fps_matches[1])) {
             $data['video']['frame_rate'] = floatval($fps_matches[1]);
         }
         preg_match('/\\[PAR ([0-9\\:\\.]+) DAR ([0-9\\:\\.]+)\\]/', $matches[0], $ratio_matches);
         if (count($ratio_matches)) {
             $data['video']['pixel_aspect_ratio'] = $ratio_matches[1];
             $data['video']['display_aspect_ratio'] = $ratio_matches[2];
         }
         if (!empty($data['duration']) && !empty($data['video'])) {
             $data['video']['frame_count'] = ceil($data['duration']['sec'] * $data['video']['frame_rate']);
             $data['frames'] = array();
             $data['frames']['total'] = $data['video']['frame_count'];
             $data['frames']['excess'] = ceil($data['video']['frame_rate'] * ($data['duration']['excess'] / 10));
             $data['frames']['exact'] = $data['duration']['hms'] . '.' . $data['frames']['excess'];
         }
         $parts = explode(',', $matches[2]);
         $other_parts = array($dimensions_matches[0], $fps_matches[0]);
         $formats = array();
         foreach ($parts as $key => $part) {
             $part = trim($part);
             if (!in_array($part, $other_parts)) {
                 array_push($formats, $part);
             }
         }
         $data['video']['pixel_format'] = $formats[1];
         $data['video']['codec'] = $formats[0];
     }
     return $data;
 }