--- sites/all/modules/ffmpeg_wrapper/ffmpeg_wrapper.module	2009-01-02 15:26:20.000000000 -0500
+++ sites/all/modules/ffmpeg_wrapper/ffmpeg_wrapper.new.module	2009-02-21 18:56:37.000000000 -0500
@@ -256,9 +256,9 @@ function ffmpeg_wrapper_run_command($com
     return false;
   }
 
-  // run the command
+  // run the command TODO: FIX so that it works with all files? (Try under Linux first). 
   ob_start();
-    passthru($ffmpeg_object->command." 2>&1", $command_return);
+	passthru($ffmpeg_object->command." 2>&1", $command_return);
     $ffmpeg_object->output = ob_get_contents();
   ob_end_clean();
 
@@ -273,6 +273,7 @@ function ffmpeg_wrapper_run_command($com
       return false;
     }
   }
+  
   return $ffmpeg_object->output;
 }
 
@@ -318,7 +319,8 @@ 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.
+ * Checks the file format and codecs. Not the file extension. 
  *
  * @param $file
  *   A full system filepath.
@@ -326,24 +328,54 @@ function ffmpeg_wrapper_vhook_list($path
  *   TRUE if file is in the list of decodeable files.
  */
 function ffmpeg_wrapper_can_decode($path) {
-  $file_types = ffmpeg_wrapper_get_file_formats('decode');
-  $path_parts = pathinfo($path);
-
-  // exception handling
-  // WMVs are sometimes asf files
-  if (preg_match('/wmv/i', $path_parts['extension'])) {
-    $exception = 'asf';
-  }
-
+  
+  $file_types = ffmpeg_wrapper_get_file_formats('decode'); 
+  
+  $file_data = ffmpeg_wrapper_file_data($path); 
+    
+  // check to see if file format can be decoded.
+  $can_file_format_decode = false; 
+  
+  // if the file matches a valid format code, or the movie is in a quicktime format, 
+  // it passes since it's a format supported by ffmpeg. 
   if ($file_types) {
     foreach ($file_types as $file_type) {
-      if ($path_parts['extension']) {
-        if (stristr($file_type, $path_parts['extension']) || stristr($file_type, $exception) ) {
-          return true;
-        }
+        if (stristr($file_type, $file_data['format']) || $file_data['format'] == 'mov,mp4,m4a,3gp,3g2,mj2' ) {
+          $can_file_format_decode = true;
+		  break;
+        } 
+     }
+   } 
+  
+  // check to see if codecs can be decoded. 
+  $codecs = ffmpeg_wrapper_get_codecs('decode');  
+  
+  $can_audio_decode = false; 
+  $can_video_decode = false; 
+
+  if ($file_types) {
+    foreach ($codecs as $codec) { 
+
+      if  ($codec == $file_data["audio"]["codec"] || $file_data["audio"]["codec"] == NULL) {
+          $can_audio_decode = true;
       }
+
+	  if ($codec == $file_data["video"]["codec"] || $file_data["video"]["codec"] == NULL) {
+	  	  $can_video_decode = true;
+	  }
+	  
+	  if ($can_audio_decode && $can_video_decode && $can_file_format_decode) {
+	  	if ($file_data["audio"]["codec"] == NULL && $file_data["video"]["codec"] == NULL) {
+			return false;
+		}
+	  	else {
+			return true;
+		} 
+	  }
+	  
     }
   }
+
   return false;
 }
 
@@ -424,6 +456,7 @@ function ffmpeg_wrapper_get_codecs($retu
     $data['encode'] = $encode_formats;
     $data['decode'] = $decode_formats;
     $data['rows'] = $rows;
+	
     cache_set($cache_id, $data, 'cache', CACHE_TEMPORARY);
   }
   else {
@@ -543,6 +576,7 @@ function ffmpeg_wrapper_frame_sizes(){
  *   Array of options.
  */
 function ffmpeg_wrapper_get_file_formats($ret = null) {
+
   $cache_id = 'ffmpeg_wrapper_file_formats';
   $cache = cache_get($cache_id, 'cache');
   // do we have cached data?
@@ -602,7 +636,7 @@ function ffmpeg_wrapper_get_file_formats
   else {
     $output = $cache->data;
   }
-
+  
   // return the requested data
   return $output[$ret];
 }
@@ -620,7 +654,7 @@ function ffmpeg_wrapper_get_file_formats
  */
 function ffmpeg_wrapper_file_duration($path, $timecode = null) {
   // get duration from ffmpeg
-  $output = ffmpeg_wrapper_run_command("-i $path");
+  $output = ffmpeg_wrapper_run_command("-i \"$path\"");
 
   // parse the output looking for "Duration: 00:02:12"
   $pattern = "/Duration: ([0-9]+:[0-9]+:[0-9]+)\.[0-9]+/";
@@ -656,11 +690,12 @@ function ffmpeg_wrapper_file_duration($p
 function ffmpeg_wrapper_file_data($path = null) {
   if ($path) {
     // get duration from ffmpeg
-    $output = ffmpeg_wrapper_run_command("-i $path");
-
+    $output = ffmpeg_wrapper_run_command("-i \"$path\"");
+	
     // get file format
     $pattern = '/Input #0, (.*),/';
     preg_match($pattern, $output, $matches);
+	
     $file['format'] = $matches[1];
 
     // get file duration
@@ -683,19 +718,22 @@ function ffmpeg_wrapper_file_data($path 
 
     unset($matches);
     // take the last match and extract the bit rate if present
-    $pattern = "/Audio: .* (.*) kb\/s/";
+    $pattern = "/Audio: .* (.* kb\/s)/";
     preg_match($pattern, $output, $matches);
     $file['audio']['ab'] = $matches[1] ? $matches[1] : 'na';
 
     // get video settings
-    // format is: codec, filetype?, frame size, kb/s
-    $pattern = "/Video: (.*), (.*), ([0-9x]*) \[.*\], ([0-9]*)/";
-    preg_match($pattern, $output, $matches);
+    // format is: codec, filetype?, frame size, kb/s or extra data.
+    $pattern = "/Video: ([^,]+), ([^,]+), ([0-9x ]*)[^,]*, ([0-9]*.*\/s|[A-Za-z]+[^,]*), ([0-9\.]*)/";
+    preg_match($pattern, $output, $matches); 
     $file['video']['codec'] = $matches[1];
     // $file['video']['type'] = $matches[2];
     $file['video']['s'] = $matches[3];
+	// if we don't have a valid bitrate returned from FFMPEG, set this element null. 
+	if (substr($matches[4], -2) != "/s") {
+		$matches[4] = NULL;
+	}
     $file['video']['br'] = $matches[4] ? $matches[4] : 'na';
-
     return $file;
   }
 }
