【问题标题】:two codes Theoreticaly same but practicaly works different...how is it possible两个代码理论上相同,但实际上工作方式不同......怎么可能
【发布时间】:2015-05-20 12:41:03
【问题描述】:

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 文件并在本地主机中运行

【问题讨论】:

  • 脚本版本 1 和版本 2 中的 print_r($file) 是什么?
  • 你的版本version 2的第一行是
  • 您的文件 getaudio 版本 2 只有您发布的代码?文件的第一行是

标签: php youtube youtube-dl


【解决方案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 Full Song 1080p HD Raaz 3 2012 YouTube-bvYtKY-UMxA.m4a---这里没有空间

Deewana Kar Raha Hai Full Song 1080p HD Raaz 3 2012 YouTube-bvYtKY-UMxA.m4a ---这里的空间

$file1 的长度是:77

$file2 的长度是:78

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

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

解决方案: 在 getaudio.php 版本 2

中对 $file 使用 trim() 函数
$file = trim($file); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多