Search code examples
phpffmpegcentossudo

executing a command from php that requires being root


I would like to find a solution to this problem: I have a list of images that i want to convert into a video on the server. I've created a virtual machine with centOS and installed ffmpeg to test that. Everything working well when I type myself this line in the terminal

sudo ffmpeg -r 10 -i img%03d.png -c:v libx264 -preset veryslow -crf 0 
video.mp4

What I want is to call this when I click on a button. Here is the code:

Javascript:

$(document).ready(function(){
    $("#ffmpeg").click(function(){
        $.get('ffmpegBooklet.php', function (data) {
            console.log(data);
       });
    });
});

PHP:

echo shell_exec("/usr/local/bin/ffmpeg  -r 10 -i /img%03d.png -c:v 
libx264 -preset veryslow -crf 0 video.mp4 2>&1");

What i get in the console of the browser is : the log from ffmpeg and at the end --> Permission denied

I've tried with a static build (after reading on internet) but I have the same problem. Is there any solution to do this without the sudo rights? I don't want to do this as sudo or to some tricky command to give sudo permissions through php because I think it's not secure.

Any helps is welcome! Thanks for reading it :)


Solution

  • Fixed like this. Thank you for the help.

    I disabled SELinux just to check what happened. FFmpeg through php worked like a charm. I checked the file created and the owner and group were apache:apache. First, I've changed the owner of the folder for input and output and set apache. Then I gave rights to apache. I switched on SELinux and had again the permission problem. I've found this solution, which fixed my issue:

    # semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html(/.*)?'
    
    # restorecon -R /var/www/html
    

    I've also add my profile to the apache group because I couldn't access the folder anymore.

    Now everything is working with SELinux ON, which satisfied me. FFmpeg/apache are not blocked anymore!