-1

Currently I am using Advanced Renamer script method (it gives a window to write your own JavaScript) to write my custom script, but with this I don't think I will be able to use IMDB API or any other resources other than writing some script to remove dots, dates, capitalize letters.

So my question is, from where should I start if I want to make a file renamer program for TV shows and movies? I know there are already many tool available on net, but I want to make my own; if it is possible with PHP and JavaScript then it will be great.

tshepang
  • 12,111
  • 21
  • 91
  • 136
BluSky
  • 83
  • 1
  • 1
  • 7

1 Answers1

3

I suggest you to use libraries for this. There is lm which uses imdbpy and is like the ls command, but for movies and it's written in python, I did not test it. There's also guessit

You could do everything from your own too, but I personally think this is a big challenge.

First, you need a way to parse movie titles from unreadable movie names (by guessing them), than, you want to send the titles to the imdb api, once you think the answers are ok, you have to ask your program to rename original files.

Doing everything in php and javascript is possible, but I think it is easier to accomplish in a language like python. I started something in php, but at the end of this answer, there is a well working solution with python and guessit ;)

I used omdbapi because there's no documentation for the imdb api, see on stack overflow

PHP and javascript example

Let's say you have a movies directory filled with movie files; run this in a terminal if you want to simulate things a little

touch Iron.Man.and.Captain.America.Heroes.United.2014.1080p.WEB-DL.x26.mkv
touch Batman.Assault.on.Arkham.2014.1080p.WEB-DL.x264.AAC-RARBG.mkv
touch The.Great.Hypnoti.2014.1080p.WEB-DL.x264.AAC-SeeHD.mkv
touch Taxi!\ Taxi!\ 2013\ 1080p\ WEB-DL\ x264\ AAC-SeeHD.mkv
touch Everybody\'s\ Business.2013.1080p.WEB-DL.x264.AAC-SeeHD.mkv
touch Jack.Frost.1998.1080p.WEB-DL.DD5.1.H.264.HKD.mkv
touch How.High.2001.1080p.WEB-DL.AC3.x264-FraMeSToR.mkv
touch Crazy.Sexy.Cool.The.TLC.Story.2013.1080p.WEB-DL.AC3.x264-FraMeST.mkv
touch Batman.Assault.on.Arkham.2014.1080p.WEB-DL.DD5.1.H264-RARBG.mkv
touch Iron.Man.and.Captain.America.Heroes.United.2014.1080p.WEB-DL.DD5.mkv
touch A.Haunted.House.2.2014.1080p.WEB-DL.DD5.1.H264-RARBG.mkv
touch Batman.Assault.on.Arkham.2014.1080p.WEB-DL.DD5.1.H.264-YFN.mkv
touch Puff.Puff.Pass.2006.1080p.WEB-DL.AC3.x264-FraMeSToR.mkv
touch My.Man.Is.a.Loser.2014.1080p.WEB-DL.AC3.x264-FraMeSToR.mkv
touch Welcome.Home.Roscoe.Jenkins.2008.1080p.WEB-DL.AC3.x264-FraMeSToR.mkv

Note: I don't even know most of these movies, those are only filenames, I didn't really care about finding a good list of movie files. Oh and pay for the movies you like ;)

Ok now, here's a awesome index.php with a working sample example:

<!doctype html>
<html>
<head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>
        // magical local jquery fallback
        if (!window.jQuery) {
            document.write('<script src="js/jquery-2.1.1.min.js"><\/script>');
        }
    </script>
    <script type="text/javascript" src="js/app.js"></script>
</head>

<body>
    <?php
    $dir    = './movies';

    // returns an array of only files (won't show ".", ".." and folders)
    $files = array_filter(scandir($dir), function($item) {
        return !is_dir($item);
    });

    echo "<ul id='movies'>";

    // very dumb name extraction
    foreach ($files as $file => $value) {
        // remove extension
        $files[$file] = preg_replace('/\\.[^.\\s]{3,4}$/', '', $value);
        // replace dots with spaces
        $files[$file] = str_replace('.', ' ', $files[$file]);
        // keep name before year
        $files[$file] = preg_split("/\d{4}/", $files[$file])[0];
        // display on page with data-attribute filename
        echo '<li class="file"><a href="#" data-filename=\'' . $files[$file] . '\')">' . $files[$file] . '</a></li>';
    }
    echo "</ul>";
    ?>
