Skip to content

Instantly share code, notes, and snippets.

@mitio
Last active August 12, 2018 19:11
Show Gist options
  • Save mitio/0f31cab22e5e502297a9a3230050476a to your computer and use it in GitHub Desktop.
Save mitio/0f31cab22e5e502297a9a3230050476a to your computer and use it in GitHub Desktop.
fix-subtitle-encoding.sh & reencode-with-subs.sh

Example usage

Full example:

QMIN=1 QMAX=4 RESOLUTION=480 SUBTITLE_OPTIONS='FontName=Arial,FontSize=22,OutlineColour=&H10000000,BorderStyle=3' reencode-with-subs mkv

Quality control:

QMIN=1 QMAX=4

Constrain the (vertical) resolution:

RESOLUTION=480

Use all CPU cores:

CUSTOM_OPTIONS='-threads 0'

Take only the first audio stream:

CUSTOM_OPTIONS='-map 0:v -map 0:a:0'

Subtitle rendering options – large font with a semi-opaque background:

SUBTITLE_OPTIONS='FontName=Arial,FontSize=22,OutlineColour=&H10000000,BorderStyle=3'

Same, but with subtitles at the top of the screen:

SUBTITLE_OPTIONS='FontName=Arial,FontSize=22,OutlineColour=&H10000000,BorderStyle=3,Alignment=6'
find . -type f -name '*.srt' | grep -v '/\._' | grep -v original-subs | sort > subs
mkdir -p original-subs
IFS="
"
for f in `cat subs`; do
echo working on $f
name=`basename "$f"`
current_encoding=`file --mime-encoding --brief "$f"`
if [ "$current_encoding" = "utf-8" ]; then
echo "Skipping UTF-8 subtitles..."
continue
fi
mv "$f" "original-subs/$name"
iconv -f cp1251 -t utf8 "original-subs/$name" > $name
done
rm -f subs
set -x
extension="$1"
if [ -z "$extension" ]; then
extension="avi"
fi
[ -z "$QMIN" ] && QMIN=1
[ -z "$QMAX" ] && QMAX=2
if [ ! -z "$RESOLUTION" ]; then
VIDEO_FILTERS="scale=-2:$RESOLUTION,setsar=1/1"
else
VIDEO_FILTERS=""
fi
find . -type f -name "*.$extension" | grep -v '/\._' | grep -v original-videos | sort > videos
mkdir -p original-videos
original_ifs="$IFS"
IFS="
"
for f in `cat videos`; do
echo working on $f
name=`basename "$f"`
folder=`dirname "$f"`
name_without_ext=`echo "$name" | sed "s/\.$extension\$//"`
subs="$folder/$name_without_ext.srt"
mv -v "$f" "original-videos/$name"
current_video_filters="$VIDEO_FILTERS"
if [ -f "$subs" ]; then
echo "Re-rendering $f WITH subs..."
extra_subtitle_options=""
if [ ! -z "$SUBTITLE_OPTIONS" ]; then
extra_subtitle_options=":force_style='$SUBTITLE_OPTIONS'"
fi
subtitles_filter="subtitles=${subs}${extra_subtitle_options}"
if [ ! -z "$VIDEO_FILTERS" ]; then
current_video_filters="$VIDEO_FILTERS,$subtitles_filter"
else
current_video_filters="$subtitles_filter"
fi
ffmpeg -threads 0 -i "original-videos/$name" -c:v libxvid -vtag XVID $CUSTOM_OPTIONS -vf $current_video_filters -qmin $QMIN -qmax $QMAX -c:a mp3 -b:a 128k -s:a 48000 -y "$folder/$name_without_ext.avi"
else
echo "Re-rendering $f WITHOUT subs..."
if [ ! -z "$VIDEO_FILTERS" ]; then
current_video_filters="-vf $VIDEO_FILTERS"
else
current_video_filters=""
fi
IFS="$original_ifs"
ffmpeg -threads 0 -i "original-videos/$name" -c:v libxvid -vtag XVID $CUSTOM_OPTIONS $current_video_filters -qmin $QMIN -qmax $QMAX -c:a mp3 -b:a 128k -s:a 48000 -y "$folder/$name_without_ext.avi"
IFS="
"
fi
done
rm -f videos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment