aboutsummaryrefslogtreecommitdiff
path: root/modules/video_zencoder/video_zencoder.module
blob: 93735c0de08833580f7b4a96a24281cb0048b33e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?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_cron().
 */
defined('VIDEO_RENDERING_PENDING') ||
    define('VIDEO_RENDERING_PENDING', 1);
defined('VIDEO_RENDERING_ACTIVE') ||
    define('VIDEO_RENDERING_ACTIVE', 5);
defined('VIDEO_RENDERING_COMPLETE') ||
    define('VIDEO_RENDERING_COMPLETE', 10);
defined('VIDEO_RENDERING_FAILED') ||
    define('VIDEO_RENDERING_FAILED', 20);

/**
 * Implementation of hook_menu().
 */
function video_zencoder_menu() {
  $items = array();
  $items['postback/jobs'] = array(
//    'title' => 'Video',
//    'description' => 'Configure different aspects of the video module and its plugins',
    'page callback' => '_video_zencoder_postback_jobs',
    'access arguments' => array('zencoder postback'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * This will handle Zencoder postback once video conversion is completed
 *
 */
function _video_zencoder_postback_jobs() {
// get JSON post data
  $data = file_get_contents("php://input");
  watchdog('zencoder', t('Postback received from the Zencoder Transcoding servers.' . serialize($data)));
// get the file object by zenocder job id
  $video = json_decode($data);
//  print_r($zc);
//  $zc_job_id = $video->job->id;
  $zc_job_state = trim($video->job->state);
//  $zc_output_id = $video->output->id;
  $zc_output_state = trim($video->output->state);
//  $zc_output_url = $video->output->url;
  if ($zc_output_state == 'finished' && $zc_job_state == 'finished')
    $video->output->state = VIDEO_RENDERING_COMPLETE;
  if ($zc_output_state == 'failed' || $zc_job_state == 'failed')
    $video->output->state = VIDEO_RENDERING_FAILED;
  if ($zc_job_state == 'processing') {
    watchdog('zencoder', t('Job !jobid is processing.', array('!jobid' => $video->job->id)));
    return;
  }

// update the Zencoder Job
  module_load_include('inc', 'video_zencoder', '/includes/transcoder/video_zencoder');
  $zc = new video_zencoder;
// Lets run delete.
  $videodb = $zc->load_job_by_jobid($video->job->id);
  if ($video->output->state == VIDEO_RENDERING_COMPLETE) {
    $nid = $videodb->nid;
    $vid = $videodb->vid;
    $fid = $videodb->fid;

    $zc->change_status($vid, VIDEO_RENDERING_COMPLETE);
// update the thumbanils
// this will update the default thumbnails, if user want to select another one then they wil need to edit the node
// Setup our thmbnail path.
    $video_thumb_path = variable_get('video_thumb_path', 'videos/thumbnails');
    $final_thumb_path = file_default_scheme() . '://' . $video_thumb_path . '/' . $fid;
    // Ensure the destination directory exists and is writable.
    file_prepare_directory($final_thumb_path, FILE_CREATE_DIRECTORY);
//    $i = rand(0, (variable_get('no_of_video_thumbs', 5) - 1));
    $filename = $fid . '_' . sprintf("%04d", 1) . '.png';
    $thumbfile = $final_thumb_path . '/' . $filename;
    // 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 . '/' . $fid . '/' . $filename)) {
      $response = $s3->get_object($bucket, $video_thumb_path . '/' . $fid . '/' . $filename, array('fileDownload' => drupal_realpath($thumbfile)));
      $default = $final_thumb_path . '/no-thumb.png';
      @unlink(drupal_realpath($default));
      if ($response->status == 200)
        watchdog('zencoder', t('Successfully downloaded the thumbnails file and replaced the default image.'));
      else
        watchdog('zencoder', t('Download thumbanils files is failed.'), array(), WATCHDOG_ERROR);
    }
    watchdog('zencoder', t('No thumbanils found.', array('!id' => $video->job->id, '!states' => $zc_output_state)));
  } else if ($video->output->state == VIDEO_RENDERING_FAILED) {
//    echo 'working failed';
    $zc->change_status($vid, VIDEO_RENDERING_FAILED);
    watchdog('zencoder', t('Zencoder job failed converting videos, please login to zencoder web and check the erros.', array()), NULL, WATCHDOG_ERROR);
  } else {
    echo 'zencoder postback is working';
  }
}

/**
 * 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.

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']));
}