You are here

public function video_phpvideotoolkit::generate_thumbnails in Video 7

Overrides transcoder_interface::generate_thumbnails

File

transcoders/video_phpvideotoolkit.inc, line 69

Class

video_phpvideotoolkit

Code

public function generate_thumbnails($video) {
  global $user;

  // Setup our thmbnail path.
  $video_thumb_path = variable_get('video_thumb_path', 'videos/thumbnails');

  // Get the file system directory.
  $schema_thumb_path = file_default_scheme() . '://' . $video_thumb_path . '/' . $video['fid'];
  file_prepare_directory($schema_thumb_path, FILE_CREATE_DIRECTORY);

  // Total thumbs to generate
  $total_thumbs = variable_get('video_thumbs', 5);
  $videofile = file_load($video['fid']);

  //get the actual video file path from the stream wrappers
  $videopath = drupal_realpath($videofile->uri);

  //get the playtime from the current transcoder
  $duration = $this
    ->get_playtime($videopath);
  $files = NULL;
  for ($i = 1; $i <= $total_thumbs; $i++) {
    $seek = $duration / $total_thumbs * $i - 1;

    //adding minus one to prevent seek times equaling the last second of the video
    $filename = file_munge_filename("video-thumb-for-" . $video['fid'] . "-{$i}.jpg", '', TRUE);
    $thumbfile = $schema_thumb_path . '/' . $filename;

    //skip files already exists, this will save ffmpeg traffic
    if (!is_file(drupal_realpath($thumbfile))) {
      $result = $this->toolkit
        ->setInputFile($videopath);
      if (!$result) {

        // 			if there was an error then get it
        $error_msg = t($this->toolkit
          ->getLastError());
        watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
        $this->toolkit
          ->reset();
        continue;
      }
      $this->toolkit
        ->extractFrame($seek);
      $result = $this->toolkit
        ->setOutput(drupal_realpath($schema_thumb_path) . '/', $filename, PHPVideoToolkit::OVERWRITE_EXISTING);
      if (!$result) {

        // 			if there was an error then get it
        $error_msg = t($this->toolkit
          ->getLastError());
        watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
        $this->toolkit
          ->reset();
        continue;
      }
      $result = $this->toolkit
        ->execute(FALSE, TRUE);
      if ($result !== PHPVideoToolkit::RESULT_OK) {

        // 			if there was an error then get it
        $error_msg = t($this->toolkit
          ->getLastError());
        watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
        $this->toolkit
          ->reset();
        continue;
      }
      if (!file_exists(drupal_realpath($thumbfile))) {
        $error_param = array(
          '%file' => $thumbfile,
        );
        $error_msg = t("Error generating thumbnail for video: generated file %file does not exist.", $error_param);

        // Log the error message.
        watchdog('transcoder', $error_msg, array(), WATCHDOG_ERROR);
        continue;
      }
    }

    // Begin building the file object.
    // @TODO : use file_munge_filename()
    $file = new stdClass();
    $file->uid = $user->uid;
    $file->status = 0;
    $file->filename = trim($filename);
    $file->uri = $thumbfile;
    $file->filemime = file_get_mimetype($filename);
    $file->filesize = filesize(drupal_realpath($thumbfile));
    $file->timestamp = time();
    $files[] = $file;
  }
  return $files;
}