help with recursive handbrake script

I have a batch of mkv video files I would like to transcode to a more compressed format. After spending a significant amount of time experimenting with various parameters I think I have found the handbrake settings that give the best compromise. I now want to construct a simple bash script that will scan the directory containing the original mkv files, recreate the directory tree and then transcode them into the new directory. I then need to alter the file name to replace the underscores with spaces and add the episode number to the beginning based on the original file name.

My original file structure is as follows:

original_series
|
|-series1
| |-episode_1.s01e01.mkv
| |-episode_2.s01e02.mkv
|
|-series2
| |-episode1.s01e01.mkv
| |-episode2.s02e02.mkv

where the s01 represents season 1 and e01 or e02 represent episode in the season as per thetvdb.com. I would like to replicate this directory structure and change the file name to add the season number and episode number to the front of the file based on the sXX and eXX values, so that the new structure looks like:

transcoded_series
|
|-series1
| |-1-1-episode 1.mp4
| |-1-2-episode 2.mp4
|
|-series2
|-1-1-episode 1.mp4
|-2-2-episode 2.mp4

My first attempt looks like this:

#!/bin/bash

sourcedir="/media/video/original_series"
destdir="/media/video/transcoded_series"
cd "$sourcedir"

for i in *.mkv; do

HandBrakeCLI -i "$i" -o "$destdir/${i%.*}.mp4" -e x264 -q 20.0 -E copy  -B auto -6 auto -R Auto -D 0.0 \
 -f mkv --detelecine --decomb  --loose-anamorphic -m -x b-adapt=2:rc-lookahead=50

done

but it is not recursive, I have no idea how to recreate the directory tree and no idea how to alter the file names:confused:

I would be really grateful for some (a lot of) help!

To make the directories:

cd /media/video/original_series
find . -type d | sort > /tmp/mdirlist.txt
cd /media/video/transcoded_series
while read d
do
   [ ! -d $d ]  && mkdir $d
done < /tmp/mdirlist.txt

It is not clear to me, at all, what all of the options to HandBrakeCLI are doing.
So this is a guess.

src=/media/video/original_series
dest=/media/video/transcoded_series
cd $src
find . -type f -name '*.mkv' |
while read fname 
do
   ofile=$dest/${fname}
   ofile=${ofile%.*}.mp4
   HandBrakeCLI -i "$fname" -o "$ofile" -e x264 -q 20.0 -E copy  -B \ 
   auto -6 auto -R Auto -D 0.0 \
    -f mkv --detelecine --decomb  \
  --loose-anamorphic -m -x b-adapt=2:rc-lookahead=50
done

Jim, Thanks for you help.

It nearly works - the new directories are created but only the last mkv file in the last directory is transcoded, for example if I have:
dir1 - file1.mkv & file2.mkv
dir2 - file3.mkv & file4.mkv
only file4 is transcoded in the new dir2 that is created, dir1 is also created but remains empty.

I checked that the mdirlist.txt is correct.

Any further suggestions?

what is the output of this

src=/media/video/original_series
dest=/media/video/transcoded_series
cd $src
echo $src
find . -type f -name '*.mkv'
echo $dest
cd $dest
find . -type f 

it basically lists everything in the source dir followed by everything in the destination dir, ie:

/media/video/original_series
./TEST2/TortillaFlaps.s01e05.mkv
./TEST2/WestOfPesos.s01e08.mkv
./TEST/AMessageToGracias.s01e14.mkv
./TEST/AHuntingWeWillGo.s01e27.mkv
/media/video/transcoded_series
./BEN_HOLLYS_LITTLE_KINGDOM/extrafanart/477431.jpg
./BEN_HOLLYS_LITTLE_KINGDOM/logo.png
./BEN_HOLLYS_LITTLE_KINGDOM/poster.jpg
./BEN_HOLLYS_LITTLE_KINGDOM/banner.jpg
./BEN_HOLLYS_LITTLE_KINGDOM/landscape.jpg
./BEN_HOLLYS_LITTLE_KINGDOM/TheLostEgg.s01e11.mp4
./BEN_HOLLYS_LITTLE_KINGDOM/fanart.jpg
......etc, etc