[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/video/plugins/video_zencoder/ -> video_zencoder.lib.inc (source)

   1  <?php
   2  /*
   3   * @file
   4   * Class file to handle amazon s3 transfers.
   5   *
   6   */
   7  
   8  define('VIDEO_ZC_PENDING', 0);
   9  define('VIDEO_ZC_WORKING', 1);
  10  define('VIDEO_ZC_ACTIVE', 2);
  11  define('VIDEO_ZC_FAILED', 3);
  12  
  13  class video_zencoder_api {
  14    private $access_key;
  15    private $limit;
  16    private $bucket;
  17    
  18    /**
  19     * @var Services_Zencoder
  20     */
  21    private $zencoder;
  22  
  23    public function __construct() {
  24      // Create the Zencoder class
  25      $libpath = libraries_get_path('zencoder');
  26      $libfile = $libpath .'/Services/Zencoder.php';
  27  
  28      if (!file_exists($libfile)) {
  29        drupal_set_message(t('The Zencoder library has not been installed correctly. See the <a href="@drupal-status-page">Drupal status page</a> for more information.', array('@drupal-status-page' => url('admin/reports/status'))), 'error');
  30        return;
  31      }
  32  
  33      $this->access_key = variable_get('video_zencoder_api_key', NULL);
  34      $this->limit = variable_get('amazon_s3_limit', 5);
  35      $this->bucket = variable_get('amazon_s3_bucket', '');
  36  
  37      require_once $libfile;
  38      $this->zencoder = new Services_Zencoder($this->access_key);
  39    }
  40  
  41    /**
  42     * create transcoding job on Zencoder.com
  43     */
  44    public function create($file) {
  45      if ($this->zencoder == NULL) {
  46        return FALSE;
  47      }
  48  
  49      // dimensions
  50      $dimensions = explode('x', $file->dimensions);
  51  
  52      // Notifications
  53      $notifications = array(array(
  54        'format' => 'json',
  55        'url' => variable_get('video_zencoder_postback', url('postback/jobs', array('absolute' => TRUE))),
  56      ));
  57  
  58      // S3 permissions
  59      $public = !variable_get('amazon_s3_private', FALSE);
  60  
  61      // Common output URL prefix
  62      $output_url_prefix = 's3://'. $this->bucket .'/'. pathinfo($file->filepath, PATHINFO_DIRNAME) .'/converted/'. pathinfo($file->filepath, PATHINFO_FILENAME) .'.';
  63  
  64      // construct the output array with the presets
  65      $zc_outputs = array();
  66      foreach ($file->presets as $name => $preset) {
  67        $zc_output = array();
  68        $quality = $preset['quality'];
  69        $speed = $preset['speed'];
  70        $upscale = $preset['upscale'];
  71        $stretch = $preset['stretch'];
  72        $frame_rate = $preset['frame_rate'];
  73        $max_frame_rate = $preset['max_frame_rate'];
  74        $keyframe_interval = $preset['keyframe_interval'];
  75        $video_bitrate = $preset['video_bitrate'];
  76        $bitrate_cap = $preset['bitrate_cap'];
  77        $buffer_size = $preset['buffer_size'];
  78        $h264_profile = $preset['h264_profile'];
  79        $h264_level = $preset['h264_level'];
  80        $skip_video = $preset['skip_video'];
  81        $audio_codec = $preset['audio_codec'];
  82        $audio_bitrate = $preset['audio_bitrate'];
  83        $audio_channels = $preset['audio_channels'];
  84        $audio_sample_rate = $preset['audio_sample_rate'];
  85        $skip_audio = $preset['skip_audio'];
  86        $start_clip = $preset['start_clip'];
  87        $clip_length = $preset['clip_length'];
  88  
  89        $zc_output['label'] = 'VIDEO_'. $name .'_'. $file->fid;
  90        $zc_output['url'] = $output_url_prefix . $preset['extension'];
  91        $zc_output['public'] = $public;
  92        $zc_output['width'] = $dimensions[0];
  93        $zc_output['height'] = $dimensions[1];
  94  
  95        if (!empty($quality))
  96          $zc_output['quality'] = $quality;
  97        if (!empty($speed))
  98          $zc_output['speed'] = $speed;
  99        if (!empty($upscale))
 100          $zc_output['upscale'] = $upscale;
 101        if (!empty($frame_rate))
 102          $zc_output['frame_rate'] = $frame_rate;
 103        if (!empty($max_frame_rate))
 104          $zc_output['max_frame_rate'] = $max_frame_rate;
 105        if (!empty($keyframe_interval))
 106          $zc_output['keyframe_interval'] = $keyframe_interval;
 107        if (!empty($video_bitrate))
 108          $zc_output['video_bitrate'] = $video_bitrate;
 109        if (!empty($bitrate_cap))
 110          $zc_output['bitrate_cap'] = $bitrate_cap;
 111        if (!empty($buffer_size))
 112          $zc_output['buffer_size'] = $buffer_size;
 113        if (!empty($h264_profile))
 114          $zc_output['h264_profile'] = $h264_profile;
 115        if (!empty($h264_level))
 116          $zc_output['h264_level'] = $h264_level;
 117        if (!empty($skip_video))
 118          $zc_output['skip_video'] = $skip_video;
 119        if (!empty($audio_codec))
 120          $zc_output['audio_codec'] = $audio_codec;
 121        if (!empty($audio_bitrate))
 122          $zc_output['audio_bitrate'] = $audio_bitrate;
 123        if (!empty($audio_channels))
 124          $zc_output['audio_channels'] = $audio_channels;
 125        if (!empty($audio_sample_rate))
 126          $zc_output['audio_sample_rate'] = $audio_sample_rate;
 127        if (!empty($skip_audio))
 128          $zc_output['skip_audio'] = $skip_audio;
 129        if (!empty($start_clip))
 130          $zc_output['start_clip'] = $start_clip;
 131        if (!empty($clip_length))
 132          $zc_output['clip_length'] = $clip_length;
 133  
 134        //notifications
 135        $zc_output['notifications'] = $notifications;
 136        $zc_outputs[] = $zc_output;
 137      }
 138  
 139      // Add thumbnails
 140      if (empty($zc_outputs)) $zc_outputs[0] = array();
 141      $video_thumb_path = variable_get('video_thumb_path', 'video_thumbs');
 142      $final_thumb_path = file_directory_path() .'/'. $video_thumb_path .'/'. $file->fid;
 143  
 144      $zc_outputs[0]['thumbnails'] = array(
 145        'number' => intval(variable_get('video_thumbs', 5)),
 146        'size' => variable_get('video_thumbs_size', '160x120'),
 147        'base_url' => 's3://'. $this->bucket .'/'. $final_thumb_path,
 148        'prefix' => $file->fid,
 149      );
 150  
 151      $encoding_job_json = array(
 152        'api_key' => $this->access_key,
 153        'input' => 's3://'. $this->bucket .'/'. $file->filepath,
 154        'outputs' => $zc_outputs,
 155      );
 156  
 157      try {
 158        return $this->zencoder->jobs->create($encoding_job_json);
 159      }
 160      catch (Services_Zencoder_Exception $e) {
 161        watchdog('zencoder', 'Zencoder reports errors while converting %file:<br/>!errorlist', array('%file' => $file->filename, '!errorlist' => theme('item_list', $e->getErrors())), WATCHDOG_ERROR);
 162        return FALSE;
 163      }
 164    }
 165  
 166    /**
 167     * Create Zencoder user account
 168     */
 169    public function create_user($email) {
 170      if ($this->zencoder == NULL) {
 171        return FALSE;
 172      }
 173  
 174      try {
 175        // $result is Services_Zencoder_Account
 176        $result = $this->zencoder->accounts->create(array(
 177          'terms_of_service' => '1',
 178          'email' => $email,
 179          'affiliate_code' => 'drupal-video',
 180        ));
 181        
 182        $params = array(
 183          'email' => $email,
 184          'api_key' => $result->api_key,
 185          'password' => $result->password,
 186        );
 187        
 188        variable_set('video_zencoder_api_key', $result->api_key);
 189        $message = drupal_mail('video_zencoder', 'video_zencoder', $email, language_default(), $params);
 190        if (!$message['result']) {
 191          drupal_set_message(t('Unable to send e-mail!. Your Zencoder Details are as below.<br/> <b>API Key</b> : !api_key<br/> <b>Password</b> : !password<br/>', array('!api_key' => $results['api_key'], '!password' => $results['password'])), 'status');
 192        }
 193        else {
 194          drupal_set_message(t('Your account has been created and is ready to start processing on Zencoder'));
 195        }
 196  
 197        return TRUE;
 198      }
 199      catch (Services_Zencoder_Exception $e) {
 200        $errors = '';
 201        foreach ($e->getErrors() as $error) {
 202          if ($error == 'Email has already been taken') {
 203            drupal_set_message(t('Your account already exists on Zencoder. So !login to here and enter API key below.', array('!login' => l(t('login'), 'https://app.zencoder.com/session/new'))));
 204            variable_set('video_zencoder_api_key', 'Please enter your API Key');
 205            return TRUE;
 206          }
 207          $errors .= $error;
 208        }
 209  
 210        return $errors;
 211      }
 212    }
 213  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7