Firstly, this question was made as a follow up of my previous question: How to batch rename files with "variables"?. I'm just restating it considering Sergey's recommendations.

I use xmbc to view my tv shows. Before I began using xmbc, I batch downloaded the first few seasons of "Bleach". What can I say, I'm an anime fan ;-). These are named: "bleachxx.mp4" where xx is the episode number in respect to the whole series, not to the season. so "bleach21.mp4" is the 1st episode of the second season and 21st overall. The files themselves are, however, divided into their own season folders.

The problem is that the command will rename all the files into "Bleach S0XExx" but because the files have the overall file numbers, bleach52.mp4 --> 'Bleach S03E52.mp4' when there obviously aren't 52 episodes in season 3.

To solve this problem, I would have to subtract the number of episodes in the previous seasons by the overall number and essentially get the specific season number.

Example: If there are 20 episodes in season 1, then 25-20=5 so the 25th episode is the 5th of season 2 and renaming would carry out as normal.

I'm a complete newb in coding, I just suggested a python script as I was told this was an effective way of solving this problem. Can somebody please help me in the step by step process of writing this script? Thanks!

  • There already is a rename script. Why reinvent the wheel? – Matt Oct 27 '11 at 2:00
  • The scripts if have tried (pyrename, the "rename" command and a plethora of "tv show organizing" scripts) can't do this, i.e. Changing the number value in a filename by a certain function/math operation. Do you know any script/program that can do this? – deox Oct 27 '11 at 2:25
  • Actually, it is a perl script..[Well, the one I'm talking about (rename) is] and now that I check the symlinks, on my system, it points to prename. And what do you mean? give me an example of a filename, and tell me what it needs to be renamed to. – Matt Oct 27 '11 at 2:37
  • So "bleach21.mp4" means S2 E01? "bleach53.mp4" means S5 E03? If the first number is the season ["5" in 53] and the second is the episode number, I think it is doable to rename them all. – Matt Oct 27 '11 at 2:44
  • 3
    @Matt: you don't want a person who describes themselves as "complete newb in coding" to mess with Perl, do you? :) – Sergey Oct 27 '11 at 2:45

This is off the top of the head, but try:

# call program as: epirename.py /path/to/mp4files 20 41 59
# the numbers are the sequence of first episodes in each season after the first
import os
import re
import sys
mp4dir = sys.argv[1]
seasons = [int(startnum) for startnum in sys.argv[2:]]
seasons.insert(0, 0) # start with '0' as the first episode of season 1
season = 0
patt = re.compile(r'([[:alpha:]]+)([[:digit:]]+).mp4')
for filename in sorted(os.listdir(mp4dir)):
    match = patt.search(filename)
    if match:
        episode = int(match.group(2))
            if episode >= seasons[season+1]:
                season += 1
        # create new filename as '{basename}S{season}E{episode}'
        newname = '%sS%02dE%03d.mp4' % (match.group(1), season+1, episode - seasons[season])
        os.rename(os.path.join(dir, filename),
                  os.path.join(dir, newname))
        print 'renamed', filename 'to', newname

The result is that the files that match '{name}{epi}.mp4' will be renamed to '{name}S{season}E{episode}.mp4'. Other files are left untouched.

Again, this is off the top of my head; there may be more efficient techniques.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.