</body>
</html>

And here's the /js/app.js:

$( document ).ready(function() {
    get_imdb_data = function (strMovieTitle, where_to_show_poster_selector) {
        $.ajax({
            url: "http://www.omdbapi.com/?t=" + strMovieTitle,
            dataType: 'jsonp',
            success: function(results){
                console.log(results);
                where_to_show_poster_selector.append("<img name='" + results['Title'] + "' src='" + results['Poster'] + "'/>");
            }
        });
    }

    $('#movies .file a').each(function() {
        get_imdb_data($(this).data('filename'), $('body'));
    });

});

Result

All of this will result in this ugly page ;)

imdb api kickstart usage example

It's not working very well, some pictures are missing, probably because of the name guessing part or because I chose really bad movie names, anyway.

chrome console picture not found imdb stuff

It is not currently renaming files, but you could easily do something in php with the rename function.

using guessit

pip install guessit
ls movies | xargs -I {} guessit {}

output

For: movies:
GuessIt found: {
    [1.00] "type": "movie",
    [0.60] "title": "movies"
}
For: A.Haunted.House.2.2014.1080p.WEB-DL.DD5.1.H264-RARBG.mkv
GuessIt found: {
    [1.00] "mimetype": "video/x-matroska",
    [1.00] "videoCodec": "h264",
    [1.00] "container": "mkv",
    [0.60] "title": "A Haunted House 2",
    [1.00] "format": "WEB-DL",
    [1.00] "releaseGroup": "RARBG",
    [1.00] "audioChannels": "5.1",
    [1.00] "screenSize": "1080p",
    [1.00] "year": 2014,
    [1.00] "type": "movie",
    [1.00] "audioCodec": "DolbyDigital"
}
For: Batman.Assault.on.Arkham.2014.1080p.WEB-DL.DD5.1.H.264-YFN.mkv
GuessIt found: {
    [1.00] "mimetype": "video/x-matroska",
    [1.00] "videoCodec": "h264",
    [1.00] "container": "mkv",
    [1.00] "format": "WEB-DL",
    [0.40] "series": "Batman Assault on Arkham",
    [1.00] "releaseGroup": "YFN",
    [1.00] "audioChannels": "5.1",
    [1.00] "screenSize": "1080p",
    [1.00] "year": 2014,
    [1.00] "type": "episode",
    [1.00] "audioCodec": "DolbyDigital"
}
For: Batman.Assault.on.Arkham.2014.1080p.WEB-DL.DD5.1.H264-RARBG.mkv
GuessIt found: {
    [1.00] "mimetype": "video/x-matroska",
    [1.00] "videoCodec": "h264",
    [1.00] "container": "mkv",
    [0.60] "title": "Batman Assault on Arkham",
    [1.00] "format": "WEB-DL",
    [1.00] "releaseGroup": "RARBG",
    [1.00] "audioChannels": "5.1",
    [1.00] "screenSize": "1080p",
    [1.00] "year": 2014,
    [1.00] "type": "movie",
    [1.00] "audioCodec": "DolbyDigital"
}
For: Batman.Assault.on.Arkham.2014.1080p.WEB-DL.x264.AAC-RARBG.mkv
GuessIt found: {
    [1.00] "mimetype": "video/x-matroska",
    [1.00] "videoCodec": "h264",
    [1.00] "container": "mkv",
    [0.60] "title": "Batman Assault on Arkham",
    [1.00] "format": "WEB-DL",
    [1.00] "releaseGroup": "RARBG",
    [1.00] "screenSize": "1080p",
    [1.00] "year": 2014,
    [1.00] "type": "movie",
    [1.00] "audioCodec": "AAC"
}
For: Business.2013.1080p.WEB-DL.x264.AAC-SeeHD.mkv
GuessIt found: {
    [1.00] "mimetype": "video/x-matroska",
    [1.00] "videoCodec": "h264",
    [1.00] "container": "mkv",
    [0.60] "title": "Business",
    [1.00] "format": "WEB-DL",
    [1.00] "releaseGroup": "SeeHD",
    [1.00] "screenSize": "1080p",
    [1.00] "year": 2013,
    [1.00] "type": "movie",
    [1.00] "audioCodec": "AAC"
}
For: Crazy.Sexy.Cool.The.TLC.Story.2013.1080p.WEB-DL.AC3.x264-FraMeST.mkv
GuessIt found: {
    [1.00] "mimetype": "video/x-matroska",
    [1.00] "videoCodec": "h264",
    [1.00] "container": "mkv",
    [0.60] "title": "Crazy Sexy Cool The TLC Story",
    [1.00] "format": "WEB-DL",
    [1.00] "releaseGroup": "FraMeST",
    [1.00] "screenSize": "1080p",
    [1.00] "year": 2013,
    [1.00] "type": "movie",
    [1.00] "audioCodec": "AC3"
}
xargs: unterminated quote

