Skip to content

Commit

Permalink
Ability to convert to mp4
Browse files Browse the repository at this point in the history
Merge pull request #12 from MichaelBelgium/convert-to-mp4
  • Loading branch information
MichaelBelgium authored May 3, 2020
2 parents 8ccb4ae + 266ce74 commit 3a843e8
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode/
vendor/
download/*.mp3
download/*.mp4
composer.lock
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ See the wiki for examples.

## `GET - convert.php`

### *Convert a video*

| Parameter | Required | Type | Description |
|-----------|----------|-------------|-------------|
| youtubelink | Yes | string | The full youtubelink of the video you want to download. |
| youtubelink | Yes | string | The full youtubelink of the video you want to download. |
| format | No (default = mp3) | string: mp3 or mp4 | The format to download |
| delete | No | string | The youtubeid of which you want it to be deleted from storage on the server |

### __Possible youtubelinks__
### *Delete a downloaded video*

| Parameter | Required | Type | Description |
|-----------|----------|-------------|-------------|
| delete | Yes | string | The youtubeid that has to be deleted from storage on the server |
| format | No (default = mp3 & mp4) | string: mp3 or mp4, leave empty to remove all | The format of the video that has to be deleted |

### Possible youtubelinks
```
youtube.com/v/{vidid}
youtube.com/vi/{vidid}
Expand Down Expand Up @@ -82,14 +92,19 @@ First we install the dependencies on the server, then website.

### `search.php`
```PHP
define("MAX_RESULTS", 10);
define("API_KEY", "");
define("MAX_RESULTS", 10); //max search results
define("API_KEY", ""); //google api key
```

### `convert.php`

```PHP
define("DOWNLOAD_MAX_LENGTH", 0); //max video duration (in seconds) to be able to download, set to 0 to disable
```

### `convert.php`

```PHP
define("DOWNLOAD_FOLDER", dirname(__FILE__)."/download/"); //the folder where files are accessable to download
define("DOWNLOAD_FOLDER_PUBLIC", "http://michaelbelgium.me/ytconverter/download/");
define("DOWNLOAD_FOLDER", "download/"); //the folder where files are accessable to download
define("DOWNLOAD_MAX_LENGTH", 0); //max video duration (in seconds) to be able to download, set to 0 to disable
```
```
90 changes: 68 additions & 22 deletions convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@

use YoutubeDl\YoutubeDl;

define("DOWNLOAD_FOLDER", __DIR__."/download/"); //Be sure the chmod the download folder
define("DOWNLOAD_FOLDER_PUBLIC", "http://michaelbelgium.me/ytconverter/download/");
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

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);

Expand All @@ -20,14 +25,14 @@

$id = $matches[0];

$localfile = DOWNLOAD_FOLDER.$id.".mp3";
$exists = file_exists($localfile);
$exists = file_exists(DOWNLOAD_FOLDER.$id.".".$format);

if(DOWNLOAD_MAX_LENGTH > 0 || $exists) {
if(DOWNLOAD_MAX_LENGTH > 0 || $exists)
{
$dl = new YoutubeDl(['skip-download' => true]);
$dl->setDownloadPath(DOWNLOAD_FOLDER);

try {
try {
$video = $dl->download($youtubelink);

if($video->getDuration() > DOWNLOAD_MAX_LENGTH && DOWNLOAD_MAX_LENGTH > 0)
Expand All @@ -40,26 +45,40 @@
}

if(!$exists)
{
$dl = new YoutubeDl(array(
'extract-audio' => true,
'audio-format' => 'mp3',
'audio-quality' => 0,
'output' => '%(id)s.%(ext)s',
//'ffmpeg-location' => '/usr/local/bin/ffmpeg'
));
{
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
{
$video = $dl->download($youtubelink);

$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]/download/";
if($exists)
$file = DOWNLOAD_FOLDER_PUBLIC.$id.".mp3";
else
$file = DOWNLOAD_FOLDER_PUBLIC.$video->getFilename();
$file = $url.$id.".".$format;
else
{
$video = $dl->download($youtubelink);
$file = $url.$video->getFilename();
}

echo json_encode(array(
"error" => false,
Expand All @@ -79,11 +98,38 @@
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 = [];

if(unlink(DOWNLOAD_FOLDER.$id.".mp3"))
echo json_encode(array("error" => false, "message" => "File removed"));
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
echo json_encode(array("error" => true, "message" => "File not found"));
$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"));
Expand Down
2 changes: 1 addition & 1 deletion download/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
In this folder the downloadable mp3's will be placed. Make sure that this folder is writable.
In this folder the downloadable files will be placed. Make sure that this folder is writable for the webserver.
61 changes: 35 additions & 26 deletions index.html → index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ <h5 class="card-title">Youtube to mp3</h5>
<div class="form-group">
<input type="text" name="youtubelink" class="form-control" id="link" placeholder="Youtube url" required />
</div>
<div class="form-group">
<label for="format">Format</label>
<select class="form-control" name="format" id="format">
<option value="mp3">Audio (mp3)</option>
<option value="mp4">Video (mp4)</option>
</select>
</div>
<button type="submit" class="btn btn-outline-primary"><i class="fa fa-refresh" aria-hidden="true"></i> Convert</button>
</form>
</div>
Expand All @@ -36,28 +43,31 @@ <h5 class="card-title">Json response</h5>
<tbody>
<tr>
<td>Error:</td>
<td><span id="error"><i class="fa fa-times" aria-hidden="true"></i></span></td>
<td><i class="fa fa-times" aria-hidden="true"></i></td>
</tr>
<tr>
<td>Error message:</td>
<td><span id="error-message">-</span></td>
<td>-</td>
</tr>
<tr><td colspan="2"></td></tr>
<tr>
<td>Title:</td>
<td><span id="title">-</span></td>
<td>-</td>
</tr>
<tr>
<td>Duration</td>
<td><span id="duration">0</span> seconds</td>
</tr>
<tr>
<td>Other</td>
<td><span id="other">-</span></td>
<td>Youtube ID</td>
<td></td>
</tr>
<tr>
<td>Uploaded at</td>
<td></td>
</tr>
<tr>
<td>
<a class="btn btn-outline-primary disabled" href="#" id="download"><i class="fa fa-cloud-download" aria-hidden="true"></i> Listen/download</a>
<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>
<a class="btn btn-outline-danger disabled" href="#" id="remove" data-id="">Remove</a>
</td>
<td></td>
Expand All @@ -73,34 +83,33 @@ <h5 class="card-title">Json response</h5>
<script>
$(document).ready(function() {
$("#frm-convert").submit(function(e) {
$("#frm-convert button[type=submit]").html("<i class=\"fa fa-refresh\" aria-hidden=\"true\"></i> Converting... Please wait");
$("#frm-convert button[type=submit]").html("<i class=\"fa fa-spin fa-refresh\" aria-hidden=\"true\"></i> Converting... Please wait");
e.preventDefault();
$.get($(this).attr("action"), { youtubelink: $("#link").val() }, function(data) {
$.get($(this).attr("action"), { youtubelink: $('#link').val(), format: $('#format').val() }, function(data) {
$("pre").text(JSON.stringify(data, null, 4));
$("#frm-convert button[type=submit]").html("<i class=\"fa fa-refresh\" aria-hidden=\"true\"></i> Convert");
if(data.error) {
$("#error").html("<i class=\"fa fa-check\" aria-hidden=\"true\"></i>");
$("#error-message").text(data.message);

$("#duration").text(0);
$("#title").text("-");
$("#other").text("-");
$("#download").attr("href", "#");
$("#download").addClass("disabled");
$("table tr:eq(0) td:last").html("<i class=\"fa fa-check\" aria-hidden=\"true\"></i>");
$("table tr:eq(1) td:last").text(data.message);
$("table tr:eq(2) td:last").text("-");
$("table tr:eq(3) td:last").text(0);
$("table tr:eq(4) td:last").text("-");
$("table tr:eq(5) td:last").text("-");
$("#download").attr("href", "#").addClass("disabled");
$("#remove").addClass("disabled");
} else {
$("#error").html("<i class=\"fa fa-times\" aria-hidden=\"true\"></i>");
$("#error-message").text("-");
$("table tr:eq(0) td:last").html("<i class=\"fa fa-times\" aria-hidden=\"true\"></i>");
$("table tr:eq(1) td:last").text("-");
$("table tr:eq(2) td:last").text(data.title + " (" + data.alt_title + ")");
$("table tr:eq(3) td:last").text(data.duration);
$("table tr:eq(4) td:last").text(data.youtube_id);
$("table tr:eq(5) td:last").text(new Date(data.uploaded_at.date));
$("#duration").text(data.duration);
$("#title").text(data.title + " (" + data.alt_title + ")");
$("#download").attr("href", data.file);
$("#download").removeClass("disabled");
$("#remove").removeClass("disabled");
$("#remove").data("id", data.youtube_id);
$("#other").html("Youtube id: " + data.youtube_id + "<br/>Size: " + data.file_size + " bytes<br/>Uploaded at: " + data.uploaded_at.date);
$("#download").attr("href", data.file).removeClass("disabled");
$("#remove").removeClass("disabled").data("id", data.youtube_id);
}
});
});
Expand Down

0 comments on commit 3a843e8

Please sign in to comment.