从PHP脚本执行包含FFmpeg的Shell脚本

时间:2019-02-26 17:50:30

标签: php wordpress bash ffmpeg php-ffmpeg

我正在尝试通过shell_exec()在PHP中运行Shell脚本。 Shell脚本使用FFmpeg合并两个mp3文件。我已经在终端中测试了代码,并且工作正常。但是,我无法使其与php一起使用。当我单击网页上的按钮(我正在使用WordPress)时,该脚本应该可以运行。当我尝试执行此操作时,它进入外壳脚本页面并通过它。但是,似乎FFmpeg无法正常工作。

image of webpage

我打电话:

$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.mp3 http://localhost/zzz/wp-content/uploads/mp3/main_file.mp3 new_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

我认为这可能是环境或其他原因。但是我不知道怎么了。

1 个答案:

答案 0 :(得分:0)

所以我找到了解决方案。但是我不确定从长远来看是否很好。我必须指定ffmpeg的位置。例如/ usr / locacl / bin / ffmpeg。但是我担心将来会使用它。是否有可能将其存储在其他地方?