I have a bunch of video files with associated subtitles. I am trying to program a bash script that merges the subtitles and videos into matroshka containers.
The input video files are named "videoName.mkv", "videoName.webm" or videoName.mp4" depending on the original container. The input subtitles files are named "videoName.languageCode.vtt". The output video files are named "subsvideoName.mkv"
Here is the script I came up with:
#!/bin/bash
for video in *; do
if [[ $video =~ \.(mkv|webm|mp4)$ ]]; then
[[ $video =~ \.(mkv|webm|mp4) ]]
format=${BASH_REMATCH}
[[ $video =~ .*[^$format] ]]
name=${BASH_REMATCH}
echo $name
arsubs=()
for subs in *; do
echo "$subs"
if [[ $subs =~ $name.*\.vtt$ ]]; then
[[ $subs =~ \.[a-zA-Z0-9\-]*\.vtt$ ]]
lang=$BASH_REMATCH
[[ $lang =~ [^\.][a-zA-Z0-9\-]*[^\.] ]]
lang=$BASH_REMATCH
if [[ $lang =~ [a-z]*[^a-z]+ ]]; then
lang=$BASH_REMATCH
[[ $lang =~ [a-z]* ]]
lang=$BASH_REMATCH
fi
arsubs+=(--language 0:$lang)
arsubs+=("$subs")
fi
done
nameout=subs$name.mkv
mkvmerge -o "$nameout" "$video" "${arsubs[@]}"
fi
done
The regex I'm having trouble with is [[ $subs =~ $nom.*.vtt$ ]] (line 13) that doesn't match if the filename contains regex tags such as "+" or "$". If the filename doesn't contains such characters, the script works well.
I looked around, but the only solutions I could find require escaping the problematic characters which, I think, cannot be done here.
Thank you for your time.