I have the following url from youtube:
https://r4---sn-gwpa-a3vl.googlevideo.com/videoplayback?itag=140&ei=W12aWtqEKY6d1gLtxrCIDQ&requiressl=yes&keepalive=yes&fvip=4&ipbits=0&pl=36&key=cms1&mime=audio%2Fmp4&lmt=1514440019010993&expire=1520087483&c=WEB&source=youtube&clen=1849657&ip=2a02%3Ac207%3A2016%3A2180%3A%3A1&gir=yes&signature=02253C6FF1B66649D5830E55415CE3ED216A3D24.6489003335D2F1ECCE7DD85F767A12A78FF20935&sparams=clen,dur,ei,expire,gir,id,initcwndbps,ip,ipbits,ipbypass,itag,keepalive,lmt,mime,mip,mm,mn,ms,mv,pl,requiressl,source&id=o-AOjAGDxOAog_lYGvNSG5lhiJhh-tcLbQ9Onr7w9eY3CB&dur=116.401&ratebypass=yes&redirect_counter=1&rm=sn-4g5e6r7z&req_id=5a55cbbe03daa3ee&cms_redirect=yes&ipbypass=yes&mip=2405:204:a509:abdb:8c40:a522:92dd:334a&mm=31&mn=sn-gwpa-a3vl&ms=au&mt=1520065776&mv=m
I can open this directly in my browser and save it as a m4a file and then i can play it via windows media player.
But if i try to download it via php using headers etc file is still getting download but i am getting error when trying to play it on windows media player.
Error is:
Windows Media Player cannot play the file. The Player might not support the file type or might not support the codec that was used to compress the file.
Some how I am able to get file size in bytes and this is the header that I am using right now:
header('Content-Description: File Transfer');
header('accept-ranges: bytes');
header('Content-length: ' . $size);
header("Content-Range: 0-".($size-1)."/".$size);
header("Content-Type: audio/mp4");
header("Connection: keep-alive");
header('Content-Type: application/force-download');
header('Content-Transfer-Encoding: chunked');
header('Content-disposition: attachment; filename="'.$title.'.m4a"');
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); // URL of youtube video in M4A
exit;
Just making few adjustments, this code works with mp3 file on my server without any issue but i need to download a remote file which is m4a/mp4 [Audio only] from my server.
Note: When I checked both files directly downloaded & downloaded via php. I found one difference inside the property of the files which is, File downloaded via my php code does not contain bit-rate / length related details. And on the other hand direct downloaded file via browser contains bit-rate value 0kbps and length as well.
Maybe I am missing something but don't know what?
Can somebody help me?
Your code is correct but will work for audio->audio file only.
But as you said that you want to download audio track from mp4 video files, there might some patches for same, but it won't be optimal option.
I would like to suggest you to use 'FFMPEG' for same as when we deal with video to audio conversion we have to maintain their codec info (sample rate, bit rate) , etc.
FFMPEG will be optimal solution for such operations.
Thanks :)