Rename Subtitle file to Match Video file with Partial Match

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

Rename Subtitle file to Match Video file with Partial Match

Postby LuckyDay » Thu Apr 01, 2021 7:59 pm

Hey folks, before I fully commit myself to trying to learn javascript, I figure I should check with the pros and see if something like this already exists. I recently downloaded all the subtitles for an old TV show that I have on our NAS. I muxed those subtitles without issue into the mp4 files, but as it turns out, my roku doesn't care for that. It wants separate .srt files that have the exact same name (case sensitive) as the mp4 file.

Unfortunately, the subtitle files are listed in alphabetical order so I can't just use a quick Autohotkey script to Alt+Tab between the folders and rename via copy/paste. I need a way to have it start with the first file in my Episode list (Folder 1) and use the episode title to find a match within the Subtitle folder and rename it.

If it was really fancy, it would rename exact matches, but if it wasn't identical, it would append a prefix ~ (to have it appear at the top) and append the renamed file to the old file name so it can be verified manually...not sure if that makes sense. My concern is if there are episode names that are similar or have the same title with a "Part 2" it would rename a file that's already been renamed. Maybe a different prefix in front (_) that tells the script to ignore it so it doesn't rename a file more than once? It would be easy to remove those prefixes after review.

Anyway, thanks in advance for your time and consideration, folks!

Example:
Folder 1 (Episodes 1-150)
TVShowName - S01E01 - Dark Side of the Moon.mp4
TVShowName - S01E02 - Take Me Out to the Ball Game.mp4
TVShowName - S01E03 - It's Beginning to Look a lot Like Christmas.mp4
TVShowName - S01E04 - Fourth Episode of this Show.mp4
...

Folder 2 (Subtitles)
Dark Side of the Moon-XIENFJVKDEFAEF.en.srt
Fourth Episode of this Show-RANDOMLETTERS.en.srt
It's Beginning to Look a lot Like Christmas-FJKDEIFf345FF.en.srt
Take Me Out to the Ball Game.en.srt
...

Folder 2 (Subtitles) After Script is Run
TVShowName - S01E01 - Dark Side of the Moon.srt
TVShowName - S01E02 - Take Me Out to the Ball Game.srt
TVShowName - S01E03 - It's Beginning to Look a lot Like Christmas.srt
TVShowName - S01E04 - Fourth Episode of this Show.srt
...

I guess they could all be in the same folder and just compare .mp4 against .srt extensions. This was just easier for me to visualize and explain this way.
LuckyDay
 
Posts: 2
Joined: Thu Apr 01, 2021 7:28 pm

Re: Rename Subtitle file to Match Video file with Partial Match

Postby LuckyDay » Thu Apr 01, 2021 10:11 pm

So I got a little impatient and ended up using Autohotkey after all. Now, this only worked well for me in this scenario because I was certain the episode names were identical to those in the subtitle files. I also knew that there were exactly 150 mp4s that matched with exactly 150 srt files. So, if/when I decide to get subtitles for more of my shows, I may not be so lucky and I really want to investigate/learn about how I can use BRU more effectively.

Here's the script I used - i tried to slow it down so I could verify the renaming as it ran. Before running, I made sure to have both folders, one with the mp4's, the other with the subtitles, open and ready to Alt+tab between.

Code: Select all
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

onClipboardChange:
episodeTitle := substr(clipboard, 18) ; extracts title from mp4 saves to variable

SetKeyDelay, 100

#z:: ; Windows + Z Keyboard Combination to start Loop
Loop, 150 ; 150 iterations
{
Sleep 500
SendInput {F2}; F2 Key to Rename (for copying)
Sleep 500
SendInput ^c ; Ctrl+C to Copy
Sleep 500
SendInput !{tab} ; Alt+Tab to go to other window (folder)
Sleep 500
SendInput %episodeTitle% ;  types out episode title to highlight/select subtitle
Sleep 500
SendInput {F2} ; F2 Key to Rename
Sleep 500
SendInput ^v ; Paste copied file name from first window
Sleep 500
SendInput !{tab} ; Return to original MP4 list window
Sleep 500
SendInput {Down} ; Go to next file before loop starts again
}
Return

~#!x::ExitApp ; Win+Alt+x to Exit App
LuckyDay
 
Posts: 2
Joined: Thu Apr 01, 2021 7:28 pm


Return to Javascript Renaming


cron