forked from alchemyguy/YoutubeLaravelApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoService.php
165 lines (136 loc) · 4.69 KB
/
VideoService.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
<?php
namespace ZeroDUDDU\YoutubeLaravelApi;
use Google\Http\MediaFileUpload;
use Google\Service\YouTube;
use Google\Service\YouTube\Video;
use Google\Service\YouTube\VideoSnippet;
use Google\Service\YouTube\VideoStatus;
use ZeroDUDDU\YoutubeLaravelApi\Auth\AuthService;
use Exception;
class VideoService extends AuthService
{
private readonly Youtube $youTube;
public function __construct($token)
{
parent::__construct();
if (!$this->setAccessToken($token)) {
throw new Exception('invalid token');
}
$this->youTube = new YouTube($this->client);
}
/**
* @param string $part [snippet,contentDetails,id,statistics](comma separated id's)
* @param array $params [regionCode,relevanceLanguage,videoCategoryId, videoDefinition, videoDimension]
*/
public function videosListById(string $part, array $params = []): YouTube\VideoListResponse
{
$params = array_filter($params);
return $this->youTube->videos->listVideos($part, $params);
}
/**
* @param string $part [snippet,id]
* @param array $params ['maxResults','q','type','pageToken']
*/
public function searchListByKeyword(string $part, array $params): YouTube\SearchListResponse
{
$params = array_filter($params);
return $this->youTube->search->listSearch($part, $params);
}
/**
* @param string $part [ sinppet, id]
* @param array $params [ regionCode,relatedToVideoId,relevanceLanguage,videoCategoryId,type(video or channel)]
*/
public function relatedToVideoId(string $part, array $params): YouTube\SearchListResponse
{
$params = array_filter($params);
return $this->youTube->search->listSearch($part, $params);
}
/**
* @param string[] $tags
*/
public function uploadVideo(
string $videoPath,
string $title,
string $description,
string $categoryId,
string $privacyStatus = 'private',
array $tags = [],
array $data = []
): Video {
/**
* snippet [title, description, tags and category ID]
* asset resource [snippet metadata and type.]
*/
$snippet = new VideoSnippet();
$snippet->setTitle($title);
$snippet->setDescription($description);
$snippet->setCategoryId($categoryId);
$snippet->setTags($tags);
/**
* video status ["public", "private", "unlisted"]
*/
$status = new VideoStatus();
$status->privacyStatus = $privacyStatus;
/**
* snippet and status [link with new video resource.]
*/
$video = new Video();
$video->setSnippet($snippet);
$video->setStatus($status);
/**
* size of chunk to be uploaded in bytes [default 1 * 1024 * 1024]
* (Set a higher value for reliable connection as fewer chunks lead to faster uploads)
*/
if (isset($data['chunk_size'])) {
$chunkSizeBytes = $data['chunk_size'];
} else {
$chunkSizeBytes = 1 * 1024 * 1024;
}
/**
* Setting the defer flag to true tells the client to return a request which can be called with ->execute();
* instead of making the API call immediately
*/
$this->client->setDefer(true);
/**
* request [API's videos.insert method] [ to create and upload the video]
*/
$insertRequest = $this->youTube->videos->insert('status,snippet', $video);
/**
* MediaFileUpload object [resumable uploads]
*/
$media = new MediaFileUpload(
$this->client,
$insertRequest,
'video/*',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($videoPath));
/**
* Read the media file [to upload chunk by chunk]
*/
$status = false;
$handle = fopen($videoPath, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
fclose($handle);
/**
* set defer to false [to make other calls after the file upload]
*/
$this->client->setDefer(false);
return $status;
}
public function deleteVideo(string $id, array $params = []): mixed
{
$params = array_filter($params);
return $this->youTube->videos->delete($id, $params);
}
public function videosRate(string $id, string $rating = 'like', array $params = []): mixed
{
$params = array_filter($params);
return $this->youTube->videos->rate($id, $rating, $params);
}
}