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 -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	22 Apr 2009 20:51:51 -0000
@@ -230,7 +230,7 @@
 
 
 /* ************************************************ */
-/* Interactions with ffmpeg                         */
+/* Interactions with ffmpeg                         */ 
 /* ************************************************ */
 
 /**
@@ -328,7 +328,8 @@
 
 
 /**
- * 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.
@@ -337,19 +338,42 @@
  */
 function ffmpeg_wrapper_can_decode($path) {
   // 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)) {
-      return true;
+   // 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;
     }
   }
-  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' ) {
+      $can_audio_decode = true;
+    }
+
+    if ($codec == $file_data['video']['codec'] || $file_data['video']['codec'] == 'na') {
+      $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;
+    }    
+  }  
+  return false;  
 }
 
 
@@ -551,7 +575,7 @@
   $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 +591,10 @@
     $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 +604,9 @@
       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 +617,9 @@
       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 +636,7 @@
     $output['encode'] = $encode_formats;
     $output['decode'] = $decode_formats;
     $output['row'] = $rows;
+    
     cache_set($cache_id, $output, 'cache', CACHE_TEMPORARY);
   }
   else {
@@ -637,7 +663,8 @@
   // 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 +701,8 @@
 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, (.*),/';
@@ -1245,4 +1273,4 @@
   );
 
   return $form;
-}
\ No newline at end of file
+}
