/**
* Get the duration of an audio or video file.
*
* @param $node
*   The node to which the audio or video file is attached.
*
* @param $filepath
*   The path to the audio or video file.
*
* @return
*   A string in the form "HH:MM:SS" representing the duration in hours,
*   minutes and seconds.
*/
function flashvideo_get_duration($node, $filepath) {
  if ($filepath &&
      variable_get('flashvideo_' . $node->type .'_ffmpegphp', 0)) {
    $extension = "ffmpeg";
    $extension_soname = $extension . "." . PHP_SHLIB_SUFFIX;
    $extension_fullname = PHP_EXTENSION_DIR . "/" . $extension_soname;

    // Load extension.
    if (!extension_loaded($extension)) {
      dl($extension_soname);
    }

    // Use FFMPEG-PHP to get video information.
    $movie = new ffmpeg_movie($filepath);
  }

  // Get the duration of the movie or audio file in seconds.
  if($filepath && $movie) {
    $duration = $movie->getDuration();
  }
  else {
    $duration = 0;
  }

  // Return the duration as a formatted "HH:MM:SS" string.
  $hours = $duration / 3600;
  $minutes = ($duration % 3600) / 60;
  $seconds = ($duration % 3600) % 60;
  return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
}