2014-09-21 309 views
-1

我想要做的就是使用PHP來執行youtube-dl -x --audio-format mp3 "token"並獲得以下參數JSON:從PHP代碼中執行shell腳本

  • 狀態(錯誤= 0 /成功= 1)
  • 保存的文件URL (您將在控制檯結果示例中看到它:[avconv] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.mp3

這是我寫的PHP代碼。

<?php 
if ($_GET["token"]) { 
      $url = $_GET["token"]; 
      $template = '/home/website/public_html/%(id)s.%(ext)s'; 
      $string = ('youtube-dl -x --audio-format mp3 ' . escapeshellarg($url) . ' ' . escapeshellarg($template)); 
      $descriptorspec = array(
       0 => array("pipe", "r"), // stdin 
       1 => array("pipe", "w"), // stdout 
       2 => array("pipe", "w"), // stderr 
      ); 
      $process = proc_open($string, $descriptorspec, $pipes); 
      $stdout = stream_get_contents($pipes[1]); 
      fclose($pipes[1]); 
      $stderr = stream_get_contents($pipes[2]); 
      fclose($pipes[2]); 
      $ret = proc_close($process); 
      echo json_encode(array('status' => $ret, 'errors' => $stderr, 
       'url_orginal' => $url, 'output' => $stdout, 
       'command' => $string)); 
     } 
?> 

基本上它執行

youtube-dl -x --audio-format mp3 "token" 

當我在控制檯執行相同的命令(通過SSH),我得到這樣的

[youtube] Setting language 
[youtube] Confirming age 
[youtube] nxtIRArhVD4: Downloading webpage 
[youtube] nxtIRArhVD4: Downloading video info webpage 
[youtube] nxtIRArhVD4: Extracting video information 
[youtube] nxtIRArhVD4: Encrypted signatures detected. 
[youtube] nxtIRArhVD4: Downloading js player vflE7vgXe 
[download] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.m4a 
[download] 100% of 3.93MiB in 00:00 
[avconv] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.mp3 
Deleting original file Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.m4a (pass -k to keep) 

結果當我在PHP環境中執行相同的命令越來越關注json結果

{ 
    "status":1, 
    "errors":"WARNING: The url doesn't specify the protocol, trying with http\nWARNING: Could not send HEAD request to http:\/\/\/home\/website\/public_html\/%(id)s.%(ext)s: \nWARNING: Falling back on generic information extractor.\nERROR: Unable to download webpage: \n", 
    "url_orginal":"nxtIRArhVD4", 
    "output":"[youtube] Setting language\n[youtube] Confirming age\n[youtube] nxtIRArhVD4: Downloading webpage\n[youtube] nxtIRArhVD4: Downloading video info webpage\n[youtube] nxtIRArhVD4: Extracting video information\n[youtube] nxtIRArhVD4: Encrypted signatures detected.\n[youtube] nxtIRArhVD4: Downloading js player vflE7vgXe\n[download] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.m4a\n\r[download] 0.0% of 3.93MiB at 729.44KiB\/s ETA 00:05\r[download] 0.1% of 3.93MiB at 1.86MiB\/s ETA 00:02\r[download] 0.2% of 3.93MiB at 3.86MiB\/s ETA 00:01\r[download] 0.4% of 3.93MiB at 7.69MiB\/s ETA 00:00\r[download] 0.8% of 3.93MiB at 13.79MiB\/s ETA 00:00\r[download] 1.6% of 3.93MiB at 17.80MiB\/s ETA 00:00\r[download] 3.2% of 3.93MiB at 17.64MiB\/s ETA 00:00\r[download] 6.3% of 3.93MiB at 17.79MiB\/s ETA 00:00\r[download] 12.7% of 3.93MiB at 20.88MiB\/s ETA 00:00\r[download] 25.4% of 3.93MiB at 25.21MiB\/s ETA 00:00\r[download] 50.8% of 3.93MiB at 28.47MiB\/s ETA 00:00\r[download] 100.0% of 3.93MiB at 41.23MiB\/s ETA 00:00\r[download] 100% of 3.93MiB in 00:00\n[avconv] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.mp3\nDeleting original file Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.m4a (pass -k to keep)\n[generic] %(id)s: Requesting header\n[generic] %(id)s: Downloading webpage\n", 
    "command":"youtube-dl --extract-audio --audio-format mp3 'nxtIRArhVD4' '\/home\/website\/public_html\/%(id)s.%(ext)s'" 
} 

事實上,PHP做的工作和保存文件,但輸出不相同,我無法獲得目標文件的網址。

我在做什麼錯?有什麼建議麼?

回答

0

看起來您的命令$string未正確生成。您的$url參數可能爲空。

如果你看一下錯誤它試圖加載網頁http:///home/website/public_html/%(id)s.%(ext)s

即時猜測$url必須是這是造成第四個參數(輸出文件),成爲第三個參數(視頻網址)空的,所以後來它試圖加載作爲URL的錯誤。

嘗試查看$url的內容後,請查看它的值,確保它實際上是一個URL。如果它是正確的,那麼檢查$string的內容確保命令實際上已完成,甚至嘗試手動運行該命令並查看會發生什麼,escapeshellarg()可能會導致比解決問題更多的問題;我建議您對輸入進行一些不同的驗證,例如對於YouTube網址的preg_match()