2

getaudio.php 版本 1

<?php
    $youtubeUrl =  $_GET['url'];
    $content = shell_exec("youtube-dl -j $youtubeUrl "); 
    $meta=json_decode($content);  
    $file= $meta->{'_filename'};
    $fileWithoutExtension = explode(".",$file)[0];
    $extension = ".m4a";
    $file = $fileWithoutExtension . $extension;   

    header("Content-Disposition: attachment; filename=\"$file\"" );
    header("Content-Type: application/octet-stream");

    passthru("youtube-dl -f 140 -o - $youtubeUrl");     
?>

getaudio.php 版本 2

<?php
    $youtubeUrl =  $_GET['url'];
    $file = shell_exec("youtube-dl -f 140 --get-filename $youtubeUrl "); 

    header("Content-Disposition: attachment; filename=\"$file\"" );
    header("Content-Type: application/octet-stream");

    passthru("youtube-dl -f 140 -o - $youtubeUrl");
?>

如果 url = https://www.youtube.com/watch?v=bvYtKY-UMxA那么我们得到$file = Deewana Kar Raha Hai Full Song 1080p HD Raaz 3 2012 YouTube-bvYtKY-UMxA.m4a

我的问题是getaudio.php 版本 1工作正常,但getaudio.php 版本 2不是......

getaudio.php 版本 2正在下载,但文件已损坏....


当两个 php 文件的$file相同时,第二个文件怎么可能不起作用


提示:下载youtube-dl.exe(适用于 windows)并将其与两个 php文件放在一起并在 localhost 中运行

4

1 回答 1

1
<?php
$youtubeUrl =  "https://www.youtube.com/watch?v=bvYtKY-UMxA";
$content = shell_exec("youtube-dl -j $youtubeUrl "); 
$meta=json_decode($content);  
$file= $meta->{'_filename'};
$fileWithoutExtension = explode(".",$file)[0];
$extension = ".m4a";

$file1 = $fileWithoutExtension . $extension;   

$file2 = shell_exec("youtube-dl -f 140 --get-filename $youtubeUrl ");

echo $file1,"NO SPACE HERE"; 
echo "<br>";    
 // have extra space at the end 
echo $file2,"SPACE HERE";        

echo "<br>"; 
echo "length of \$file1 is :".strlen($file1);  // 77
echo "<br>"; 
echo "length of \$file2 is :".strlen($file2);  // 78

?>

输出

Deewana Kar Raha Hai 完整歌曲 1080p 高清 Raaz 3 2012 YouTube-bvYtKY-UMxA.m4a---这里没有空间

Deewana Kar Raha Hai 完整歌曲 1080p 高清 Raaz 3 2012 YouTube-bvYtKY-UMxA.m4a ---SPACE HERE

$file1 的长度是:77

$file2 的长度是:78

看到$file1在.m4a之后没有空格,但对于$ file2 ,在.m4a之后有一个空格。

这使它们成为两个不同的字符串

解决方案:在getaudio.php 版本 2中对 $file 使用 trim() 函数

$file = trim($file); 
于 2015-03-20T12:03:03.940 回答