0

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,

Kapil
  • 115
  • 3
  • 17
  • No answer to your question, but do you really need to use php? There is a command line tool, which is doing fine what you want, but you don't need php to run it: https://rg3.github.io/youtube-dl/ .. And if you want do use PHP, use shell_exec(..) to call it. Can give you examples if needed. – Business Tomcat Feb 03 '18 at 12:09
  • Well i am also retrieving that link from youtube-dl but i dont want to do conversion. It takes huge time. Trust me. youtube is rate limiting download speed as well. so i just want to grab mp4 audio link and give download as a mp3 file to users. Just want to somehow play it in the players @UweMannl – Kapil Feb 03 '18 at 12:12
  • 2
    Okay, I understand what you want. But first problem: if you grab MP4, you can't provide a MP3 file. This are different formats. MP4 is mostly like AAC codec, and a MP3 file has MPEG Audio Layer 3. It is possible to store MP3 Audio in a MP4 file, but youtube doesn't do hat (cause of worse compression compared to AAC). So either you reencode, or you provide an AAC/MP4 file to your users. Not sure if youtube will block you because you generate many request from your site. – Business Tomcat Feb 03 '18 at 12:44
  • Hmm, Thinking about re encode once again. I do that in the following manner, ffmpeg -i 'mp4-audio-url' -codec libmp3lame 'output.mp3'. Is it possible to just re-encode the file but do not save it on server. Just give instant download to users ? All i want to add MPEG Audio Layer 3 to the output file. @UweMannl – Kapil Feb 04 '18 at 04:08
  • Very good question, never done this. I believe it is not very easy. Have a look at the protocols from ffmpeg: https://ffmpeg.org/ffmpeg-protocols.html and in the wiki https://trac.ffmpeg.org/wiki/StreamingGuide. Perhaps http or rtp or rtsp is relevant. For rtp, you need a player on client side, which is capable of rtp. Additionally you can read here: https://github.com/Streamedian/html5_rtsp_player/wiki/HTML5-RTSP-Player. There are also hints for the server side. Perhaps anybody else has more information. – Business Tomcat Feb 04 '18 at 07:44

0 Answers0