0

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
  • 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

1

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

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

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.

0

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
do
    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"
    fi;
done

You must log in to answer this question.

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