Last active
March 2, 2022 17:42
-
-
Save m3hransh/a96d2b97d8195b83d829df278086028b to your computer and use it in GitHub Desktop.
Renaming sub files of series according to their video file names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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