2014-01-16 171 views
0

我正在嘗試編寫一個php腳本,它將採用上傳的mp3文件並自動創建一個ogg格式的副本。我的想法是使用exec()或shell_exec()從php運行dir2ogg。這是我的代碼:從php腳本運行dir2ogg

$command = 'dir2ogg /var/www/bais-mordechai-laravel/public/uploads/"'.$filename.'"'; 
$output = shell_exec($command); 

,我從此得到的輸出只能期望輸出的第一行:

dir2ogg 0.11.8 (2009-08-04), converts audio files into ogg vorbis. 

當我在命令行中運行這個直接(以根用戶身份)我得到以下內容:

dir2ogg 0.11.8 (2009-08-04), converts audio files into ogg vorbis. 

INFO: Converting "/var/www/bais-mordechai-laravel/public/uploads/Track 02.mp3" (using mpg123 as decoder)... 
[wav.c:371] warning: Cannot rewind WAV file. File-format isn't fully conform now. 
Encoding standard input to 
     "/var/www/bais-mordechai-laravel/public/uploads/Track 02.ogg" 
at quality 3.00 
    Encoding [ 0m15s so far]/[wav.c:371] warning: Cannot rewind WAV file. File-format  isn't fully conform now. 


Done encoding file "/var/www/bais-mordechai-laravel/public/uploads/Track 02.ogg" 

File length: 3m 50.0s 
Elapsed time: 0m 15.7s 
Rate:   14.6600 
Average bitrate: 91.1 kb/s 

因此,似乎該函數在命令執行完成之前返回。

我認爲腳本可能自己完成,但沒有創建ogg文件,因爲我打算。

所以我的問題是如何得到這個工作,或者我該如何去調試呢?

UPDATE: 使用邁赫蘭的建議,這是我的標準錯誤輸出:

Array 
(
[stdout] => dir2ogg 0.11.8 (2009-08-04), converts audio files into ogg vorbis. 


[stderr] => Traceback (most recent call last): 
    File "/usr/bin/dir2ogg", line 673, in <module> 
main() 
    File "/usr/bin/dir2ogg", line 641, in main 
conf = read_opts() 
    File "/usr/bin/dir2ogg", line 120, in read_opts 
in_path = [prefix for prefix in os.environ['PATH'].split(os.pathsep) if os.path.exists(os.path.join(prefix, command))] 
    File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__ 
raise KeyError(key) 
KeyError: 'PATH' 

[return] => 1 
) 
+0

以root身份運行PHP時會發生什麼? – Brad

+0

而我更新了我的回答 – Mehran

回答

2

您的問題背後的原因是特定於您的系統,我可以輕鬆地運行您的代碼和生成的.ogg文件。下面是我得到的,當我在它運行你的代碼的PHP文件:

dir2ogg 0.11.8 (2009-08-04), converts audio files into ogg vorbis. 

INFO: Converting "/home/mehran/001.mp3" (using mpg123 as decoder)... 

正如我在以前001.ogg表示創建成功。無論您的執行情況如何,都與您的配置有關。調試它的方法是獲取並打印該過程的stderr。這裏是你如何能得到它(shell_execexec功能不給你說):

<?php 
function _pipeExec($cmd, $input = '') 
{ 
    $proc = proc_open($cmd, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes); 
    fwrite($pipes[0], $input); 
    fclose($pipes[0]); 
    $stdout = stream_get_contents($pipes[1]); 
    fclose($pipes[1]); 
    $stderr = stream_get_contents($pipes[2]); 
    fclose($pipes[2]); 
    $rtn = proc_close($proc); 
    $result = array(
     'stdout' => $stdout 
     , 'stderr' => $stderr 
     , 'return' => $rtn 
    ); 
    return $result; 
} 

$command = 'dir2ogg /home/mehran/"001.mp3"'; 
$output = _pipeExec($command); 
print_r($output); 

你可以找到proc_open here更多信息。但是簡短的描述是它完全符合你的要求,它給你一個過程產生的所有輸出。在我的電腦上面的代碼生成:

