Skip to content
Advertisement

ffmpeg command executes via command line, but not via PHP script

I am working on a simple video upload and compression system and I am currently using the following process.

First Step

I use a multipart/form-data to upload the original video file, which I store in /var/www/site/videos_pre/video.mp4. My public folder is var/www/site/public_html/. I store an entry in my database with the video information.

Second step

I have a converter process which is very basic, but does the job (at least in CLI).

It has the following code:

public function converter($content_id)
{
    $content = $this->videos_m->get($content_id);

    $id = uniqid();
    $name_mp4 = $content->name_slug.'_'.$id.'.mp4';
    $name_webm = $content->name_slug.'_'.$id.'.webm';

    $command_mp4 = 'ffmpeg -i ../videos_pre/'.$content->original_file.' -b:v 1500k -bufsize 1500k ./videos/'.$name_mp4;
    system($command_mp4);

    $command_webm = 'ffmpeg -i ./videos/'.$name_mp4.' -c:v vp9 -c:a libvorbis ./videos/'.$name_webm;
    system($command_webm);

    $update_video = new stdClass();
    $update_video->archivo_mp4 = $name_mp4;
    $update_video->archivo_webm = $name_webm;

    $this->db->where('content_id', $content_id);
    $this->db->update('Videos', $update_video);
}

Third step – where the problem occurs

I have tested this on Windows 10 and Ubuntu 18.04. The code works on Windows 10 in both php and cli. The code on Ubuntu only works with cli.

The resulting commands are something like this:

// First command to reduce the bitrate of the mp4
ffmpeg -i /var/www/site/videos_pre/video.mp4 -b:v 1500k -bufsize 1500k /var/www/site/public_html/videos/video_5e757d3e0d762.mp4

// Second command to convert the mp4 to a webm to get both types
ffmpeg -i /var/www/site/public_html/videos/video_5e757d3e0d762.mp4 -c:v vp9 -c:a libvorbis /var/www/site/public_html/videos/video_5e757d3e0d762.webm

If I execute those commands they work perfectly. If I run the php script, be it in the browser or via CLI, literally nothing happens. No error messages nothing.

I am not sure if it’s a permission issue, if there’s something specific for php to run this, or some module I’ve not activated. I have enough execution time, I have it at 600 seconds, but as I said it doesn’t even take time, it just does nothing.

I can even echo the commands and they appear. So, basically the problem is I need to be able to run those commands from PHP. I did it on Windows, but Ubuntu 18.04 won’t let me. Both OSs have ffmpeg recently installed and I’ve been able to convert on both of them.

I tried changing paths from absolute to relative, no difference at all.

Advertisement

Answer

So, I managed to solve it, and it turns out it was a permission problem.

I had my /var/www/site folder with permissions like this: myuser:www-data. After checking this answer on a similar issue, I changed the folder permission to www-data:root and it worked instantly.

I hope that permission change doesn’t affect the rest of things, but for now everything’s working fine.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement