Join mp3 file pairs in folder with ffmpeg

I've got a load of mp3 files in a folder, and I want to merge each pair (Part 1 and Part 2) into a single mp3 file. All of the files are in the same format.

I know I can do this on a pair of files:

ffmpeg -i "concat:01 Snow White Part 1.mp3|01 Snow White Part 2.mp3" -acodec copy "01 Snow White.mp3"

.. but how would I do it on a whole folder? This is the folder contents:

01 Snow White Part 1.mp3
01 Snow White Part 2.mp3
02 Jack and the Beanstalk Part 1.mp3
02 Jack and the Beanstalk Part 2.mp3
03 The Wizard of Oz Part 1.mp3
03 The Wizard of Oz Part 2.mp3
04 Thumbelina Part 1.mp3
04 Thumbelina Part 2.mp3
05 Puss in Boots Part 1.mp3
05 Puss in Boots Part 2.mp3
06 The Lions Glasses Part 1.mp3
06 The Lions Glasses Part 2.mp3
07 The Snow Queen Part 1.mp3
07 The Snow Queen Part 2.mp3
08 Alibaba and the Forty Thieves Part 1.mp3
08 Alibaba and the Forty Thieves Part 2.mp3
09 The Emperor's New Clothes Part 1.mp3
09 The Emperor's New Clothes Part 2.mp3
10 Little Red Riding Hood Part 1.mp3
10 Little Red Riding Hood Part 2.mp3

Solution 1:

Assuming you're using bash on Linux, and assuming all file pairs are numbered as you had listed, I would iterate through the numbers with a for loop.

for c in {1..10}; do
  c=$(printf '%02.f' "$i")
  fnames=$(find . -maxdepth 1 -name "${i}*" | sort)
  parts=$(wc -l <<< "$fnames")
  if [ "$parts" -gt 1 ]; then 
    fnameout="${fnames%% Part 1*}.mp3"
    ffmpeg -i "concat: $f" -acodec copy "$fnameout"
  fi
done

If there are any files not paired up, they will be ignored. For testing purposes, I would add echo before ffmpeg, then make the last line done > test.txt so I could check for either mistakes or brilliance.

I hope this helps.