Marc Becker
Marc Becker

Reputation: 553

YouTube-dl server direct file download

I have succesfully installed youtube-dl on my server and from command line everything is working like a charm.

Now I want to be able to call a link on my site from my web browser which directly initiates a download of the file. So in that order:

  1. Open site in browser (for example http://example.com/download.php?v=as43asx3

  2. YouTube-dl processes the input

  3. Web-browser downloads file

  4. Temporary files will be deleted from server

I am not very experienced with this, but nevertheless need to solve this issue.

Upvotes: 0

Views: 2730

Answers (1)

Marc Becker
Marc Becker

Reputation: 553

There might be a better way of doing this and I would appreciate seeing it, however, here is how I solved it:

<?php
ignore_user_abort(true);

//getting ID from URL
$youtubeID = $_GET['videoID'] ;

//getting audio file from youtube video via youtube-dl
exec('~/bin/youtube-dl --verbose --extract-audio -o "~/html/YouTubeDownloader/%(id)s.%(ext)s" '.$youtubeID);

//downloading file to client/browser
$filename = $youtubeID.".m4a";
header("Content-disposition: attachment;filename=$filename");
header("Content-type: audio/m4a");
readfile($filename);

//deleting file again
unlink($filename);?>

Upvotes: 1

Related Questions