Index: ffmpeg_wrapper.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ffmpeg_wrapper/ffmpeg_wrapper.module,v
retrieving revision 1.1.2.20.2.27
diff -u -p -r1.1.2.20.2.27 ffmpeg_wrapper.module
--- ffmpeg_wrapper.module	20 Apr 2009 21:18:45 -0000	1.1.2.20.2.27
+++ ffmpeg_wrapper.module	3 Jun 2009 12:24:13 -0000
@@ -328,24 +328,55 @@ function ffmpeg_wrapper_vhook_list($path
 
 
 /**
- * Check an incoming file path extension to see if it can be decoded.
+ * Check an incoming file to see if it can be decoded by comparing the file's codec
+ * and format against the list of decode formats in FFMPEG.
  *
  * @param $file
- *   A full system filepath.
+ *   string, A full system filepath.
+ * @param $types
+ *   array, what kind of decode do we need to check for
  * @return
- *   TRUE if file is in the list of decodeable files.
+ *   boolean, TRUE if file is in the list of decodeable files.
  */
-function ffmpeg_wrapper_can_decode($path) {
+function ffmpeg_wrapper_can_decode($path, $types = array('video', 'audio')) {
   // get the kind of files
-  $file_types = ffmpeg_wrapper_get_codecs('decode');
+  $file_types = ffmpeg_wrapper_get_file_formats('decode');
 
-  // get the data on this file
+  // get the format and codec information on this file
   $file_data = ffmpeg_wrapper_file_data($path);
 
-  // some files have multiple formats attached to them
-  foreach(explode(',', $file_data['video']['codec']) as $type) {
-    // is this type in the array of items that we can decode?
-    if (in_array($type, $file_types)) {
+   // if the file has multiple formats, all formats
+   // must be in $file_types Otherwise, it is
+   // not supported by ffmpeg.
+  foreach(explode(',', $file_data['format']) as $type) {
+    if (! in_array($type, $file_types)) {
+      return false;
+    }
+  }
+
+  // if we have na values for the audio and video codecs, the file can not be decoded
+  // @ TODO is this really true? The logic below does not seem to indicate this,
+  //        however, I might not understand what the 'na' value really means
+  if ($file_data['audio']['codec'] == 'na' && $file_data['video']['codec'] == 'na') {
+    return false;
+  }
+
+  // Now get installed codecs to compare file codecs
+  foreach (ffmpeg_wrapper_get_codecs('decode') as $codec) {
+    if ($codec == $file_data['audio']['codec'] || $file_data['audio']['codec'] == 'na' ) {
+       // are we only handing back audio?
+      if ($types['audio'] && ! $types['video']) { return true; }
+      $can_audio_decode = true;
+    }
+
+    if ($codec == $file_data['video']['codec'] || $file_data['video']['codec'] == 'na') {
+      // are we only handing back video?
+      if ($types['video'] && ! $types['audio']) { return true; }
+      $can_video_decode = true;
+    }
+
+    // If we can decode both audio and video, the file is ok
+    if ($can_audio_decode && $can_video_decode) {
       return true;
     }
   }
@@ -551,7 +582,7 @@ function ffmpeg_wrapper_get_file_formats
   $cache_id = 'ffmpeg_wrapper_file_formats';
   $cache = cache_get($cache_id, 'cache');
   // do we have cached data?
-  if (! isset($cache->data)) {
+  if (!isset($cache->data)) {
     // if we can't get formats, do not bother
     if (! $formats = ffmpeg_wrapper_run_command('-formats') ) {
       return;
@@ -567,9 +598,10 @@ function ffmpeg_wrapper_get_file_formats
     $encode_formats = array();
 
     $rows = array();
+
     foreach (explode("\n", $formats) as $format) {
       // match the decode, encode, format, description
-      $pattern ='/[ ]*([D ])([E ])[ ]*([a-zA-Z0-9_,]*)[ ]*([a-zA-Z0-9,_ ]*)/';
+      $pattern ='/[ ]*([D ])([E ])[ ]*([a-zA-Z0-9_,]*)[ ]*([^\$]*)/';
       preg_match($pattern, $format, $matches);
 
       $a_format['type'] = $matches[3];
@@ -579,9 +611,9 @@ function ffmpeg_wrapper_get_file_formats
       if ($matches[1] == 'D') {
         $a_format['decode'] = t('yes');
         // we can have multiple types per format
-        $types = explode(',', $a_format['name']);
+        $types = explode(',', $a_format['type']);
         foreach ($types as $type) {
-          $decode_formats[] = $a_format['type'];
+          $decode_formats[] = $type;
         }
       }
       else {
@@ -592,9 +624,9 @@ function ffmpeg_wrapper_get_file_formats
       if ($matches[2] == 'E') {
         $a_format['encode'] = t('yes');
         // we can have multiple types per format
-        $types = explode(',', $a_format['name']);
+        $types = explode(',', $a_format['type']);
         foreach ($types as $type) {
-          $encode_formats[] = $a_format['type'];
+          $encode_formats[] = $type;
         }
       }
       else {
@@ -611,6 +643,7 @@ function ffmpeg_wrapper_get_file_formats
     $output['encode'] = $encode_formats;
     $output['decode'] = $decode_formats;
     $output['row'] = $rows;
+
     cache_set($cache_id, $output, 'cache', CACHE_TEMPORARY);
   }
   else {
@@ -637,7 +670,8 @@ function ffmpeg_wrapper_file_duration($p
   // do we have any output from ffmpeg already?
   if (! $output) {
     // get duration from ffmpeg
-    $output = ffmpeg_wrapper_run_command("-i $path");
+    // need quotes around the path parameter in case filename has spaces.
+    $output = ffmpeg_wrapper_run_command("-i \"$path\"");
   }
 
   // parse the output looking for "Duration: 00:02:12"
@@ -674,7 +708,8 @@ function ffmpeg_wrapper_file_duration($p
 function ffmpeg_wrapper_file_data($path = null) {
   if (file_exists($path)) {
     // get duration from ffmpeg
-    $output = ffmpeg_wrapper_run_command("-i $path");
+    // need quotes around the path parameter in case filename has spaces.
+    $output = ffmpeg_wrapper_run_command("-i \"$path\"");
 
     // get file format
     $pattern = '/Input #0, (.*),/';
@@ -803,7 +838,7 @@ function ffmpeg_wrapper_padded_size($fil
         $padding = $dest_x - $dest_x_calc;
         $padoptions = '-padleft %d -padright %d';
       }
-      
+
       // Calculate padding on each side. Each value has to be a multiple of 2.
       $padding &= ~1;
       $padding1 = floor($padding / 2);
