I need a way to automatically convert gif files to mp4. And set a directory and check for new files every 5min. Id like to use ffmpeg for the coverting and delete the gif after.

ffmpeg -f gif -i cat.gif cat.mp4

Is this hard to to do?

  • 1
    Instead of polling the directory you may want to listen for changes with inotifywait(1) that way it’s trivial to ensure your job runs exactly once per file. When a “create” event fires, immediately move the filesystem entry out of the directory (this works even if it’s still being written to) into a unique temporary destination (another directory that must be located on the same filesystem) and then process its content with ffmpeg.
    – phg
    Commented Aug 24, 2018 at 11:43

2 Answers 2


It should be straightforward; create a shell script that does the work (eg ffmpegcron.sh):

for i in /path/to/gifs/*.gif
    [ -f "$i" ] || continue
    ffmpeg -f gif -i "$i" "${i%.gif}.mp4" &&
        rm -f "$i"

Then make the script executable with chmod +rx ffmpegcron.sh, and use crontab -e to create the cron job which should look like:

*/5 * * * * /path/to/ffmpegcron.sh

Then cron will run your script every 5 mins.


I would refrain from using a time-based solution here and strongly advocate the approach in phg's comment to the question.

While the likelihood may not be high, it's quite possible to start a conversion using cron before the GIF, wherever it is coming from, may not be fully written yet.

#! /bin/bash
inotifywait -m -e close_write /path/to/dir | while read line
    if [[ "${line,,}" =~ ".gif" ]] ; then
      file=$(echo "$line"|awk 'BEGIN{FS=" CLOSE_WRITE,CLOSE "}{printf "%s%s",$1, $NF}')
      ffmpeg -f gif -i "$file" "${file%.*}.mp4" &&
          rm -f "$file"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .