从php脚本运行dir2ogg

时间:2014-01-16 02:55:04

标签: php shell mp3 ogg

我正在尝试编写一个php脚本,它将采用上传的mp3文件,并以ogg格式自动创建它的副本。我的想法是使用exec()或shell_exec()从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.

当我直接从命令行运行时(以root用户身份),我得到以下内容:

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文件。

所以我的问题是如何让它工作,或者我该如何调试呢?

更新: 使用Mehran的建议,这是我的stderr输出:

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
)

2 个答案:

答案 0 :(得分: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并查找代码有什么问题。

<强> [UPDATE]

不幸的是,我无法复制您的错误。我只是尝试做你想做的事情并面对我自己的问题,我认为值得分享。这是我上传和转换音频文件的完整工作代码:

<?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兼容,因为我在某些帖子中已经阅读了某些格式不兼容的内容{{1}输出像你这样的错误。

最后,请确保您的文件大小不超过您的上传大小和/或帖子大小限制。 dir2ogg会提示您当前的phpinfo()upload_max_filesize。也许你的文件根本没有上传!您可以通过查看post_max_size

来确保成功上传文件

这就是我所拥有的一切!

答案 1 :(得分: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&gt;&amp; 1将命令输出重定向到控制台,因此将其分配给 逐行$convert_output

  2. 我对你的输出感兴趣,结果关键字存在于 10 行, “完成编码...... ”。因此,您可以使用关键字“完成”找到正则表达式 在 10

  3. 最后,转换完成后您将无法获得输出
  4. 您还可以通过;

    检查结果输出
    var_dump(array($convert_output, $return_val));