-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
convert.php
169 lines (138 loc) · 4.91 KB
/
convert.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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use MichaelBelgium\YoutubeConverter\Config;
use YoutubeDl\Options;
use YoutubeDl\YoutubeDl;
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))
{
http_response_code(400);
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)
{
http_response_code(400);
die(json_encode(array("error" => true, "message" => "No video specified")));
}
$id = $matches[0];
$exists = file_exists(Config::DOWNLOAD_FOLDER.$id.".".$format);
if(Config::DOWNLOAD_MAX_LENGTH > 0 || $exists)
{
try {
$dl = new YoutubeDl();
$video = $dl->download(
Options::create()
->noPlaylist()
->proxy(Config::PROXY)
->skipDownload(true)
->downloadPath(Config::DOWNLOAD_FOLDER)
->url($youtubelink)
)->getVideos()[0];
if ($video->getError() !== null)
throw new Exception($video->getError());
if($video->getDuration() > Config::DOWNLOAD_MAX_LENGTH && Config::DOWNLOAD_MAX_LENGTH > 0)
throw new Exception("The duration of the video is {$video->getDuration()} seconds while max video length is ".Config::DOWNLOAD_MAX_LENGTH." seconds.");
}
catch (Exception $ex)
{
http_response_code(400);
die(json_encode(array("error" => true, "message" => $ex->getMessage())));
}
}
if(!$exists)
{
$options = Options::create()
// ->ffmpegLocation('/usr/local/bin/ffmpeg')
->output('%(id)s.%(ext)s')
->downloadPath(Config::DOWNLOAD_FOLDER)
->proxy(Config::PROXY)
->noPlaylist()
->url($youtubelink);
if($format == 'mp3')
{
$options = $options->extractAudio(true)
->audioFormat('mp3')
->audioQuality('0');
}
else
$options = $options->format('bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best');
}
try
{
$dirname = dirname($_SERVER['PHP_SELF']);
$url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] .
($dirname == '/' ? '' : $dirname) . "/";
if($exists)
$file = $url.Config::DOWNLOAD_FOLDER.$video->getId().'.'.$format;
else
{
$dl = new YoutubeDl();
$video = $dl->download($options)->getVideos()[0];
if($video->getError() !== null)
die(json_encode(array("error" => true, "message" => $video->getError())));
$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(Config::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)
{
http_response_code(400);
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 = Config::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
{
http_response_code(400);
echo json_encode(array("error" => true, "message" => "Invalid request: missing 'youtubelink' parameter"));
}
?>