Monday, September 5, 2011

Extracting movie title from torrent file name using Regular Expression

Movie files downloaded from torrent sites has file name which contains format types (like dvdrip, dvdscr, xvid etc), year, comments , user names and of course movie name. We want to extract movie name from this file names.

Input File namesOutput Achieved to zero

Nim's.Island[2008]DvDrip-aXXo.avinim's island



20000 Leagues Under The Sea.avi

Across The Universe.MoZinRaT CD1.aviacross the universe mozinrat

Adoration 2008 DvdRip ExtraScene RG.aviadoration

Amelie(English Dubbed).aviamelie






badder.santa.dvdrip.xvid-deity.avibadder santa

Balls of Fury[2007]DvDrip[Eng]-FXG.aviballs of fury

Bruno (2009) DVDRip-MAXSPEED

Defiance DvDSCR[2009] ( 10rating ).avidefiance

Down With Love (cute romantic comedy).avidown with love

Einstein.And.Eddington.2008.DVDRip.XviD.avieinstein and eddington


Based on the observation that

  • Most of the file name contains the format like 'dvdrip', 'xvid', 'brrip','dvdscr' or other words like 'CD1','(<year>)','[<year>]'  specified in the name and everything after any of this  words doesnot contains any useful data.

  • Sometimes extra information is added to file name inside bracket  like
    Defiance DvDSCR[2009] ( 10rating ).avi
    Down With Love (cute romantic comedy).avi
    so we can also ignore the part including and after brackets as movie names doesn't have brackets in them and have no useful information after it.

'(.*?)(dvdrip|xvid| cd[0-9]|dvdscr|brrip|divx|[\{\(\[]?[0-9]{4}).*'

This regular expression will find file names where we have dvdrip, brrip, xvid(we can specify any number of values here) or  year and finds first of any one of the appearing patterns because we have used lazy parsing here using .*?.  We then extract the first back referenced part \1.

Secondly, to remove the part within brackets we use

'(.*?)\(.*\)(.*)' regular expression.

Following code snippet gets the movie names(to an extent) from file name.

import re
fr = open('filenameslist.txt', 'r')
fw = open('movienames.txt', 'w')
for line in fr:
    text = line.strip()
text1 ='([^\\\]+)\.(avi|mkv|mpeg|mpg|mov|mp4)$', text)
if text1:
    text =
text = text.replace('.', ' ').lower()
text2 ='(.*?)(dvdrip|xvid| cd[0-9]|dvdscr|brrip|divx|[\{\(\[]?[0-9]{4}).*', text)
if text2:
    text =
text3 ='(.*?)\(.*\)(.*)', text)
if text3:
    text =
# print text
fw.write(text + '\n')

Output can be improved further observing things like we can replace characters like underscore with space, we can check for only four digits where next character is non word character ..


  1. Can you make the same for PHP please. :(

  2. Tried using but it takes everything out except the title and year so this string 'Balls of Fury[2007]DvDrip[Eng]-FXG' became Balls of Fury[2007]. How can i get it to get rid of the year part.

  3. @wolfman122
    try using

    import re
    text ='Balls of Fury[2007]DvDrip[Eng]-FXG'.strip()
    text2='(.*?)(dvdrip|xvid| cd[0-9]|dvdscr|brrip|divx|[\{\(\[]?[0-9]{4}).*',text)
    if text2:
    if text3:
    print text

    It will work. Actually, code mentioned in blog is expecting file extension also so when you don' mention file extension it fails at first IF clause itself.

  4. Thanks for the very helpful module to enhance my Movies Rater
    The new version now after your module is way more accurate since almost my application now can get ratings for any movie name with any format and the frequently noise in files names.

    Thanks again!

  5. Thanks Montaro! I am Glad to hear that.