-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencoding_com.module
More file actions
executable file
·299 lines (239 loc) · 8.71 KB
/
encoding_com.module
File metadata and controls
executable file
·299 lines (239 loc) · 8.71 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
require('encoding_com.php');
function encoding_com_theme() {
return array(
'encoding_com_uploader' => array('arguments' => array('element' => NULL)),
'encoding_com_formatter_default' => array('arguments' => array('element' => NULL)),
);
}
/**
* Implementation of hook_form_alter().
*
* We add our custom 'preroll' form elements to node-forms for:
* - game nodes
* - show nodes
* - video nodes??
*/
function encoding_com_form_alter(&$form, $form_state, $form_id) {
// check that the form is a node-edit form (and the node-type can use pre-rolls) before altering the form.
if (strpos($form_id, '_node_form') && isset($form ['#node']) && $form ['#node']->type='video') {
$form ['video_file'] = array(
'#title' => t('video file s3'),
'#description' => t('The following section is for adding specific pre-roll advertising to this particular node: '),
'#attributes' => array(
'id' => 'cn_ad_prerolls_controls' ),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE );
}
}
function encoding_com_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#encoding_com":
$output = '<p>'. t("This module allows users to upload videos through Encoding.com, the video encoding service") .'</p>';
break;
}
return $output;
}
function encoding_com_perm() {
return array('upload videos');
}
function encoding_com_menu() {
$items = array();
$items['admin/settings/encoding_com'] = array(
'title' => 'Encoding.com API Settings',
'description' => 'Specify your Encoding.com account details here',
'page callback' => 'drupal_get_form',
'page arguments' => array('encoding_com_admin_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
'file' => 'encoding_com.admin.inc',
);
return $items;
}
function encoding_com_field_info() {
return array(
'encoding_com_video' => array(
'label' => t('Video file'),
'description' => t('Upload a video directly to Encoding.com for immediate encoding'),
),
);
}
function encoding_com_widget_info() {
return array(
'encoding_com_uploader' => array(
'label' => t('JS uploader'),
'field types' => array('encoding_com_video'),
'callbacks' => array(
'default value' => CONTENT_CALLBACK_DEFAULT,
),
),
);
}
function encoding_com_field_settings($op, $field) {
$delegate = 'encoding_com_field_settings_' . str_replace(' ', '_', $op);
if (function_exists($delegate)) {
return $delegate($field);
}
}
function encoding_com_field_settings_database_columns($field) {
return array(
'video_id' => array('type' => 'varchar', 'length' => 255, 'not null' => FALSE, 'sortable' => TRUE),
);
}
function encoding_com_field($op, &$node, $field, &$items, $teaser, $page) {
$delegate = 'encoding_com_field_' . $op;
if (function_exists($delegate)) {
return $delegate($node, $field, $items, $teaser, $page);
}
}
function encoding_com_field_formatter_info() {
return array(
'default' => array('label' => t('Default'), 'field types' => array('encoding_com_video')),
);
}
function encoding_com_widget(&$form, &$form_state, $field, $items, $delta = 0) {
$element = array(
'#type' => $field['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
);
return $element;
}
function encoding_com_elements() {
$elements = array();
$elements['encoding_com_uploader'] = array(
'#input' => TRUE,
'#columns' => array('video_id'),
'#process' => array('encoding_com_widget_process'),
);
return $elements;
}
function encoding_com_widget_process($element, $edit, $form_state, $complete_form) {
$field_key = 'video_id';
drupal_add_js(drupal_get_path('module', 'encoding_com') .'/encoding_com_uploader/jquery.encoding_com-uploader-0.6.cat.js');
$prefix = '';
if (isset($element['#value']['video_id']) && $element['#value']['video_id']) {
$prefix .= theme('encoding_com_formatter_default', array('#item' => $element['#value']));
}
$prefix .= _encoding_com_widget_additional_markup($element['#id']);
$element[$field_key] = array(
'#type' => 'hidden',
'#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : '',
'#title' => $element['#title'],
'#description' => $element['#description'],
'#required' => $element['#required'],
'#field_name' => $element['#field_name'],
'#type_name' => $element['#type_name'],
'#delta' => $element['#delta'],
'#columns' => $element['#columns'],
'#prefix' => $prefix,
'#suffix' => _encoding_com_widget_js($element['#id']),
);
return $element;
}
function encoding_com_content_is_empty($item, $field) {
return ! $item['video_id'];
}
function theme_encoding_com_uploader($element) {
drupal_add_css(drupal_get_path('module', 'panda') .'/encoding_com_uploader/panda-uploader.css');
return theme('form_element', $element, $element['#children']);
}
function theme_encoding_com_formatter_default($element) {
$video_id = $element['#item']['video_id'];
$vid = _encoding_com_get_video_details($video_id);
$vid->url = _encoding_com_get_video_url($vid);
$vid->screenshot_url = _encoding_com_get_screenshot_url($vid);
if ($vid->status == 'success') {
$output = <<<EOF
<div>
<video id="movie" width="{$vid->width}" height="{$vid->height}" preload="none" poster="{$vid->screenshot_url}" controls>
<source src="{$vid->url}" type="video/mp4">
</video>
</div>
EOF;
}
else {
$output = <<<EOF
<p>This video is currently being processed. Please wait until completed.</p>
EOF;
}
return $output;
}
function _encoding_com_widget_additional_markup($element_id) {
return <<<EOF
<span id="{$element_id}-upload_button"></span>
<input type="text" id="{$element_id}-upload_filename" disabled="true" class="encoding_com_upload_filename" />
<div id="{$element_id}-upload_progress" class="encoding_com_upload_progress"></div>
EOF;
}
function _encoding_com_widget_js($element_id) {
$signed_params = json_encode(@_panda()->signed_params('POST', '/videos.json'));
return <<<EOF
<script type="text/javascript">
if ($('#{$element_id}-video-id').length > 0) {
$('#{$element_id}-video-id').pandaUploader($signed_params, {
upload_button_id: '{$element_id}-upload_button',
upload_filename_id: '{$element_id}-upload_filename',
upload_progress_id: '{$element_id}-upload_progress',
api_url: 'http://api.pandastream.com/v2',
uploader_dir: '/encoding_com_example_php/encoding_com_uploader',
disable_submit_button: false
});
}
</script>
EOF;
}
function _panda() {
static $panda;
if ($panda) {
return $panda;
}
$cloud_id = variable_get('encoding_com_cloud_id', NULL);
$access_key = variable_get('encoding_com_access_key', NULL);
$secret_key = variable_get('encoding_com_secret_key', NULL);
$panda = new Panda(array('cloud_id' => $cloud_id, 'access_key' => $access_key, 'secret_key' => $secret_key));
return $panda;
}
function _encoding_com_get_video_url($details) {
return _encoding_com_get_file_base_url($details) . $details->extname;
}
function _encoding_com_get_screenshot_url($details) {
return _encoding_com_get_file_base_url($details) . '_4.jpg';
}
function _encoding_com_get_file_base_url($details) {
$s3_bucket_name = variable_get('encoding_com_s3_bucket', NULL);
return "http://$s3_bucket_name.s3.amazonaws.com/{$details->encoding_id}";
}
function _encoding_com_get_video_details($video_id) {
static $details;
if ($details) {
return $details;
}
$details = db_fetch_object(db_query("SELECT * FROM {encoding_com_video_encodings} WHERE video_id = '%s'", $video_id));
if ($details && $details->status != 'processing') {
return $details;
}
$encodings = json_decode(@_panda()->get("/videos/$video_id/encodings.json"));
if ( ! $encodings) {
return NULL;
}
// TODO: mechanism to choose between available profiles
// TODO: break gently if no profiles available
foreach ($encodings as $encoding) {
if ($encoding->extname == '.mp4') {
$encoding->encoding_id = $encoding->id;
unset($encoding->id);
$details = _encoding_com_store_video_details($encoding);
}
}
return $details;
}
function _encoding_com_store_video_details($encoding) {
db_query("UPDATE {encoding_com_video_encodings} SET status = '%s' WHERE video_id = '%s'", $encoding->status, $encoding->video_id);
if ( ! db_affected_rows()) {
db_query("INSERT INTO {encoding_com_video_encodings} (video_id, encoding_id, width, height, extname, status) VALUES ('%s', '%s', %d, %d, '%s', '%s')", $encoding->video_id, $encoding->encoding_id, $encoding->width, $encoding->height, $encoding->extname, $encoding->status);
}
return $encoding;
}