forked from sonata-project/SonataMediaBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYouTubeProvider.php
292 lines (234 loc) · 10.2 KB
/
YouTubeProvider.php
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
<?php
/*
* This file is part of the Sonata project.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\MediaBundle\Provider;
use Sonata\MediaBundle\Entity\BaseMedia as Media;
use Symfony\Component\Form\Form;
use Sonata\AdminBundle\Form\FormMapper;
class YouTubeProvider extends BaseProvider
{
/**
* Return the reference image
*
* @param \Sonata\MediaBundle\Entity\BaseMedia $media
* @return null
*/
public function getReferenceImage(Media $media)
{
return $media->getMetadataValue('thumbnail_url');
}
/**
* return the absolute path of source media
*
* @param \Sonata\MediaBundle\Entity\BaseMedia $media
* @return string
*/
public function getAbsolutePath(Media $media)
{
return sprintf('http://www.youtube.com/v/%s', $media->getProviderReference());
}
/**
* @param \Sonata\MediaBundle\Entity\BaseMedia $media
* @param string $format
* @param array $options
* @return array
*/
public function getHelperProperties(Media $media, $format, $options = array())
{
// documentation : http://code.google.com/apis/youtube/player_parameters.html
$defaults = array(
//Values: 0 or 1. Default is 1. Sets whether the player should load related
// videos once playback of the initial video starts. Related videos are
// displayed in the "genie menu" when the menu button is pressed. The player
// search functionality will be disabled if rel is set to 0.
'rel' => 0,
// Values: 0 or 1. Default is 0. Sets whether or not the initial video will autoplay
// when the player loads.
'autoplay' => 0,
// Values: 0 or 1. Default is 0. In the case of a single video player, a setting of 1
// will cause the player to play the initial video again and again. In the case of a
// playlist player (or custom player), the player will play the entire playlist and
// then start again at the first video.
'loop' => 0,
// Values: 0 or 1. Default is 0. Setting this to 1 will enable the Javascript API.
// For more information on the Javascript API and how to use it, see the JavaScript
// API documentation.
'enablejsapi' => 0,
// Value can be any alphanumeric string. This setting is used in conjunction with the
// JavaScript API. See the JavaScript API documentation for details.
'playerapiid' => null,
// Values: 0 or 1. Default is 0. Setting to 1 will disable the player keyboard controls.
// Keyboard controls are as follows:
// Spacebar: Play / Pause
// Arrow Left: Jump back 10% in the current video
// Arrow Right: Jump ahead 10% in the current video
// Arrow Up: Volume up
// Arrow Down: Volume Down
'disablekb' => 0,
// Values: 0 or 1. Default is 0. Setting to 1 enables the "Enhanced Genie Menu". This
// behavior causes the genie menu (if present) to appear when the user's mouse enters
// the video display area, as opposed to only appearing when the menu button is pressed.
'egm' => 0,
// Values: 0 or 1. Default is 0. Setting to 1 enables a border around the entire video
// player. The border's primary color can be set via the color1 parameter, and a
// secondary color can be set by the color2 parameter.
'border' => 0,
// Values: Any RGB value in hexadecimal format. color1 is the primary border color, and
// color2 is the video control bar background color and secondary border color.
'color1' => null,
'color2' => null,
// Values: A positive integer. This parameter causes the player to begin playing the video
// at the given number of seconds from the start of the video. Note that similar to the
// seekTo function, the player will look for the closest keyframe to the time you specify.
// This means sometimes the play head may seek to just before the requested time, usually
// no more than ~2 seconds
'start' => 0,
// Values: 0 or 1. Default is 0. Setting to 1 enables the fullscreen button. This has no
// effect on the Chromeless Player. Note that you must include some extra arguments to
// your embed code for this to work.
'fs' => 1,
// Values: 0 or 1. Default is 0. Setting to 1 enables HD playback by default. This has no
// effect on the Chromeless Player. This also has no effect if an HD version of the video
// is not available. If you enable this option, keep in mind that users with a slower
// connection may have an sub-optimal experience unless they turn off HD. You should ensure
// your player is large enough to display the video in its native resolution.
'hd' => 1,
// Values: 0 or 1. Default is 1. Setting to 0 disables the search box from displaying when
// the video is minimized. Note that if the rel parameter is set to 0 then the search box
// will also be disabled, regardless of the value of showsearch.
'showsearch' => 0,
// Values: 0 or 1. Default is 1. Setting to 0 causes the player to not display information
// like the video title and rating before the video starts playing.
'showinfo' => 0,
// Values: 1 or 3. Default is 1. Setting to 1 will cause video annotations to be shown by
// default, whereas setting to 3 will cause video annotation to not be shown by default.
'iv_load_policy' => 1,
// Values: 1. Default is based on user preference. Setting to 1 will cause closed captions
// to be shown by default, even if the user has turned captions off.
'cc_load_policy' => 1
);
$player_parameters = array_merge($defaults, isset($options['player_parameters']) ? $options['player_parameters'] : array());
$params = array(
'player_parameters' => http_build_query($player_parameters),
'allowFullScreen' => $player_parameters['fs'] == '1' ? 'true' : 'false',
'allowScriptAccess' => isset($options['allowScriptAccess']) ? $options['allowScriptAccess'] : 'always',
'width' => isset($options['width']) ? $options['width'] : $media->getWidth(),
'height' => isset($options['height']) ? $options['height'] : $media->getHeight(),
);
return $params;
}
/**
* build the related create form
*
*/
function buildEditForm(FormMapper $formMapper)
{
$formMapper->add('name');
$formMapper->add('enabled');
$formMapper->add('authorName');
$formMapper->add('cdnIsFlushable');
$formMapper->add('description');
$formMapper->add('copyright');
$formMapper->add('binaryContent', array(), array('type' => 'string'));
}
/**
* build the related create form
*
*/
function buildCreateForm(FormMapper $formMapper)
{
$formMapper->add('binaryContent', array(), array('type' => 'string'));
}
/**
*
* @see BaseProvider::preSave
*/
public function prePersist(Media $media)
{
if (!$media->getBinaryContent()) {
return;
}
$metadata = $this->getMetadata($media);
$media->setProviderName($this->name);
$media->setProviderReference($media->getBinaryContent());
$media->setProviderMetadata($metadata);
$media->setName($metadata['title']);
$media->setAuthorName($metadata['author_name']);
$media->setHeight($metadata['height']);
$media->setWidth($metadata['width']);
$media->setContentType('video/x-flv');
$media->setProviderStatus(Media::STATUS_OK);
$media->setCreatedAt(new \Datetime());
$media->setUpdatedAt(new \Datetime());
}
/**
* @param \Sonata\MediaBundle\Entity\BaseMedia $media
* @return
*/
public function preUpdate(Media $media)
{
if (!$media->getBinaryContent()) {
return;
}
$metadata = $this->getMetadata($media);
$media->setProviderReference($media->getBinaryContent());
$media->setProviderMetadata($metadata);
$media->setHeight($metadata['height']);
$media->setWidth($metadata['width']);
$media->setProviderStatus(Media::STATUS_OK);
$media->setUpdatedAt(new \Datetime());
}
/**
* @throws \RuntimeException
* @param \Sonata\MediaBundle\Entity\BaseMedia $media
* @return mixed|string
*/
public function getMetadata(Media $media)
{
if (!$media->getBinaryContent()) {
return;
}
$url = sprintf('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=%s&format=json', $media->getBinaryContent());
$metadata = @file_get_contents($url);
if (!$metadata) {
throw new \RuntimeException('Unable to retrieve youtube video information for :' . $url);
}
$metadata = json_decode($metadata, true);
if (!$metadata) {
throw new \RuntimeException('Unable to decode youtube video information for :' . $url);
}
return $metadata;
}
/**
* @param \Sonata\MediaBundle\Entity\BaseMedia $media
* @return void
*/
public function postUpdate(Media $media)
{
$this->postPersist($media);
}
/**
* @param \Sonata\MediaBundle\Entity\BaseMedia $media
* @return
*/
public function postPersist(Media $media)
{
if (!$media->getBinaryContent()) {
return;
}
$this->generateThumbnails($media);
}
/**
* @param \Sonata\MediaBundle\Entity\BaseMedia $media
* @return void
*/
public function preRemove(Media $media)
{
}
}