Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php脚本运行dir2ogg_Php_Shell_Mp3_Ogg - Fatal编程技术网

从php脚本运行dir2ogg

从php脚本运行dir2ogg,php,shell,mp3,ogg,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

我试图写一个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
)
数组
(
[stdout]=>dir2ogg 0.11.8(2009-08-04),将音频文件转换为ogg vorbis。
[stderr]=>回溯(最近一次呼叫最后一次):
文件“/usr/bin/dir2ogg”,第673行,在
main()
文件“/usr/bin/dir2ogg”,第641行,主文件
conf=read_opts()
读取选项中的文件“/usr/bin/dir2ogg”,第120行
in_path=[如果os.path.exists(os.path.join(prefix,command)),则os.environ['path'].split(os.pathsep)中前缀的前缀]
文件“/usr/lib/python2.7/UserDict.py”,第23行,在__
升起钥匙错误(钥匙)
KeyError:“路径”
[返回]=>1
)

您出现问题的原因与您的系统有关,因为我可以轻松运行您的代码并生成.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_exec
exec
函数不提供此功能):

您可以使用
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>
这将重命名临时上载的文件,使其具有与其原始文件名相同的扩展名,即将
.mp3
附加到上载的文件中。没有这个,我无法将文件转换为
.ogg
文件

关于您的错误,还有一件事,请确保您上传的文件与
dir2ogg
兼容,正如我在这里和那里的一些帖子中所读到的,某些格式不兼容,因此
dir2ogg
会输出与您类似的错误

最后,确保文件大小不超过上载大小和/或帖子大小限制。一个
phpinfo()
将提示您当前的
上传最大文件大小
上传最大文件大小
。也许你的文件根本没有上传!通过查看
$\u文件['music']['error']
,您可以确保文件上载成功


我只有这些

你可以这样做

var_dump(array($convert_output, $return_val));
我的观点的要点

  • 2> &1将命令输出重定向到控制台,以便将其分配给
    $convert\u输出
    逐行

  • 我查看了您的输出,结果关键字出现在第10行, “完成编码…”。因此,您可以使用关键字“Done”来查找正则表达式 在线10

  • 最后一件事,在转换完成之前,您不会得到输出
  • 您还可以通过检查输出的结果


    当您以root用户身份运行PHP时会发生什么?我已经更新了我的答案谢谢。现在我可以看到我的错误被抛出。我在问题中添加了输出。您的回答引导我找到了解决方案。请参见我的后续问题:
    <?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);
    
    $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;
    }
    
    var_dump(array($convert_output, $return_val));