Kapil
Kapil

Reputation: 115

correct way to download a mp4 [Audio only] file as a mp3 file via php

I am asking this question because i found it impossible as far my knowledge stands. However i believe here on stackoverflow a lot of genius persons visits so maybe someone can give a good advice / trick.

My problem is, I am downloading a audio/mp4 file of youtube hosted on googlevideo.com's server.

My PHP code for this purpose: $mp3path is url of video

header('Content-Description: File Transfer');
if(strpos($mp3path, "https://") === false) {
    header('Content-length: ' . size($mp3path)); //size is custom function
} 
    header("Content-Type: audio/MP4A-LATM, audio/MP4A, audio/m4a, audio/mp4, audio/mp4a, audio/mp4-audio, audio/mpeg"); 
    header('Content-Type: application/force-download');
    header("Accept-Ranges: bytes");
    header('Content-Transfer-Encoding: binary'); 
    header('Content-disposition: attachment; filename="'.$title.'.mp3"'); 
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
    header("Cache-Control: private", false);
    header('Pragma: no-cache');
    readfile($mp3path);
    exit;

I can download this audio file but few mp3 players are not able to play it when i did some research on by using mp3val [mp3val.sourceforge.net]. I found this file do not contain sample-rate, bit-rate or some other required codecs inside the file. Error received from mp3val: Unknown file format

I know one possible way of doing this which is ffmpeg but i am looking for a less time consuming option of doing this because first ffmpeg download the whole file and then convert it to mp3 and save it on server. It takes a lot of time.

I am looking for a easiest solution in which, i dont want to save the whole file on my server. I want to call this file from remote server and want to add sample rate, bit-rate in the file and then just somehow with php i want to start download on browser.

All i mean to say i need a faster solution. I have seen 2 yt to mp3 converter, They are doing the same thing, giving instant download. i dont know how?

Please if you think its a stupid question or not possible then dont report to stackoverflow. I am trying to figure it out that's why i am asking this question here. I hope you'll understand my curiosity.

Thanks,

Upvotes: 0

Views: 1233

Answers (0)

Related Questions