|
|
|
@ -6,6 +6,11 @@ DEFAULT_LANGUAGE_CODE="eng"
|
|
|
|
# TODO: This needs to be used in the glob, but I don't know how.
|
|
|
|
# TODO: This needs to be used in the glob, but I don't know how.
|
|
|
|
VIDEO_EXTENSIONS="mkv,mp4,avi"
|
|
|
|
VIDEO_EXTENSIONS="mkv,mp4,avi"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typeset -A LANGUAGE_NAMES
|
|
|
|
|
|
|
|
LANGUAGE_NAMES=( \
|
|
|
|
|
|
|
|
'english' 'en' \
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
_mkvmerge_add_subtitles_mkvmerge () {
|
|
|
|
_mkvmerge_add_subtitles_mkvmerge () {
|
|
|
|
original_file="$1"
|
|
|
|
original_file="$1"
|
|
|
|
results_directory="$2"
|
|
|
|
results_directory="$2"
|
|
|
|
@ -26,21 +31,61 @@ _mkvmerge_add_subtitles_mkvmerge () {
|
|
|
|
# TODO: Support the other patterns listed in the above comment.
|
|
|
|
# TODO: Support the other patterns listed in the above comment.
|
|
|
|
video_basename="$1"
|
|
|
|
video_basename="$1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Include subtitles without language codes, assuming $DEFAULT_LANGUAGE_CODE is correct.
|
|
|
|
|
|
|
|
# For example: [basename].srt
|
|
|
|
|
|
|
|
uncoded_subtitle_name="${original_file:r}.srt"
|
|
|
|
|
|
|
|
if [[ -f "$uncoded_subtitle_name" ]]; then
|
|
|
|
|
|
|
|
subtitle_options_array+=("--language" "0:${DEFAULT_LANGUAGE_CODE}" "$uncoded_subtitle_name")
|
|
|
|
|
|
|
|
echo "Found uncoded subtitle file ${uncoded_subtitle_name}, assuming \
|
|
|
|
|
|
|
|
${DEFAULT_LANGUAGE_CODE}."
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Include subtitles with language codes in their filenames.
|
|
|
|
# Include subtitles with language codes in their filenames.
|
|
|
|
# For example [basename].[language code].srt
|
|
|
|
# For example [basename].[language code].srt
|
|
|
|
coded_subtitle_files=(${original_file:r}.*.srt(N))
|
|
|
|
coded_subtitle_files=(${original_file:r}.*.srt(N))
|
|
|
|
if [[ ! -z $coded_subtitle_files ]]; then
|
|
|
|
if [[ ! -z $coded_subtitle_files ]]; then
|
|
|
|
for fname in "$coded_subtitle_files"; do
|
|
|
|
for fname in $coded_subtitle_files; do
|
|
|
|
language_code="${${fname:r}:e}"
|
|
|
|
language_code="${${fname:r}:e}"
|
|
|
|
subtitle_options_array+=("--language" "0:${language_code}" "$fname")
|
|
|
|
subtitle_options_array+=("--language" "0:${language_code}" "$fname")
|
|
|
|
|
|
|
|
echo "Found coded subtitle file ${fname} for language ${language_code}."
|
|
|
|
done
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Include subtitles without language codes, assuming $DEFAULT_LANGUAGE_CODE is correct.
|
|
|
|
# Include subtitles in the Subs directory
|
|
|
|
# For example: [basename].srt
|
|
|
|
named_subtitle_files=(${original_file:h}/Subs/*.srt(N))
|
|
|
|
uncoded_subtitle_name="${original_file:r}.srt"
|
|
|
|
if [[ ! -z $named_subtitle_files ]]; then
|
|
|
|
if [[ -f "$uncoded_subtitle_name" ]]; then
|
|
|
|
existing_language_codes=()
|
|
|
|
subtitle_options_array+=("--language" "0:${DEFAULT_LANGUAGE_CODE}" "$uncoded_subtitle_name")
|
|
|
|
for fname in $named_subtitle_files; do
|
|
|
|
|
|
|
|
file_basename=${fname:t}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# determine language code from filenames like 2_English.srt
|
|
|
|
|
|
|
|
# This character soup does three things
|
|
|
|
|
|
|
|
# * #[0-9]*_ removes the preceding track number, such as 2_ or 13_
|
|
|
|
|
|
|
|
# * :r removes the file extension
|
|
|
|
|
|
|
|
# * :l makes it all lowercase
|
|
|
|
|
|
|
|
language_name="${${file_basename#[0-9]*_}:r:l}"
|
|
|
|
|
|
|
|
echo "Found subtitle file ${fname}."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if [[ $LANGUAGE_NAMES[$language_name] ]]; then
|
|
|
|
|
|
|
|
language_code=$LANGUAGE_NAMES[$language_name]
|
|
|
|
|
|
|
|
echo "Checking if subtitle file ${fname} is usable."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: This is absolutely an ugly way to do this, but this is what works.
|
|
|
|
|
|
|
|
if $(echo ${existing_language_codes} > grep ${language_code}); then
|
|
|
|
|
|
|
|
subtitle_options_array+=("--language" "0:${language_code}" "$fname")
|
|
|
|
|
|
|
|
existing_language_codes+=$language_code
|
|
|
|
|
|
|
|
echo "Found ${language_code} subtitles at ${fname}."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
echo "Duplicate subtitle files for ${language_code}."
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
echo "Unknown language name ${language_name}."
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Finally, actually mux all the subtitles in if there are any.
|
|
|
|
# Finally, actually mux all the subtitles in if there are any.
|
|
|
|
@ -48,7 +93,7 @@ _mkvmerge_add_subtitles_mkvmerge () {
|
|
|
|
echo "No subtitles found for file ${original_file}"
|
|
|
|
echo "No subtitles found for file ${original_file}"
|
|
|
|
else
|
|
|
|
else
|
|
|
|
echo "Remuxing ${original_file}..."
|
|
|
|
echo "Remuxing ${original_file}..."
|
|
|
|
mkvmerge --quiet -o "$partial_file" "$original_file" ${(@)subtitle_options_array} \
|
|
|
|
mkvmerge --quiet -o "$partial_file" "$original_file" ${^subtitle_options_array} \
|
|
|
|
|| rm "$partial_file"
|
|
|
|
|| rm "$partial_file"
|
|
|
|
echo "Done!"
|
|
|
|
echo "Done!"
|
|
|
|
|
|
|
|
|