-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_video_from_youtube.php
327 lines (258 loc) · 11.7 KB
/
get_video_from_youtube.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
error_reporting(E_ERROR | E_PARSE);
include_once 'message.class.php';
include_once 'youtube_converter.class.php';
include_once 'google_client_helper.php';
require_once('conf.php');
require_once __DIR__ . '/vendor/autoload.php';
use YoutubeDl\YoutubeDl;
use YoutubeDl\Exception\CopyrightException;
use YoutubeDl\Exception\NotFoundException;
use YoutubeDl\Exception\PrivateVideoException;
session_start();
if (is_ajax()) {
if (isset($_POST["type"]) && !empty($_POST["type"])
&& isset($_POST["url"]) && !empty($_POST["url"]))
{
$return = "";
if($_POST["type"] == "mp4")
//$return = GetYoutubeVideoByURL($_POST["url"]);
$return = GetYoutubeVideoDonwloadURLByURL($_POST["url"]);
else if($_POST["type"] == "mp3")
$return = GetMp3FromYoutubeVideo($_POST["url"]);
//$return = GetMp3DownloadURLFromYoutubeVideo($_POST["url"]);
else if($_POST["type"] == "srt")
$return = GetSrtByURL($_POST["url"]);
else
$return = Message::get_error_message("Uknown type. Only mp3, mp4 and srt is allowed.");
echo $return->toJSON();
}
}
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function ValidateYoutubeURL($youtubeURL) {
if(!empty($youtubeURL) && !filter_var($youtubeURL, FILTER_VALIDATE_URL) === false)
{
/*
$parsed_url = parse_url($youtubeURL);
if($parsed_url == null)
return Message::get_error_message("Please, Provide Valid URL.A");
parse_str($parsed_url["query"], $queryParams);
if($queryParams == null || empty($queryParams))
return Message::get_error_message("Please, Provide Valid URL.");
$videoId = $queryParams["v"];
if($videoId == null || empty($videoId))
return Message::get_error_message("Please, Provide valid URL. URL must contain video ID.");
*/
$videoId = GetVideoIDByURL($youtubeURL);
if($videoId == null || empty($videoId))
return Message::get_error_message("Please, Provide valid URL. URL must contain video ID.");
return Message::get_download_message("Success", null, $videoId);
}
else
return Message::get_error_message("Please provide valid YouTube URL.");
return null;
}
function GetMp3DownloadURLFromYoutubeVideo($youtubeURL)
{
$validateURLMessage = ValidateYoutubeURL($youtubeURL);
if($validateURLMessage != null && $validateURLMessage->isOk) {
$videoId = $validateURLMessage->fileName;
$cmd = "youtube-dl -g -f worstaudio \"$youtubeURL\"";
$downloadVideoURL = exec($cmd);
if(empty($downloadVideoURL))
return Message::get_error_message("Can't Find URL To Load audio. Please, try again.");
$fileName = $videoId . ".mp3"; //or webm
return Message::get_download_message("Success", $downloadVideoURL, $fileName);
}
else
return $validateURLMessage;
}
function GetYoutubeVideoDonwloadURLByURL($youtubeURL) {
$validateURLMessage = ValidateYoutubeURL($youtubeURL);
if($validateURLMessage != null && $validateURLMessage->isOk) {
$videoId = $validateURLMessage->fileName;
$cmd = "youtube-dl -g -f best \"$youtubeURL\"";
$downloadVideoURL = exec($cmd);
if(empty($downloadVideoURL))
return Message::get_error_message("Can't Find URL To Load Video. Please, try again.");
$fileName = $videoId . ".mp4"; //or webm
return Message::get_download_message("Success", $downloadVideoURL, $fileName);
}
else
return $validateURLMessage;
}
function GetSrtByURL($url)
{
global $server_captions_save_folder_name;
global $server_captions_save_folder_path;
$validateURLMessage = ValidateYoutubeURL($url);
if($validateURLMessage == null || !$validateURLMessage->isOk)
return $validateURLMessage;
$videoId = $validateURLMessage->fileName;
$foldername = $videoId;
if(file_exists($server_captions_save_folder_path . $foldername))
$foldername = $videoId . "_" . date("YmdHms");
$_i = 0;
while(file_exists($server_captions_save_folder_path . $foldername)) {
$foldername = $videoId . "_" . date("YmdHms") . "_" . i;
$i = $i + 1;
}
$saveFolder_path = $server_captions_save_folder_path . $foldername;
$zipFile_Name = $foldername . ".zip";
$zipFile_Path = $server_captions_save_folder_path . $zipFile_Name;
exec("mkdir \"$saveFolder_path\"");
exec("chmod 777 \"$saveFolder_path\"");
exec("youtube-dl -o \"$saveFolder_path/%(title)s.%(ext)s\" --all-subs --write-auto-sub --skip-download \"$url\"");
if (is_dir_empty($saveFolder_path))
return Message::get_error_message("There are no available captions for requested video.");
exec("zip -r -j \"$zipFile_Path\" \"$saveFolder_path\"");
if(!file_exists($zipFile_Path))
return Message::get_error_message("Something went wrong while compressing captions. Please, Try again.");
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
$zip_link = $actual_link . $server_captions_save_folder_name . $zipFile_Name;
return Message::get_download_message("Success", $zip_link, $zipFile_Name);
}
/**
* @param string $remote_file String, containing the remote file URL.
* @param string $local_file String, containing the path to the file to save the curl result in to.
*/
function download_file($remote_file, $local_file)
{
if(file_put_contents($local_file, file_get_contents($remote_file, 'r')))
return True;
else
return False;
}
function is_dir_empty($dir) {
if (!is_readable($dir))
return NULL;
return (count(scandir($dir)) == 2);
}
function GetVideoIDByURL($url) {
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
if(sizeof($matches) > 1){
return $matches[1];
} else
return null;
}
// we don't use it anymore
function GetYoutubeVideoByURL($youtubeURL) {
// Old logic to fetch load URL
/*
$handler = new YoutubeConverter();
$return = Message::get_info_message("Success!");
// Check whether the url is valid
if(!empty($youtubeURL) && !filter_var($youtubeURL, FILTER_VALIDATE_URL) === false)
{
// Get the downloader object
$downloader = $handler->getDownloader($youtubeURL);
// Set the url
$downloader->setUrl($youtubeURL);
// Validate the youtube video url
if($downloader->hasVideo())
{
// Get the video download link info
$videoDownloadLink = $downloader->getVideoDownloadLink();
if(isset($videoDownloadLink) && $videoDownloadLink != null)
{
$videoTitle = $videoDownloadLink[0]['title'];
$videoQuality = $videoDownloadLink[0]['qualityLabel'];
$videoFormat = $videoDownloadLink[0]['format'];
$videoFileName = strtolower(str_replace(' ', '_', $videoTitle)).'.'.$videoFormat;
$downloadURL = $videoDownloadLink[0]['url'];
$fileName = preg_replace('/[^A-Za-z0-9.\_\-]/', '', basename($videoFileName));
if(!empty($downloadURL))
$return = Message::get_download_message("Success", $downloadURL, $fileName);
else
$return = Message::get_error_message("Cannot Get Video Download Url. Please, Try Again.");
}
else
$return = Message::get_error_message("Cannot Get Video Donwload URL. Please, Try Again.");
}
else
$return = Message::get_error_message("The video is not found, please check YouTube URL.");
} else
return Message::get_error_message("Please provide valid YouTube URL.");
*/
if(!empty($youtubeURL) && !filter_var($youtubeURL, FILTER_VALIDATE_URL) === false)
{
$dl = new YoutubeDl([
'continue' => false, // force resume of partially downloaded files. By default, youtube-dl will resume downloads if possible.
'format' => 'bestvideo+bestaudio',
]);
// For more options go to https://github.com/rg3/youtube-dl#user-content-options
$dl->setDownloadPath('/var/www/html/videos');
// Enable debugging
/*$dl->debug(function ($type, $buffer) {
if (\Symfony\Component\Process\Process::ERR === $type) {
echo 'ERR > ' . $buffer;
} else {
echo 'OUT > ' . $buffer;
}
});*/
try {
$video = $dl->download($youtubeURL);
//echo $video->getTitle(); // Will return Phonebloks
//$video->getFile(); // \SplFileInfo instance of downloaded file
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
$link = $actual_link . "/videos/". $video->getFilename();
chmod("/var/www/html/videos/".$video->getFilename(), 777);
return Message::get_download_message("Video is downloading. You will be redirected to the download page.", $link , $video->getFilename());
} catch (NotFoundException $e) {
return Message::get_error_message("Video not found");
} catch (PrivateVideoException $e) {
return Message::get_error_message("Video is private");
} catch (CopyrightException $e) {
return Message::get_error_message("The YouTube account associated with this video has been terminated due to multiple third-party notifications of copyright infringement");
} catch (Exception $e) {
return Message::get_error_message("Failed to download");
}
return Message::get_error_message("Something goes wrong while downloading video. Please, try again.");
}
else
return Message::get_error_message("Please provide valid YouTube URL.");
return $return;
}
function GetMp3FromYoutubeVideo($youtubeURL)
{
global $server_video_save_folder_name;
global $server_video_save_folder_path;
global $server_ffmpeg_path;
//$youtubeVideoByURLReturnMessage = GetYoutubeVideoByURL($youtubeURL);
$youtubeVideoByURLReturnMessage = GetYoutubeVideoDonwloadURLByURL($youtubeURL);
if($youtubeVideoByURLReturnMessage->isOk == true)
{
$videoURL = $youtubeVideoByURLReturnMessage->downloadUrl;
$videoFileName = $youtubeVideoByURLReturnMessage->fileName;
$localVideoPath = $server_video_save_folder_path . $videoFileName;
//if file is already exists -> then do not ovewrite and create new with datestamp
//if(file_exists($localVideoPath))
// $localVideoPath = $server_video_save_folder_path . date("YmdHms") . "_" . $videoFileName;
$mp3filePath = substr($localVideoPath, 0, strrpos($localVideoPath, '.')) . ".mp3";
// create video file and give all permissions to it
//touch($localVideoPath);
//chmod($localVideoPath, 777);
// download mp4
//$download = download_file($videoURL, $localVideoPath);
//if($download === FALSE)
// return Message::get_error_message("Error While Saving Video On Server. Please, Try Again.");
$logdir = dirname(__FILE__);
// convert mp4 to mp3
//$cmd = "$server_ffmpeg_path -i \"$localVideoPath\" -vn -ar 8000 -ab 64k -y \"$mp3filePath\"";
$cmd = "$server_ffmpeg_path -i \"$videoURL\" -vn -ar 16000 -ab 128k -y \"$mp3filePath\"";
exec($cmd);
//chmod($mp3filePath, 777);
//remove mp4
//unlink($localVideoPath);
//return download mp3 link
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
$mp3_filename = substr($videoFileName, 0, strrpos($videoFileName, '.')) . ".mp3";
$mp3_link = $actual_link . $server_video_save_folder_name . $mp3_filename;
return Message::get_download_message("Success", $mp3_link, $mp3_filename);
}
else
return $youtubeVideoByURLReturnMessage;
}
?>