You are here

public function video_ffmpeg::generate_thumbnails in Video 6.4

Same name and namespace in other branches
  1. 7 transcoders/video_ffmpeg.inc \video_ffmpeg::generate_thumbnails()

Overrides transcoder_interface::generate_thumbnails

File

transcoders/video_ffmpeg.inc, line 65

Class

video_ffmpeg

Code

public function generate_thumbnails($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 ffmpeg traffic
    if (!is_file($thumbfile)) {
      if (!isset($duration)) {
        $duration = $this
          ->get_playtime($video['filepath']);
      }
      $seek = $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_ffmpeg', $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;
}