-1

I am trying to run a shell script in PHP through shell_exec(). The shell script merges two mp3 files using FFmpeg. I have tested the code in my terminal and it works just fine. However, I cannot get it to work with php. This is supposed to run when I click a button on a webpage (I am using WordPress). When I try doing this, it makes it to the shell script page, and makes it through it. But, it seems that FFmpeg does not work.

image of webpage

I call:

$new_file = realpath(dirname(__FILE__) . '/../../../scripts'). '/' . $new_file;
$path = realpath(dirname(__FILE__) . '/../../../scripts/combine-mp3.sh');

I get the $ad_file and $main_file from objects stored in a database.

$output = shell_exec('bash ' . $path . ' ' . $ad_file .  ' ' . $main_file . ' ' . $new_file);
echo "<pre>$output</pre>";

Example of what it looks like:

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

I am thinking it could possibly be the environment or something. But I have no idea what is wrong.

Andrew
  • 26,706
  • 9
  • 85
  • 101
mette
  • 1
  • 1
  • Is the shell executing and failing, or failing to execute. –  Feb 26 '19 at 17:53
  • Which system user runs the PHP script? Does it have permissions to write to the target? – Pinke Helga Feb 26 '19 at 17:59
  • PD of [How can I debug exec() problems?](//stackoverflow.com/q/12199353) – mario Feb 26 '19 at 18:00
  • Please show PHP `passthru('id');` and Bash `ls -la /path/to/scripts` (or just PHP `passthru('id;ls -la ' . dirname($new_file));`) – Pinke Helga Feb 26 '19 at 18:14
  • you should always use [escapeshellarg](http://php.net/manual/en/function.escapeshellarg.php) for shell args. – ArtisticPhoenix Feb 26 '19 at 18:16
  • I am not sure how well using a URL like this `http://localhost/...` is going to work for you, also remember url's "typically" need special considerations such as encoding (ex. names with spaces, ampersands etc. ). – ArtisticPhoenix Feb 26 '19 at 18:22

1 Answers1

0

So I found a solution. But I am not sure if it great for the longterm. I had to specify the location of ffmpeg. /usr/locacl/bin/ffmpeg for example. But I am nervous about using this in the future. Is there a chance it would be stored somewhere else?

mette
  • 1
  • 1