【发布时间】:2019-07-20 08:14:47
【问题描述】:
我正在尝试通过 shell_exec() 在 PHP 中运行一个 shell 脚本。 shell 脚本使用 FFmpeg 合并两个 mp3 文件。我已经在我的终端中测试了代码,它工作得很好。但是,我无法让它与 php 一起使用。这应该在我单击网页上的按钮时运行(我使用的是 WordPress)。当我尝试这样做时,它会进入 shell 脚本页面,并通过它。但是,FFmpeg 好像不行。
我打电话:
$new_file = realpath(dirname(__FILE__) . '/../../../scripts'). '/' . $new_file;
$path = realpath(dirname(__FILE__) . '/../../../scripts/combine-mp3.sh');
我从存储在数据库中的对象中获取 $ad_file 和 $main_file。
$output = shell_exec('bash ' . $path . ' ' . $ad_file . ' ' . $main_file . ' ' . $new_file);
echo "<pre>$output</pre>";
外观示例:
bash
/Users/me/Documents/websites/zzz/wp-content/plugins/myplugin/scripts/combine-mp3.sh
http://localhost/zzz/wp-content/uploads/mp3/News_Intro.mp3http://localhost/zzz/wp-content/uploads/mp3/main_file.mp3new_file_test.mp3
The combine-mp3.sh then gets run:
#this script requires 3 files, input1, input2 and output
echo -e "Input File 1:\t" $1
echo -e "Input File 2:\t" $2
echo -e "Output File:\t" $3
#make sure there are not temporarily files floating around, they use long specific names to avoid accidentally removing important files
rm combine-mp3-temp-file-1.mp3
rm combine-mp3-temp-file-2.mp3
#create intermediate files at 128kbps
ffmpeg -i $1 -f mp3 -ab 128k -ar 44100 -ac 2 combine-mp3-temp-file-1.mp3
ffmpeg -i $2 -f mp3 -ab 128k -ar 44100 -ac 2 combine-mp3-temp-file-2.mp3
#combine the two intermediate files
ffmpeg -i "concat:combine-mp3-temp-file-1.mp3|combine-mp3-temp-file-2.mp3" -acodec copy $3
#remove temporary files now they are not needed
rm combine-mp3-temp-file-1.mp3
rm combine-mp3-temp-file-2.mp3
我认为这可能是环境或其他原因。但我不知道出了什么问题。
【问题讨论】:
-
是shell执行失败,还是执行失败。
-
哪个系统用户运行 PHP 脚本?它是否有权写入目标?
-
请显示 PHP
passthru('id');和 Bashls -la /path/to/scripts(或只显示 PHPpassthru('id;ls -la ' . dirname($new_file));) -
您应该始终使用escapeshellarg 作为shell args。
标签: php wordpress bash ffmpeg ffmpeg-php