Array 
(
    [stdout] => dir2ogg 0.11.8 (2009-08-04), converts audio files into ogg vorbis. 

INFO: Converting "/home/mehran/001.mp3" (using mpg123 as decoder)... 

    [stderr] => [wav.c:371] warning: Cannot rewind WAV file. File-format isn't fully conform now. 
Encoding standard input to 
     "/home/mehran/001.ogg" 
at quality 3.00 

    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m00s so far]/
    Encoding [ 0m00s so far] - 
    Encoding [ 0m00s so far] \ 
    Encoding [ 0m00s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m01s so far] \ 
    Encoding [ 0m01s so far] | 
    Encoding [ 0m01s so far]/
    Encoding [ 0m01s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m02s so far]/
    Encoding [ 0m02s so far] - 
    Encoding [ 0m02s so far] \ 
    Encoding [ 0m02s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m03s so far] - 
    Encoding [ 0m03s so far] \ 
    Encoding [ 0m03s so far] | 
    Encoding [ 0m03s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m04s so far]/
    Encoding [ 0m04s so far] - 
    Encoding [ 0m04s so far] \ 
    Encoding [ 0m04s so far] | 
    Encoding [ 0m05s so far]/
    Encoding [ 0m05s so far] - 
    Encoding [ 0m05s so far] \ 
    Encoding [ 0m05s so far] | 
    Encoding [ 0m05s so far]/[wav.c:371] warning: Cannot rewind WAV file. File-format isn't fully conform now. 

    Encoding [ 0m05s so far] - 

Done encoding file "/home/mehran/001.ogg" 

    File length: 3m 13.0s 
    Elapsed time: 0m 05.1s 
    Rate:   37.8620 
    Average bitrate: 110.9 kb/s 


    [return] => 0 
) 

可以使用return值來檢查進程的成功,並且你可以看到,即使進度指示器也被打印。不過,您可以查看stderr並查找代碼中的錯誤。

[更新]

不幸的是,我不能複製你的錯誤。我只是試圖做你正在做的事情,並面臨我自己的問題,我認爲這是值得分享的。這是我的完整,工作代碼上傳和轉換音頻文件:

<?php 

function _pipeExec($cmd, $input = '') 
{ 
    $proc = proc_open($cmd, array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w')), $pipes); 
    fwrite($pipes[0], $input); 
    fclose($pipes[0]); 
    $stdout = stream_get_contents($pipes[1]); 
    fclose($pipes[1]); 
    $stderr = stream_get_contents($pipes[2]); 
    fclose($pipes[2]); 
    $rtn = proc_close($proc); 
    $result = array(
     'stdout' => $stdout 
     , 'stderr' => $stderr 
     , 'return' => $rtn 
    ); 
    return $result; 
} 
?> 

<html> 
<body> 

<form method="post" action="./ogg.php" enctype="multipart/form-data"> 
    <label for="file">Filename:</label> 
    <input type="file" name="music" id="file"><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

<?php 
if (isset($_FILES['music'])) { 
    $des = $_FILES['music']['tmp_name'] .'.'. pathinfo($_FILES['music']['name'], PATHINFO_EXTENSION); 
    rename($_FILES['music']['tmp_name'], $des); 
    $command = 'dir2ogg "' . $des . '"'; 
    $output = _pipeExec($command); 
    echo "<div style='height: 500px; overflow: auto;'><pre>"; 
    print_r($output); 
    echo "</pre></div>"; 
} 
?> 

</body> 
</html> 

的一部分,如果你省略將導致一個問題是:

$des = $_FILES['music']['tmp_name'] .'.'. pathinfo($_FILES['music']['name'], PATHINFO_EXTENSION); 
rename($_FILES['music']['tmp_name'], $des); 

這將重命名臨時上傳文件,以便它將具有與其原始文件名相同的擴展名,即它將追加.mp3到上傳的文件。沒有這個,我無法將文件轉換爲.ogg文件。

關於你的錯誤還有一件事,確保你上傳的文件與dir2ogg兼容,因爲我在這裏和那裏看到一些文章不兼容,因此dir2ogg輸出類似你的錯誤。

最後,請確保您的文件大小不超過您的上傳大小和/或郵寄大小限制。 A phpinfo()會提示您當前的upload_max_filesizepost_max_size。也許你的文件沒有上傳!您可以通過查看$_FILES['music']['error']來確保您的文件上傳成功。

就是這些!

+0

謝謝。現在我能夠看到我的錯誤被拋出。我添加了輸出到我的問題。 –

+0

您的回答讓我找到了解決方案。看到我的後續問題在這裏:http://stackoverflow.com/questions/21324027/why-are-python-environment-variables-different-when-executed-from-a-web-server –

0

你可以這樣做;

$command = 'dir2ogg /var/www/bais-mordechai-laravel/public/uploads/"'.$filename.'"'; 
exec ("$command 2>&1", $convert_output, $return_val); 
if(preg_match("/Done/i", $convert_output[10])) { 
    echo "Convert complete"; 
} else { 
    echo "Conversion error"; 
    exit; 
} 

我的觀點要點;

  1. 2> & 1重定向命令輸出到控制檯,所以它由線

  2. 我在你的輸出lokked分配給 $convert_output線,並在線路, 結果關鍵字是否存在「 完成編碼....「。所以,你可以通過正則表達式使用關鍵字查找「完成」 上INE

  3. 最後一件事,你不會得到輸出,直到轉換完成

您還可以通過檢查結果輸出;

var_dump(array($convert_output, $return_val));