Skip to content

Instantly share code, notes, and snippets.

@m3hransh
Last active March 2, 2022 17:42
Show Gist options
  • Save m3hransh/a96d2b97d8195b83d829df278086028b to your computer and use it in GitHub Desktop.
Save m3hransh/a96d2b97d8195b83d829df278086028b to your computer and use it in GitHub Desktop.
Renaming sub files of series according to their video file names
package main
import (
"log"
"os"
"regexp"
"strings"
)
func Renamify(path string) {
files, err := os.ReadDir(path)
if err != nil {
log.Fatal(err)
}
// Map will store S<num>E<num>
videos := make(map[string]string)
subs := make(map[string]string)
for _, f := range files {
re := regexp.MustCompile(".*(S|s)0*([1-9]+)(E|e)0*([1-9]+).*\\.((?:mkv)|(?:srt))")
ent := strings.Fields(re.ReplaceAllString(f.Name(), "$1$2$3$4 $5"))
if ent[1] == "mkv" {
videos[ent[0]] = f.Name()
}
if ent[1] == "srt" {
subs[ent[0]] = f.Name()
}
}
for key, sub := range subs {
os.Rename(path+sub, path+strings.Replace(videos[key], ".mkv", ".srt", 1))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment