You are here

public function video_zencoder::generate_thumbnails in Video 7

Same name and namespace in other branches
  1. 6.5 plugins/video_zencoder/transcoders/video_zencoder.inc \video_zencoder::generate_thumbnails()
  2. 6.4 plugins/video_zencoder/transcoders/video_zencoder.inc \video_zencoder::generate_thumbnails()

Overrides transcoder_interface::generate_thumbnails

File

modules/video_zencoder/transcoders/video_zencoder.inc, line 19

Class

video_zencoder

Code

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

  // Setup our thmbnail path.
  $video_thumb_path = variable_get('video_thumb_path', 'videos/thumbnails');
  $final_thumb_path = file_default_scheme() . '://' . $video_thumb_path . '/' . $video['fid'];

  // Ensure the destination directory exists and is writable.
  file_prepare_directory($final_thumb_path, FILE_CREATE_DIRECTORY);
  $files = array();

  // no thumbnails to generate
  $number_of_thumbs = variable_get('video_thumbs', 5);
  for ($i = 0; $i < $number_of_thumbs; $i++) {

    // @TODO Remove hard coded file types
    $filename = $video['fid'] . '_' . sprintf("%04d", $i) . '.png';
    $thumbfile = $final_thumb_path . '/' . $filename;

    //skip files already exists, this will save ffmpeg traffic
    if (!file_exists(drupal_realpath($thumbfile))) {
      $default = drupal_get_path('module', 'video') . '/images/no-thumb.png';

      // Download generated thumbnails from S3.
      libraries_load('awssdk');
      $key = variable_get('media_amazon_key', '');
      $secret_key = variable_get('media_amazon_key_secret', '');
      $bucket = variable_get('media_amazon_s3_bucket', '');
      $s3 = new AmazonS3($key, $secret_key);
      if ($s3
        ->get_object_metadata($bucket, $video_thumb_path . '/' . $video['fid'] . '/' . $filename)) {
        $s3
          ->get_object($bucket, $video_thumb_path . '/' . $video['fid'] . '/' . $filename, array(
          'fileDownload' => drupal_realpath($thumbfile),
        ));
      }
      else {
        $thumbfile = $final_thumb_path . '/no-thumb.png';
        @copy($default, drupal_realpath($thumbfile), FILE_EXISTS_REPLACE);
      }

      //          $thumbfile = drupal_get_path('module', 'video') . '/images/no_thumb.gif';
      if (!file_exists(drupal_realpath($thumbfile))) {
        $error_param = array(
          '%file' => $thumbfile,
          '%out' => $s3_get_object,
        );
        $error_msg = t("Error downloading thumbnail for video: generated file %file does not exist.<br />S3 Output:<br />%out", $error_param);

        // Log the error message.
        watchdog('zencoder', $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;
}