Selected from Linux Selected from PHP
- Ubuntu 17.10 Install google's mod pagespeed for apache - PHP-Ubuntu-Youtube-dl simple script for converting and downloading youtube video or mp3
- Ubuntu SSH tunnel via proxy - PHP while loop with every row with a different color
- Install GoAccess Apache Log Analyzer Tool on Ubuntu 17.10 - PHP use while loop in function
- SSHFS and map folder with a proxy - PHP List Files And Folders As links
- How to connect to SSH server with a proxy - PHP Extract Text From String
➥ PHP-Ubuntu-Youtube-dl simple script for converting and downloading youtube video or mp3
Added by sonik on 26-06-2018 and keywords: Youtube, mp3, download, convert, script, youtube-dl [ Print Article ]


This is a very simple script for downloading and converting youtube video and play list to mp3 or just simply downloading them.
WHAT DO WE NEED?
Debian linux Ubuntu Youtube-dl apache php
HOW TO INSTALL YOUTUBE-DL?
$ sudo add-apt-repository ppa:nilarimogard/webupd8
$ sudo apt-get update
$ sudo apt-get install youtube-dl

THE SCRIPT:
<?php
#include('functions.php');
#include('header.php');
// FUNCTIONS TO USE //
function extract_unit($string, $start, $end){
$pos = stripos($string, $start);
$str = substr($string, $pos);
$str_two = substr($str, strlen($start));
$second_pos = stripos($str_two, $end);
$str_three = substr($str_two, 0, $second_pos);
$unit = trim($str_three); // remove whitespaces
return $unit;
}
function rand_chars($c, $l, $u = FALSE) {
if (!$u) for ($s = '', $i = 0, $z = strlen($c)-1; $i < $l; $x = rand(0,$z), $s .= $c{$x}, $i++);
else for ($i = 0, $z = strlen($c)-1, $s = $c{rand(0,$z)}, $i = 1; $i != $l; $x = rand(0,$z), $s .= $c{$x}, $s = ($s{$i} == $s{$i-1} ? substr($s,0,-1) : $s), $i=strlen($s));
return $s;
}
// -------------------------- //
$youtubeVideoLink = filter_input(INPUT_POST, 'youtubeVideoLink');
$downloadType = filter_input(INPUT_POST, 'downloadType');
$form_submit = filter_input(INPUT_POST, 'form_submit');

if($downloadType == 1){
$randomnumber = rand_chars("ABCEDFG", 10);
$outputYoutubeDL = shell_exec('youtube-dl -4 --no-playlist -o "/var/www/html/mp3/%(title)s-'.$randomnumber.'.%(ext)s" --extract-audio --audio-format mp3 '.$youtubeVideoLink.'');
$downloadLink = extract_unit($outputYoutubeDL, '[ffmpeg] Destination: /var/www/html/mp3/', "Deleting original");
}
if($downloadType == 2){
$randomnumber = rand_chars("ABCEDFG", 10);
$outputYoutubeDL = shell_exec('youtube-dl -o "/var/www/html/mp3/%(uploader)s%(title)s-'.$randomnumber.'.%(ext)s" '.$youtubeVideoLink.'');
$downloadLink = extract_unit($outputYoutubeDL, '[ffmpeg] Merging formats into "/var/www/html/mp3/', "Deleting original");
}
?>
<div class=topic-title>Convert Youtube video to .mp3</div>
<div class=topic-content><div style="background: #718693; margin:auto; margin-top: 10px; width: 80%; padding: 15px; border-radius: 10px; color: #FFFFFF;"><form method="POST" action="#"><center>
<?php
if($form_submit == 1){
//echo '<pre>'.$outputYoutubeDL.'</pre>';
//echo $downloadLink;

// Here we get the v= from the youtube link to put it in iframe embeded //
$uno = explode("v=", $youtubeVideoLink); //
$embeded = explode("&", $uno[1]); //
// -------------------------------------------------------------------- //

echo '<div class="videoWrapper">';
echo '<iframe width="560" height="315" src="https://www.youtube.com/embed/'.$embeded[0].'" frameborder="0" allowfullscreen></iframe><hr>';
echo '</div>';
if(empty($downloadLink)){
echo '<font color="red"><h2><b>The link cannot be downloaded plase try different link</b></h2></font><br/>';
}else{
echo '<a href="mp3/'.$downloadLink.'"><font color="red"><h3><b>DOWNLOAD: [ '.$downloadLink.' ]</b></h3></font></a><hr>';
}
}else{
echo '<a href="http://webleit.info/youtube.php"><img src="http://webleit.info/img/youtube-logo.png" alt="" /></a>';
}
?>
<style>
.videoWrapper{position: relative;padding-bottom: 56.25%;padding-top: 25px;height: 0;}.videoWrapper iframe{position:absolute;top: 0;left: 0;width: 100%;height: 100%;}
</style>
<form action="#" method="POST">
<label for="youtubeVideoLink">Add Youtube video to convert:</label>
<input type="text" onfocus="if (this.value=='https://www.youtube.com/watch?v=9RhUcNSAYbc') this.value = ''" value="https://www.youtube.com/watch?v=9RhUcNSAYbc" name="youtubeVideoLink" />
<input type="hidden" name="form_submit" value="1" />
<hr>
<select name="downloadType">
<option value="1">Convert to mp3 audio only</option>
<option value="2">Download video</option>
</select>
<hr>
<input type="submit" value="Download" /><hr>
</form>
<a href="/mp3/"><font color="white">See other mp3 and movie downlaods by our users</font></a><br />
<a href="http://www.webleit.info/forumpost.php?forum_posts_id=36&forum_cat_id=2&forum_name=PHP-Tutorials-Discussion"><font color="white">Download PHP SCRIPT By Webleit.info</font></a><br />
<?php $ver = shell_exec('youtube-dl --version'); echo 'Youtube-dl version: '.$ver; ?>
</center></div></div>
<?php
#include('footer.php');

@All rights reserved with Copyright Webleit.info 2018 by Sonik_Blast [ Site Map ] [ RSS ] [ YouTube Mp3 Downloader and converter ]