Skip to content

Commit 3a843e8

Browse files
Ability to convert to mp4
Merge pull request #12 from MichaelBelgium/convert-to-mp4
2 parents 8ccb4ae + 266ce74 commit 3a843e8

File tree

5 files changed

+127
-56
lines changed

5 files changed

+127
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode/
22
vendor/
33
download/*.mp3
4+
download/*.mp4
45
composer.lock

README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ See the wiki for examples.
77

88
## `GET - convert.php`
99

10+
### *Convert a video*
11+
1012
| Parameter | Required | Type | Description |
1113
|-----------|----------|-------------|-------------|
12-
| youtubelink | Yes | string | The full youtubelink of the video you want to download. |
14+
| youtubelink | Yes | string | The full youtubelink of the video you want to download. |
15+
| format | No (default = mp3) | string: mp3 or mp4 | The format to download |
1316
| delete | No | string | The youtubeid of which you want it to be deleted from storage on the server |
1417

15-
### __Possible youtubelinks__
18+
### *Delete a downloaded video*
19+
20+
| Parameter | Required | Type | Description |
21+
|-----------|----------|-------------|-------------|
22+
| delete | Yes | string | The youtubeid that has to be deleted from storage on the server |
23+
| format | No (default = mp3 & mp4) | string: mp3 or mp4, leave empty to remove all | The format of the video that has to be deleted |
24+
25+
### Possible youtubelinks
1626
```
1727
youtube.com/v/{vidid}
1828
youtube.com/vi/{vidid}
@@ -82,14 +92,19 @@ First we install the dependencies on the server, then website.
8292

8393
### `search.php`
8494
```PHP
85-
define("MAX_RESULTS", 10);
86-
define("API_KEY", "");
95+
define("MAX_RESULTS", 10); //max search results
96+
define("API_KEY", ""); //google api key
97+
```
98+
99+
### `convert.php`
100+
101+
```PHP
102+
define("DOWNLOAD_MAX_LENGTH", 0); //max video duration (in seconds) to be able to download, set to 0 to disable
87103
```
88104

89105
### `convert.php`
90106

91107
```PHP
92-
define("DOWNLOAD_FOLDER", dirname(__FILE__)."/download/"); //the folder where files are accessable to download
93-
define("DOWNLOAD_FOLDER_PUBLIC", "http://michaelbelgium.me/ytconverter/download/");
108+
define("DOWNLOAD_FOLDER", "download/"); //the folder where files are accessable to download
94109
define("DOWNLOAD_MAX_LENGTH", 0); //max video duration (in seconds) to be able to download, set to 0 to disable
95-
```
110+
```

convert.php

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33

44
use YoutubeDl\YoutubeDl;
55

6-
define("DOWNLOAD_FOLDER", __DIR__."/download/"); //Be sure the chmod the download folder
7-
define("DOWNLOAD_FOLDER_PUBLIC", "http://michaelbelgium.me/ytconverter/download/");
6+
define("DOWNLOAD_FOLDER", "download/"); //Be sure the chmod the download folder
87
define("DOWNLOAD_MAX_LENGTH", 0); //max video duration (in seconds) to be able to download, set to 0 to disable
98

109
header("Content-Type: application/json");
1110

11+
const POSSIBLE_FORMATS = ['mp3', 'mp4'];
12+
1213
if(isset($_GET["youtubelink"]) && !empty($_GET["youtubelink"]))
1314
{
1415
$youtubelink = $_GET["youtubelink"];
16+
$format = $_GET['format'] ?? 'mp3';
17+
18+
if(!in_array($format, POSSIBLE_FORMATS))
19+
die(json_encode(array("error" => true, "message" => "Invalid format: only mp3 or mp4 are possible")));
1520

1621
$success = preg_match('#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#', $youtubelink, $matches);
1722

@@ -20,14 +25,14 @@
2025

2126
$id = $matches[0];
2227

23-
$localfile = DOWNLOAD_FOLDER.$id.".mp3";
24-
$exists = file_exists($localfile);
28+
$exists = file_exists(DOWNLOAD_FOLDER.$id.".".$format);
2529

26-
if(DOWNLOAD_MAX_LENGTH > 0 || $exists) {
30+
if(DOWNLOAD_MAX_LENGTH > 0 || $exists)
31+
{
2732
$dl = new YoutubeDl(['skip-download' => true]);
2833
$dl->setDownloadPath(DOWNLOAD_FOLDER);
2934

30-
try {
35+
try {
3136
$video = $dl->download($youtubelink);
3237

3338
if($video->getDuration() > DOWNLOAD_MAX_LENGTH && DOWNLOAD_MAX_LENGTH > 0)
@@ -40,26 +45,40 @@
4045
}
4146

4247
if(!$exists)
43-
{
44-
$dl = new YoutubeDl(array(
45-
'extract-audio' => true,
46-
'audio-format' => 'mp3',
47-
'audio-quality' => 0,
48-
'output' => '%(id)s.%(ext)s',
49-
//'ffmpeg-location' => '/usr/local/bin/ffmpeg'
50-
));
48+
{
49+
if($format == 'mp3')
50+
{
51+
$options = array(
52+
'extract-audio' => true,
53+
'audio-format' => 'mp3',
54+
'audio-quality' => 0,
55+
'output' => '%(id)s.%(ext)s',
56+
//'ffmpeg-location' => '/usr/local/bin/ffmpeg'
57+
);
58+
}
59+
else
60+
{
61+
$options = array(
62+
'continue' => true,
63+
'format' => 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
64+
'output' => '%(id)s.%(ext)s'
65+
);
66+
}
5167

68+
$dl = new YoutubeDl($options);
5269
$dl->setDownloadPath(DOWNLOAD_FOLDER);
5370
}
5471

5572
try
5673
{
57-
$video = $dl->download($youtubelink);
58-
74+
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]/download/";
5975
if($exists)
60-
$file = DOWNLOAD_FOLDER_PUBLIC.$id.".mp3";
61-
else
62-
$file = DOWNLOAD_FOLDER_PUBLIC.$video->getFilename();
76+
$file = $url.$id.".".$format;
77+
else
78+
{
79+
$video = $dl->download($youtubelink);
80+
$file = $url.$video->getFilename();
81+
}
6382

6483
echo json_encode(array(
6584
"error" => false,
@@ -79,11 +98,38 @@
7998
else if(isset($_GET["delete"]) && !empty($_GET["delete"]))
8099
{
81100
$id = $_GET["delete"];
101+
$format = $_GET["format"] ?? POSSIBLE_FORMATS;
102+
103+
if(empty($format))
104+
$format = POSSIBLE_FORMATS;
105+
106+
if(!is_array($format))
107+
$format = [$format];
108+
109+
$removedFiles = [];
82110

83-
if(unlink(DOWNLOAD_FOLDER.$id.".mp3"))
84-
echo json_encode(array("error" => false, "message" => "File removed"));
111+
foreach($format as $f) {
112+
$localFile = DOWNLOAD_FOLDER.$id.".".$f;
113+
if(file_exists($localFile)) {
114+
unlink($localFile);
115+
$removedFiles[] = $f;
116+
}
117+
}
118+
119+
$resultNotRemoved = array_diff(POSSIBLE_FORMATS, $removedFiles);
120+
121+
if(empty($removedFiles))
122+
$message = 'No files removed.';
85123
else
86-
echo json_encode(array("error" => true, "message" => "File not found"));
124+
$message = 'Removed files: ' . implode(', ', $removedFiles) . '.';
125+
126+
if(!empty($resultNotRemoved))
127+
$message .= ' Not removed: ' . implode(', ', $resultNotRemoved);
128+
129+
echo json_encode(array(
130+
"error" => false,
131+
"message" => $message
132+
));
87133
}
88134
else
89135
echo json_encode(array("error" => true, "message" => "Invalid request"));

download/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
In this folder the downloadable mp3's will be placed. Make sure that this folder is writable.
1+
In this folder the downloadable files will be placed. Make sure that this folder is writable for the webserver.

index.html renamed to index.php

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ <h5 class="card-title">Youtube to mp3</h5>
1919
<div class="form-group">
2020
<input type="text" name="youtubelink" class="form-control" id="link" placeholder="Youtube url" required />
2121
</div>
22+
<div class="form-group">
23+
<label for="format">Format</label>
24+
<select class="form-control" name="format" id="format">
25+
<option value="mp3">Audio (mp3)</option>
26+
<option value="mp4">Video (mp4)</option>
27+
</select>
28+
</div>
2229
<button type="submit" class="btn btn-outline-primary"><i class="fa fa-refresh" aria-hidden="true"></i> Convert</button>
2330
</form>
2431
</div>
@@ -36,28 +43,31 @@ <h5 class="card-title">Json response</h5>
3643
<tbody>
3744
<tr>
3845
<td>Error:</td>
39-
<td><span id="error"><i class="fa fa-times" aria-hidden="true"></i></span></td>
46+
<td><i class="fa fa-times" aria-hidden="true"></i></td>
4047
</tr>
4148
<tr>
4249
<td>Error message:</td>
43-
<td><span id="error-message">-</span></td>
50+
<td>-</td>
4451
</tr>
45-
<tr><td colspan="2"></td></tr>
4652
<tr>
4753
<td>Title:</td>
48-
<td><span id="title">-</span></td>
54+
<td>-</td>
4955
</tr>
5056
<tr>
5157
<td>Duration</td>
5258
<td><span id="duration">0</span> seconds</td>
5359
</tr>
5460
<tr>
55-
<td>Other</td>
56-
<td><span id="other">-</span></td>
61+
<td>Youtube ID</td>
62+
<td></td>
63+
</tr>
64+
<tr>
65+
<td>Uploaded at</td>
66+
<td></td>
5767
</tr>
5868
<tr>
5969
<td>
60-
<a class="btn btn-outline-primary disabled" href="#" id="download"><i class="fa fa-cloud-download" aria-hidden="true"></i> Listen/download</a>
70+
<a target="_blank" class="btn btn-outline-primary disabled" href="#" id="download"><i class="fa fa-cloud-download" aria-hidden="true"></i> Listen/download</a>
6171
<a class="btn btn-outline-danger disabled" href="#" id="remove" data-id="">Remove</a>
6272
</td>
6373
<td></td>
@@ -73,34 +83,33 @@ <h5 class="card-title">Json response</h5>
7383
<script>
7484
$(document).ready(function() {
7585
$("#frm-convert").submit(function(e) {
76-
$("#frm-convert button[type=submit]").html("<i class=\"fa fa-refresh\" aria-hidden=\"true\"></i> Converting... Please wait");
86+
$("#frm-convert button[type=submit]").html("<i class=\"fa fa-spin fa-refresh\" aria-hidden=\"true\"></i> Converting... Please wait");
7787
7888
e.preventDefault();
79-
$.get($(this).attr("action"), { youtubelink: $("#link").val() }, function(data) {
89+
$.get($(this).attr("action"), { youtubelink: $('#link').val(), format: $('#format').val() }, function(data) {
8090
$("pre").text(JSON.stringify(data, null, 4));
8191
$("#frm-convert button[type=submit]").html("<i class=\"fa fa-refresh\" aria-hidden=\"true\"></i> Convert");
8292
8393
if(data.error) {
84-
$("#error").html("<i class=\"fa fa-check\" aria-hidden=\"true\"></i>");
85-
$("#error-message").text(data.message);
86-
87-
$("#duration").text(0);
88-
$("#title").text("-");
89-
$("#other").text("-");
90-
$("#download").attr("href", "#");
91-
$("#download").addClass("disabled");
94+
$("table tr:eq(0) td:last").html("<i class=\"fa fa-check\" aria-hidden=\"true\"></i>");
95+
$("table tr:eq(1) td:last").text(data.message);
96+
$("table tr:eq(2) td:last").text("-");
97+
$("table tr:eq(3) td:last").text(0);
98+
$("table tr:eq(4) td:last").text("-");
99+
$("table tr:eq(5) td:last").text("-");
100+
101+
$("#download").attr("href", "#").addClass("disabled");
92102
$("#remove").addClass("disabled");
93103
} else {
94-
$("#error").html("<i class=\"fa fa-times\" aria-hidden=\"true\"></i>");
95-
$("#error-message").text("-");
104+
$("table tr:eq(0) td:last").html("<i class=\"fa fa-times\" aria-hidden=\"true\"></i>");
105+
$("table tr:eq(1) td:last").text("-");
106+
$("table tr:eq(2) td:last").text(data.title + " (" + data.alt_title + ")");
107+
$("table tr:eq(3) td:last").text(data.duration);
108+
$("table tr:eq(4) td:last").text(data.youtube_id);
109+
$("table tr:eq(5) td:last").text(new Date(data.uploaded_at.date));
96110
97-
$("#duration").text(data.duration);
98-
$("#title").text(data.title + " (" + data.alt_title + ")");
99-
$("#download").attr("href", data.file);
100-
$("#download").removeClass("disabled");
101-
$("#remove").removeClass("disabled");
102-
$("#remove").data("id", data.youtube_id);
103-
$("#other").html("Youtube id: " + data.youtube_id + "<br/>Size: " + data.file_size + " bytes<br/>Uploaded at: " + data.uploaded_at.date);
111+
$("#download").attr("href", data.file).removeClass("disabled");
112+
$("#remove").removeClass("disabled").data("id", data.youtube_id);
104113
}
105114
});
106115
});

0 commit comments

Comments
 (0)