forked from MichaelBelgium/Youtube-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.php
More file actions
147 lines (120 loc) · 3.67 KB
/
Copy pathconvert.php
File metadata and controls
147 lines (120 loc) · 3.67 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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use YoutubeDl\YoutubeDl;
define("DOWNLOAD_FOLDER", "download/"); //Be sure the chmod the download folder
define("DOWNLOAD_MAX_LENGTH", 0); //max video duration (in seconds) to be able to download, set to 0 to disable
define("LOG", false); //enable logging
header("Content-Type: application/json");
const POSSIBLE_FORMATS = ['mp3', 'mp4'];
if(isset($_GET["youtubelink"]) && !empty($_GET["youtubelink"]))
{
$youtubelink = $_GET["youtubelink"];
$format = $_GET['format'] ?? 'mp3';
if(!in_array($format, POSSIBLE_FORMATS))
die(json_encode(array("error" => true, "message" => "Invalid format: only mp3 or mp4 are possible")));
$success = preg_match('#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#', $youtubelink, $matches);
if(!$success)
die(json_encode(array("error" => true, "message" => "No video specified")));
$id = $matches[0];
$exists = file_exists(DOWNLOAD_FOLDER.$id.".".$format);
if(DOWNLOAD_MAX_LENGTH > 0 || $exists)
{
$dl = new YoutubeDl(['skip-download' => true]);
$dl->setDownloadPath(DOWNLOAD_FOLDER);
try {
$video = $dl->download($youtubelink);
if($video->getDuration() > DOWNLOAD_MAX_LENGTH && DOWNLOAD_MAX_LENGTH > 0)
throw new Exception("The duration of the video is {$video->getDuration()} seconds while max video length is ".DOWNLOAD_MAX_LENGTH." seconds.");
}
catch (Exception $ex)
{
die(json_encode(array("error" => true, "message" => $ex->getMessage())));
}
}
if(!$exists)
{
if($format == 'mp3')
{
$options = array(
'extract-audio' => true,
'audio-format' => 'mp3',
'audio-quality' => 0,
'output' => '%(id)s.%(ext)s',
//'ffmpeg-location' => '/usr/local/bin/ffmpeg'
);
}
else
{
$options = array(
'continue' => true,
'format' => 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'output' => '%(id)s.%(ext)s'
);
}
$dl = new YoutubeDl($options);
$dl->setDownloadPath(DOWNLOAD_FOLDER);
}
try
{
$url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/".DOWNLOAD_FOLDER;
if($exists)
$file = $url.$id.".".$format;
else
{
$video = $dl->download($youtubelink);
$file = $url.$video->getFilename();
}
$json = json_encode(array(
"error" => false,
"youtube_id" => $video->getId(),
"title" => $video->getTitle(),
"alt_title" => $video->getAltTitle(),
"duration" => $video->getDuration(),
"file" => $file,
"uploaded_at" => $video->getUploadDate()
));
if(LOG)
{
$now = new DateTime();
$file = fopen('logs/'.$now->format('Ymd').'.log', 'a');
fwrite($file, '[' . $now->format('H:i:s') . '] ' . $json . PHP_EOL);
fclose($file);
}
echo $json;
}
catch (Exception $e)
{
echo json_encode(array("error" => true, "message" => $e->getMessage()));
}
}
else if(isset($_GET["delete"]) && !empty($_GET["delete"]))
{
$id = $_GET["delete"];
$format = $_GET["format"] ?? POSSIBLE_FORMATS;
if(empty($format))
$format = POSSIBLE_FORMATS;
if(!is_array($format))
$format = [$format];
$removedFiles = [];
foreach($format as $f) {
$localFile = DOWNLOAD_FOLDER.$id.".".$f;
if(file_exists($localFile)) {
unlink($localFile);
$removedFiles[] = $f;
}
}
$resultNotRemoved = array_diff(POSSIBLE_FORMATS, $removedFiles);
if(empty($removedFiles))
$message = 'No files removed.';
else
$message = 'Removed files: ' . implode(', ', $removedFiles) . '.';
if(!empty($resultNotRemoved))
$message .= ' Not removed: ' . implode(', ', $resultNotRemoved);
echo json_encode(array(
"error" => false,
"message" => $message
));
}
else
echo json_encode(array("error" => true, "message" => "Invalid request"));
?>