-2

I´m trying to make a python script to rename the subtitles file (.srt) with the filename of the video file corresponding to that episode, so when I open it in VLC the subtitles are already loaded.

So far I´ve succeeded in getting all the file names from the videos and subs into strings on two separate lists. Now I need to somehow pair the video string of episode 1 with the subtitle corresponding to the same episode.

I´ve tried in lots of differents ways but I always,most with regular patterns, but none has worked. Here´s an example of my code :

import glob 

videoPaths = glob.glob(r"C:\Users\tobia_000\Desktop\BBT\Video\*.mp4")
subsPaths = glob.glob(r"C:\Users\tobia_000\Desktop\BBT\Subs\*.srt") 
#With glob I sort the filenames of video and subs in separate variables

ep1 = []
ep2 = []
ep3 = []
#etc..

This is how the videoPaths and subsPaths variables would look like with the files I have. I dont have them all yet.

videoPath = ["The.Big.Bang.Theory.S05E24.HDTV.x264-LOL.mp4",
"The.Big.Bang.Theory.S05E19.HDTV.x264LOL.mp4",
"The.Big.Bang.Theory.S05E21.HDTV.x264-LOL.mp4"]

subsPath = ["The Big Bang Theory - 5x19 - The Weekend Vortex.720p HDTV.lol.en.srt",
"The Big Bang Theory - 5x21 - The Hawking Excitation.HDTV.LOL.en.srt",
"The Big Bang Theory - 5x24 - The Countdown Reflection.HDTV.LOL.en.srt"]
TobiasP
  • 183
  • 2
  • 12
  • 3
    How the filenames look like ? – JazZ Aug 23 '16 at 23:11
  • Is the extension the only difference? If so `zip(sorted(glob(path1), sorted(glob(path2))))` – Padraic Cunningham Aug 23 '16 at 23:12
  • apart from the above two questions. Are the filenames in `videoPaths and `subsPaths` ordered appropriately. ? Is all you want renaming the filename extentions or is there more ? – Vasif Aug 23 '16 at 23:14
  • scratch that, the fact you are renaming means they cannot be the same or you would not need to rename. – Padraic Cunningham Aug 23 '16 at 23:17
  • 2
    It's pretty much a short in the dark but am thinking this other question would be useful for mathing video and subtitle files: https://stackoverflow.com/questions/17388213/python-string-similarity-with-probability – Eugen Constantin Dinca Aug 23 '16 at 23:19

1 Answers1

3

You can use zip to make the pairs, if the sorted lists for videoPaths and subsPaths are correct in the they correspond to each other. ie First episode video and subtitles are both the first element of each list.

episodes = list(zip(videoPaths, subsPaths))

Edit: and since glob returns result in an arbitrary order, sort before that.

episodes = list(zip(sorted(videoPaths), sorted(subsPaths)))
aneroid
  • 12,983
  • 3
  • 36
  • 66