You are here

public function video_localcommand::generate_thumbnails in Video 6.5

Overrides video_transcoder::generate_thumbnails

File

transcoders/video_localcommand.inc, line 73

Class

video_localcommand

Code

public function generate_thumbnails(stdClass $video) {
  global $user;
  $final_thumb_path = video_thumb_path($video);
  $total_thumbs = variable_get('video_thumbs', 5);
  $files = NULL;
  for ($i = 1; $i <= $total_thumbs; $i++) {
    $filename = '/video-thumb-for-' . $video->fid . '-' . $i . '.jpg';
    $thumbfile = $final_thumb_path . $filename;

    // Skip files already exists, this will save time
    if (!is_file($thumbfile)) {
      if (!isset($duration)) {
        $duration = $this
          ->get_playtime($video->filepath);
      }
      $seek = floor($duration / $total_thumbs) * $i - 1;

      // Adding minus one to prevent seek times equaling the last second of the video
      // Setup the command to be passed to the transcoder.
      $command = $this->cmdpath . ' ' . strtr($this->thumbcmdoptions, array(
        '!videofile' => escapeshellarg($video->filepath),
        '!seek' => $seek,
        '!thumbfile' => $thumbfile,
      ));

      // Generate the thumbnail from the video.
      $command_output = '';
      $this
        ->run_command($command, $command_output, t('generating thumbnails'));

      // don't consider zero-byte files a success
      $exists = file_exists($thumbfile);
      if (!$exists || filesize($thumbfile) == 0) {
        $params = array(
          '%file' => $thumbfile,
          '%video' => $video->filename,
        );
        if ($exists) {
          $error_msg = 'Error generating thumbnail for video %video: generated file %file is empty. This problem may be caused by a broken video file. The video reports that its length is @duration seconds. If this is wrong, please recreate the video and try again.';
          $params['@duration'] = $duration;
          unlink($thumbfile);
        }
        else {
          $error_msg = 'Error generating thumbnail for video %video: generated file %file does not exist.';
        }

        // Log the error message and break. Other thumbnails probably will not be generated as well.
        watchdog('video_command', $error_msg, $params, WATCHDOG_ERROR);
        drupal_set_message(t($error_msg, $params), 'error');
        break;
      }
    }

    // Begin building the file object.
    // @TODO : use file_munge_filename()
    $file = new stdClass();
    $file->uid = $user->uid;
    $file->status = FILE_STATUS_TEMPORARY;
    $file->filename = $filename;
    $file->filepath = $thumbfile;
    $file->filemime = 'image/jpeg';
    $file->filesize = filesize($thumbfile);
    $file->timestamp = time();
    $files[] = $file;
  }
  return $files;
}