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.
Debian linux Ubuntu Youtube-dl apache php
$ sudo add-apt-repository ppa:nilarimogard/webupd8
$ sudo apt-get update
$ sudo apt-get install youtube-dl

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>
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="'.$embeded[0].'" frameborder="0" allowfullscreen></iframe><hr>';
echo '</div>';
echo '<font color="red"><h2><b>The link cannot be downloaded plase try different link</b></h2></font><br/>';
echo '<a href="mp3/'.$downloadLink.'"><font color="red"><h3><b>DOWNLOAD: [ '.$downloadLink.' ]</b></h3></font></a><hr>';
echo '<a href=""><img src="" alt="" /></a>';
.videoWrapper{position: relative;padding-bottom: 56.25%;padding-top: 25px;height: 0;}.videoWrapper iframe{position:absolute;top: 0;left: 0;width: 100%;height: 100%;}
<form action="#" method="POST">
<label for="youtubeVideoLink">Add Youtube video to convert:</label>
<input type="text" onfocus="if (this.value=='') this.value = ''" value="" name="youtubeVideoLink" />
<input type="hidden" name="form_submit" value="1" />
<select name="downloadType">
<option value="1">Convert to mp3 audio only</option>
<option value="2">Download video</option>
<input type="submit" value="Download" /><hr>
<a href="/mp3/"><font color="white">See other mp3 and movie downlaods by our users</font></a><br />
<a href=""><font color="white">Download PHP SCRIPT By</font></a><br />
<?php $ver = shell_exec('youtube-dl --version'); echo 'Youtube-dl version: '.$ver; ?>

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