You are here

video_zencoder.module in Video 6.5

Same filename and directory in other branches
  1. 6.4 plugins/video_zencoder/video_zencoder.module

Provides wrapper functions for the s3 amazon webservices. @todo

File

plugins/video_zencoder/video_zencoder.module
View source
<?php

/**
 * @file
 * Provides wrapper functions for the s3 amazon webservices.
 * @todo
 * - cleand up the _video_zencoder_postback_jobs() function
 * - Add to select random thumbnails to the download image.
 */

/**
 * Implementation of hook_menu().
 */
function video_zencoder_menu() {
  $items = array();
  $items['postback/jobs'] = array(
    'page callback' => '_video_zencoder_postback_jobs',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * This will handle Zencoder postback once video conversion is completed
 *
 * @TODO move to separate file
 */
function _video_zencoder_postback_jobs() {
  ignore_user_abort(TRUE);
  $data = file_get_contents('php://input');
  if (empty($data)) {
    watchdog('zencoder', 'Empty postback received from the Zencoder Transcoding servers.', array(), WATCHDOG_WARNING);
    drupal_not_found();
    return;
  }
  $result = json_decode($data);
  watchdog('zencoder', 'Postback received from the Zencoder Transcoding servers.<br/><pre>@data</pre>', array(
    '@data' => print_r($result, TRUE),
  ), WATCHDOG_DEBUG);
  $jobid = $result->job->id;
  $zc_job_state = trim($result->job->state);
  $zc_output_state = trim($result->output->state);
  $state = 0;
  if ($zc_output_state == 'finished' && $zc_job_state == 'finished') {
    $state = VIDEO_RENDERING_COMPLETE;
  }
  elseif ($zc_output_state == 'failed' || $zc_job_state == 'failed') {
    $state = VIDEO_RENDERING_FAILED;
  }
  elseif ($zc_job_state == 'processing') {

    // This state means that one output has finished, but more outputs have still to be generated.
    return;
  }
  $zc = video_get_transcoder('video_zencoder');
  if ($zc == NULL) {
    watchdog('zencoder', 'Could not load Zencoder transcoder.', array(), WATCHDOG_CRITICAL);
    return;
  }
  $video = db_fetch_object(db_query('SELECT * FROM {video_zencoder} WHERE jobid = %d', $jobid));
  if ($video == NULL) {
    watchdog('zencoder', 'Received postback from Zencoder for unknown job @jobid, ignoring.', array(
      '@jobid' => $jobid,
    ), WATCHDOG_ERROR);
    return;
  }
  if ($video->status == VIDEO_RENDERING_COMPLETE) {
    watchdog('zencoder', 'Received postback from Zencoder for job @jobid, which was already finished, ignoring.', array(
      '@jobid' => $jobid,
    ), WATCHDOG_WARNING);
    return;
  }
  if ($state == VIDEO_RENDERING_COMPLETE) {
    db_query('UPDATE {node} SET status = %d WHERE nid = %d', 1, $video->nid);

    // Fetch the thumbnails
    $video_thumb_path = video_thumb_path($video);
    $number_of_thumbs = variable_get('video_thumbs', 5);
    $thumbsdownloaded = 0;
    for ($i = 0; $i < $number_of_thumbs; $i++) {
      $thumbfile = $video_thumb_path . '/' . $video->fid . '_' . sprintf('%04d', $i) . '.png';
      $thumbfiles3 = ltrim($thumbfile, '/');
      if (video_s3_get_object_info($thumbfiles3)) {
        if (video_s3_get_object($thumbfiles3, $thumbfile)) {
          $thumbsdownloaded++;
        }
        else {
          watchdog('zencoder', 'Could not download @thumbfile from Amazon S3 to the local file system.', array(
            '@thumbfile' => $thumbfiles3,
          ), WATCHDOG_ERROR);
        }
      }
    }
    if ($thumbsdownloaded > 0) {

      // Update the thumbnail of the video in the node that uses this file
      $node = node_load($video->nid);
      $hasupdates = FALSE;
      $fieldnames = array_keys(filefield_get_field_list($node->type));

      // Loop through all file fields and check if the file is the file that was just converted
      foreach ($fieldnames as $fieldname) {
        if (!empty($node->{$fieldname})) {
          foreach ($node->{$fieldname} as &$element) {
            if ($element != NULL && $element['fid'] == $video->fid) {
              $element['data']['video_thumb'] = $video_thumb_path . '/' . $video->fid . '_0000.png';
              $hasupdates = TRUE;
            }
          }
        }
      }
      if ($hasupdates) {
        node_save($node);
      }
    }
    $zc
      ->change_status($video, VIDEO_RENDERING_COMPLETE);
    watchdog('zencoder', 'Updated the Zencoder job @id to state @state.', array(
      '@id' => $jobid,
      '@state' => $zc_output_state,
    ), WATCHDOG_INFO);
  }
  elseif ($state == VIDEO_RENDERING_FAILED) {
    $zc
      ->change_status($video, VIDEO_RENDERING_FAILED);
    $errormsg = 'not given';
    $errorlink = '#';
    if (!empty($result->output->error_message)) {
      $errormsg = $result->output->error_message;
    }
    if (!empty($result->output->error_link)) {
      $errorlink = $result->output->error_link;
    }
    watchdog('zencoder', 'Zencoder job @jobid failed to convert video.<br/>Error message: @errormessage<br/><a href="@error-link">More information about this error</a>', array(
      '@jobid' => $jobid,
      '@errormessage' => $errormsg,
      '@errorlink' => $errorlink,
    ), WATCHDOG_ERROR);
  }
}

/**
 * Implementation of hook_mail().
 */
function video_zencoder_mail($key, &$message, $params) {
  $language = $message['language'];
  $message['subject'] .= 'Zencoder Registration Details for Drupal Video';
  $message['body'][] = video_zencoder_mail_default($params);
}
function video_zencoder_mail_default($params) {
  return t('Welcome to Zencoder for Drupal
-------------------------------

Your account has been created and is ready to start processing.

Your account details are as below.

E-mail address (login): @email
API Key : @api_key
Password : @password

* Login URL: https://app.zencoder.com/login

You can get help at the following places:

* Our chat room at http://zencoder.com/chat
* Customer forums at https://help.zencoder.com/forums
* The help desk at https://help.zencoder.com/tickets/new

We\'d love to hear from you. Let us know how we can help. Thanks!

Thanks,
-Zencoder for Drupal Team', array(
    '@api_key' => $params['api_key'],
    '@password' => $params['password'],
  ));
}
function video_zencoder_ctools_plugin_directory($module, $plugin) {
  if ($module == 'video' && $plugin == 'transcoder') {
    return 'transcoders';
  }
}

Functions

Namesort descending Description
video_zencoder_ctools_plugin_directory
video_zencoder_mail Implementation of hook_mail().
video_zencoder_mail_default
video_zencoder_menu Implementation of hook_menu().
_video_zencoder_postback_jobs This will handle Zencoder postback once video conversion is completed