💾 Archived View for kallinen.xyz › shell-script-to-rename-subs captured on 2022-01-08 at 13:38:30. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Shell script to rename subs

https://drewdevault.com/2020/12/12/Shell-literacy.html

After reading this blog post, I have been meaning to learn shell scripting. What usually is the best motivator for me to learn something new, is solving problems.

I've been writing simple shell scripts to wrap or easily launch applications with specific settings, but this is the first time I wrote something to do real work.

The Problem

Subtitle files usually come with their own naming conventions and obviously this makes it so that mpv does not pick the right subtitle file for the video that I am watching.

Solution

To solve this problem I decided to write a simple script that takes the names of the video files and subtitle files (hopefully both are in a chronological order), and then renames the subtitle files based on the names of the video files.

#!/bin/sh

mkvs="$(find *.mkv)"
subs="$(find ./subs/*.srt)"

while
	mkv="$(sed -n 1p <<EOF
$mkvs
EOF
)"

	mkvs="$(sed '1d' <<EOF
$mkvs
EOF
)"

	sub="$(sed -n 1p <<EOF
$subs
EOF
)"

	subs="$(sed '1d' <<EOF
$subs
EOF
)"

[ -n "$mkv" ]
do
	echo "mkv=$mkv"
	echo "sub=$sub"
	mv "$sub" ./subs/"$(echo "${mkv%.*}")".ja.srt
done

This script will only work on `.mkv` files and `.srt` subtitles. I might update it to check that the list of subtitles is as long as the list of videos, and to also work with `.ass` files, if I need that.