Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a surveillance camera and a program records videos when motion is detected. Basically, this program saves the video in a very heavy format.

My solution was to call a script that converts the video using ffmpeg. The script is called each time a video is made.

The conversion takes a relatively short time, but a lot of CPU use. The problem comes when a new video arrives to be converted and the conversion of the other hasn't finished. In that situation, the script is called again, resulting in two or more ffmpeg processes that make the machine almost hang during a considerable time.

How can I queue the conversion job, to be executed after the prior one is done? What I want is to have just one ffmpeg process at a time.

Thanks!

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You can use batch program that is part of at package (tools for job queuing). It is installed by default on many systems.

share|improve this answer
    
Thanks, batch is what I was looking for. I just had to ad | batch to the line calling the script. –  Tomas Sep 18 '11 at 5:06
add comment

I would set up a queue directory and have a background process go through the queue directory and spawn the ffmpeg conversion. And example of the background program might be:

queue=/var/tmp/vidq
outdir=/var/tmp/videos
while true; do  # do forever
    for file in $queue/*; do
        name=`basename $file`
        mv -f $file $queue/.current
        ffmpeg -i .current $outdir/$name
        touch -r $queue/.current $outdir/$name
        rm $queue/.current
    done
    sleep 300 # sleep for 5 minutes
done

You would have to put in some safety checks, like if the queued file is not a video file. And adjust the sleep value. But this would generate the converted file with the same name and with the original datestamp.

Another way is to have a file lock that is checked, when the ffmpeg process exits the lock is removed and another process can start, first creating the file lock. However this means that the logic is now in the program and must keep track of which files are left to be processed. The queue directory solution above means that the front end program just needs to deliver it to the queue.

share|improve this answer
    
This is a good solution, but there are much better ones (IMO) like at. –  Tomas Sep 18 '11 at 5:05
1  
while 1, will return 1: command not found. Use while :, or while true, or while (( 1 )). –  Chris Down Sep 18 '11 at 12:28
    
yup, I forgot the copy the 'test' when transcribing it. –  Arcege Sep 18 '11 at 13:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.