swg1cor14
swg1cor14

Reputation: 1722

Call ffmpeg via PHP but dont wait on it

I have my upload script that handles the file upload and then calls exec(ffmpeg -i......

I call ffmpeg 3 times. One to convert to mp4, one to convert to wemb and one to get thumbnail.

Problem is that if an investigator uploads ALL his footage, he doesnt want to have to sit there and wait for it to convert before he can do anything else. So how can i send the 3 commands and then exit the script?

I also want to run exec(rm -r /path/to/original) so the original video is deleted but obviously I have to wait for conversions to be complete before it can be run

Upvotes: 1

Views: 1708

Answers (5)

vbence
vbence

Reputation: 20333

You can use the ampersand character after the command to run it in the background and get your prompt (control) back right away.

I suggest creating a bash script like:

#!/bin/sh
ffmpeg -i $1 -ab 64k -vb 420k $1.mp4
ffmpeg -i $1 -ab 64k -vb 420k $1.webm

Then run it from php like:

system ('myscript.sh tempfile &');

The following example uses if structure to decide whether to use sound or no. The first parameter is the filename, if the second one equals 0 then no sound on the mp4, if the third one is zero then no sound on the webm.

#!/bin/sh
if [ $2 -eq 0 ]
then
    ffmpeg -i $1 -an -vb 420k $1.mp4
else
    ffmpeg -i $1 -ab 64k -vb 420k $1.mp4
fi


if [ $3 -eq 0 ]
then
    ffmpeg -i $1 -an -vb 420k $1.webm
else
    ffmpeg -i $1 -ab 64k -vb 420k $1.webm
fi

You can use it from PHP like:

system ('myscript.sh ' . $tempfile . ' ' . (0 + $mp4sound) . ' ' . (0 + $webmsound) . ' &');

... but remember you can even start a PHP file instead of a shellscript like:

system ("php converter.php \"$tempfile\" \"$mp4sound\" \"$webmsound\" &');

Upvotes: 5

Kristian J.
Kristian J.

Reputation: 10422

If you are running on Linux, a trailing ampersand to your command in the exec()-call will start the process in the background.

You probably also want to redirect the output from ffmpeg to /dev/null

ffmpeg -i (...)  > /dev/null 2>&1 &

The same approach could be used to call a shell script if you wanted to clean up after converting.

Upvotes: 0

sankar.suda
sankar.suda

Reputation: 1147

Try handling all ffmpeg conversions separately in one page. ex: convert.php

Once upload was done. sent hidden request to convert.php?id=someid

in top of the convert.php use ignore_user_abort(TRUE); The process will run even the user closed the browser. if the user is not closed that page still in that page with ajax response update status.

i hope it may help to get an idea..

Upvotes: 1

flyclassic
flyclassic

Reputation: 2499

  • Handle the file upload and conversion separately
  • After file has been uploaded,
  • Put it into a job queue
  • Run a cron job that check for latest queue every minute + if currently processing job has finished, if so then do the conversion.
  • After conversion do a unlink(/path/to/original ) to delete original video
  • Update the job status

Not sure if it make sense, something i am thinking of better than u do everything at once. Add little abit more of the work behind to ease the pain infront

Upvotes: 1

F21
F21

Reputation: 33383

Perhaps you can move the code that does the conversion into a script that is being called by a cron job.

For example, the user can upload a video to a directory and perhaps insert a record into a database table that contains all unconverted videos.

The cron job then runs every x minutes, checks the database for any unconverted videos, and then converts them. After that is done, the script can then delete the original videos and delete the row from the database.

Upvotes: 0

Related Questions