php - 从 PHP 脚本执行包含 FFmpeg 的 shell 脚本
问题描述
我正在尝试通过 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.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
我认为这可能是环境或其他东西。但我不知道出了什么问题。
解决方案
所以我找到了解决方案。但我不确定它是否适合长期使用。我必须指定 ffmpeg 的位置。/usr/locacl/bin/ffmpeg 例如。但我对将来使用它感到紧张。是否有可能将其存储在其他地方?
推荐阅读
- angular - Angular, Uncaught SyntaxError: Unexpected end of input when moving to production
- java - javax.mail.BodyPart getfileName 返回空 java 邮件
- bash - Docker 自动安装后无法启动
- python - pythonanywhere 上的媒体目录/文件出现问题
- blazor - 有没有办法让 WebAssembly 应用程序作为独立或自托管运行?
- java - 无法使用 Apache Camel 3.1.0 将 XML 转换为 JSON
- ruby-on-rails - SocketError:执行 docker-compose 文件时无法打开与 docker 服务的 TCP 连接
- python - Pytest - 获取使用的固定装置和创建的数据的可见性
- javascript - 将 JSON 文件传递到 php 服务器,然后将其中的数据保存到 Mysql DB
- sql - 如何摆脱动态 SQL (T-SQL)