如何仅通过 php 下载 youtube [m4a] 音频

how to download youtube [m4a] audio only via php

我有以下来自 youtube 的 url:

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

我可以直接在浏览器中打开它并将其保存为 m4a 文件,然后我可以通过 windows 媒体播放器播放它。

但是如果我尝试使用 headers 等通过 php 下载它,文件仍在下载,但是当我尝试在 windows 媒体播放器上播放时出现错误。

错误是:

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.

一些我如何能够以字节为单位获取文件大小的方法,这是我现在使用的 header:

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;

只需进行一些调整,这段代码就可以在我的服务器上使用 mp3 文件,没有任何问题,但我需要从我的服务器下载一个 m4a/mp4 [仅音频] 的远程文件。

注意: 当我检查直接下载和通过 php 下载的两个文件时。 我在文件的 属性 中发现了一个区别,即通过我的 php 代码下载的文件不包含 bit-rate / 长度相关的详细信息。 另一方面,通过浏览器直接下载的文件包含 bit-rate 值 0kbps 和长度。

也许我遗漏了什么但不知道是什么?

有人可以帮助我吗?

您的代码是正确的,但仅适用于音频-> 音频文件。

但正如您所说,您想要从 mp4 视频文件下载音轨,可能会有一些相同的补丁,但这不是最佳选择。

我建议您使用 'FFMPEG',因为当我们处理视频到音频的转换时,我们必须维护它们的编解码器信息(采样率、比特率)等。

FFMPEG 将是此类操作的最佳解决方案。

谢谢:)