Monday, June 22, 2015

Batch-Renaming Movies using Regex



I've been trying to rename some movie files using regular expressions, but so far I have been only marginally successful. The goal is to parse files like this:



2001.A.Space.Odyssey.1968.720p.BluRay.DD5.1.x264-LiNG.mkv



And rename them Like this:



2001 A Space Odyssey (1968).mkv


I created the pattern: ^(.+)\.(\d{4}).+\.(mp4|avi|mkv)$



With the output: \1 (\2).\3




Now, this works perfectly fine when I have movies with one-word titles, but when there is more than one word separated by a period, the regex fails to grab anything.



What am I doing wrong here?


Answer



Your pattern seems to work fine for me with that example filename and Perl as the regex engine:



$ echo '2001.A.Space.Odyssey.1968.720p.BluRay.DD5.1.x264-LiNG.mkv' | 
perl -npe 's/^(.+).(\d{4}).+.(mp4|avi|mkv)$/\1 (\2).\3/'

2001.A.Space.Odyssey (1968).mkv



The only thing I would change is to escape the .'s where you actually do want them to refer to a period and not a wildcard. In particular it's probably safe to assume the final period before the file extension is actually a period - I'm not sure about the one between the title and the year.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...