Home > Net >  Parse folder name, generate GIF from images inside using ffmpeg
Parse folder name, generate GIF from images inside using ffmpeg

Time:01-16

I have ~150 folders with some nasty folder structure. Inside are animation frames helpfully named 1.png, 2.png and so on. I need to generate gifs from those images and have them named properly. Here's how I'd handle this case by case:

Folder ./dragon/raw/bodies/dragon/green/old/5/down/idle has 25 .png files. I run the following commands:

# pngs to gif
ffmpeg -framerate 12 -start_number 1 -i ./dragon/raw/bodies/dragon/green/old/5/down/idle/%d.png dragon-green-old-5-down-idle-intermediate.gif

# gif to boomerang gif
ffmpeg -i dragon-green-old-5-down-idle-intermediate.gif -filter_complex "[0]trim=start_frame=1:end_frame=29,setpts=PTS-STARTPTS,reverse[r];[0][r]concat=n=2:v=1:a=0,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" dragon-green-old-5-down-idle.gif

# remove the intermediate gif
rm dragon-green-old-5-down-idle-intermediate.gif

Note: relevant part of the name for gif is whatever's after /raw/bodies

Question: how do I put this in a loop that would iterate over all the nested folders, parse their names and feed the result in the commands above?

Bonus question: is there a way to merge the two commands and create the boomerang gif immediately, removing the need in intermediate files?

CodePudding user response:

find . -name 1.png -exec sh -c '
for ref; do
  dir=${ref%/*}
  out=$(printf %s.gif "${dir#*/raw/bodies/}" | tr / -)
  echo ffmpeg -framerate 12 -i "$dir/%d.png" \
    -filter_complex "trim=end_frame=29,split[tmp],reverse,[tmp]concat" "$out"
done' sh {}  

Remove echo if the output looks good.

  • Related