How to Download an Mp3 File from Remote Url

How to download an mp3 file from remote url?

Use CURL to download, then you can use file_get_contents to save file on server, or you can use some headers to force download the file.

$ch = curl_init('http://url-to-file.com/audio.mp3');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
file_put_contents(dirname(__FILE__) . '/audio.mp3', $output);
}

Download to browser:

$ch = curl_init('http://url-to-file.com/audio.mp3');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status == 200) {
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=audio.mp3");
echo $output;
die();
}

Downloading Mp3 file from remote. Node js

The problem here is that http doesn't follow redirects.
You can use the request npm module that does it by default to avoid handling headers yourself.

var fs = require('fs'),
request = require('request');

request
.get('http://foo.com/bar.mp3')
.on('error', function(err) {
// handle error
})
.pipe(fs.createWriteStream('2.mp3'));

How to download a part of mp3 from server?

Its is not possible the way you want to do it. mp3 files do not have timestamps. If you just jump to the middle of an mp3, (and look for the frame start marker), then start decoding, You have no idea at what time this frame is for, because frames are variable size. The only way to know, is to count the number of frames before the current position. Which means you need the whole file.

php script to download files from a remote URL to the user's computer directly

You could achieve this by using the following PHP:

$file_name = $_GET['filename'];
$file_url = 'http://path_to_audio_folder.com/' . $file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;

You could then call it from your script like: download.php?filename=my_song.mp3 and it would be forced as a download to your visitor clicking on it.

getid3: download the full mp3 data from remote server

ID3v1 tags are at the end of the file, not at the beginning. Therefore, you won't be able to read them until you either download a chunk of the end of the file (with range requests done with cURL), or download the entire file.

If you want ID3v2, those will be at the beginning of the file.



Related Topics



Leave a reply



Submit