I don't have a solution for the xargs: unterminated quote, this doesn't happen when using guessit as a python module in a python script.

TLDR; Solution using python and guessit

After playing around with everything, I did this python loop and it works very well ;)

import guessit, sys

import os
for root, dirs, filenames in os.walk("./movies"):
    for filename in filenames:
        data = guessit.guess_movie_info(filename, info=['filename'])
        if data.confidence('title') > 0.5:
            extension = os.path.splitext(filename)[1]
            new_filename = data['title'] + " (" + str(data['year']) + ")" + extension
            os.rename(os.path.join(root, filename), os.path.join(root, new_filename))
            print "renamed", filename, "to" + new_filename
        else:
            print 'not sure for', filename, data['title']
            print "confidence:", data.confidence('title')

before

A.Haunted.House.2.2014.1080p.WEB-DL.DD5.1.H264-RARBG.mkv
Batman.Assault.on.Arkham.2014.1080p.WEB-DL.DD5.1.H.264-YFN.mkv
Batman.Assault.on.Arkham.2014.1080p.WEB-DL.DD5.1.H264-RARBG.mkv
Batman.Assault.on.Arkham.2014.1080p.WEB-DL.x264.AAC-RARBG.mkv
Business.2013.1080p.WEB-DL.x264.AAC-SeeHD.mkv
Crazy.Sexy.Cool.The.TLC.Story.2013.1080p.WEB-DL.AC3.x264-FraMeST.mkv
Everybody's Business.2013.1080p.WEB-DL.x264.AAC-SeeHD.mkv
How.High.2001.1080p.WEB-DL.AC3.x264-FraMeSToR.mkv
Iron.Man.and.Captain.America.Heroes.United.2014.1080p.WEB-DL.DD5.mkv
Iron.Man.and.Captain.America.Heroes.United.2014.1080p.WEB-DL.x26.mkv
Jack.Frost.1998.1080p.WEB-DL.DD5.1.H.264.HKD.mkv
My.Man.Is.a.Loser.2014.1080p.WEB-DL.AC3.x264-FraMeSToR.mkv
Puff.Puff.Pass.2006.1080p.WEB-DL.AC3.x264-FraMeSToR.mkv
The.Great.Hypnoti.2014.1080p.WEB-DL.x264.AAC-SeeHD.mkv
Welcome.Home.Roscoe.Jenkins.2008.1080p.WEB-DL.AC3.x264-FraMeSToR.mkv

after

A Haunted House 2 (2014).mkv
Batman Assault on Arkham (2014).mkv
Business (2013).mkv
Crazy Sexy Cool The TLC Story (2013).mkv
Everybody's Business (2013).mkv
How High (2001).mkv
Iron Man and Captain America Heroes United (2014).mkv
Jack Frost (1998).mkv
My Man Is a Loser (2014).mkv
Puff Puff Pass (2006).mkv
The Great Hypnoti (2014).mkv
Welcome Home Roscoe Jenkins (2008).mkv

Whatch out tho, some Batman disapeared ;) (you may need to add some user prompt or check if file exist and naming it accordingly, etc).

Good luck and have fun ;)

Community
  • 1
  • 1
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
  • Thanks brother for giving a great explanation and heading me towards right direction, as suggested i will switch to python and start working. I appreciate your work. Thanks again. – BluSky Aug 03 '14 at 13:01
  • No problem, if this helped, you can mark the answer as accepted ;) ps, if you want your questions to be upvoted, try to give more detailed information, add links to the stuff you use and use [markdown syntax](http://stackoverflow.com/editing-help) efficiently. – GabLeRoux Aug 03 '14 at 15:09
  • Thanks GabLeRoux for the tip – BluSky Aug 05 '14 at 18:18