-2

I have a folder containing many videos that i'd like to rename. I can't think of any convenient way of doing so. The naming convention is the following "SeasonX, EpisodeY: Episode name". This is going to be "SXEY:Name" for short.

An example: S01E01:JavaCode That would be Season One, Episode One of Episode called JavaCode.

I wrote something that is able to change the file names, but I need different and unique file names for every episode because it's a TV show.

Here's the code:

import java.io.File;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BatchFileRenamer {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        File folder = new File("C:\\Users\\Tony\\Videos\\New folder");
        TreeMap map = new TreeMap();
        String name = "name";

        File[] files = folder.listFiles();

        Pattern p = Pattern.compile("\\..*");

        for (int i = 0; i != files.length; i++) {
            Matcher m = p.matcher(files[i].getName());
            System.out.println(files[i].getName());
            m.find();
            files[i].renameTo(new File(folder.getAbsolutePath() + "\\" + name + " S01E" +
                    (i < 10 ? "1" : "") + i + m.group()));
        }
    }
}

I was thinking of creating an array containing the episode names but that's just as much work as manually renaming them in Windows. I guess if I had a txt file to download for all the TV shows with the names of the episodes in it it'd be useful.

Anyway, any suggestions would be greatly appreciated!

vlaxmi
  • 468
  • 4
  • 18
  • Could you obtain the episode names by polling, say, IMBD or Wikipedia? – David Rawson May 04 '17 at 02:02
  • Do the 'random' names need to be meaningful? – Chris May 04 '17 at 02:02
  • Chris, the random names do have to be meaningful. I have to follow naming convention for my media center at home and i'd like to be able to have my personalized media. As for what David has to say, that is a great suggestion! Any idea how I can achieve that? – SuppahHacka May 04 '17 at 02:03
  • @SuppahHacka There are a lot of tutorials on how to connect to an API using Java. And as for the API itself: [Does IMDB provide an api?](http://stackoverflow.com/questions/1966503/does-imdb-provide-an-api) – David Rawson May 04 '17 at 02:20
  • 1
    `i < 10 ? "1" : ""` shouldn't this be a `0`? That way you get `"01", "02", ..., "09", "10"`. – Obicere May 04 '17 at 02:22
  • @DavidRawson Thanks! Seems overwhelming but i'll try to wrap my head around it. – SuppahHacka May 04 '17 at 02:27
  • @Obicere Oh right, well if I want it to start from 0, i'd have to just take out the one and leave both quotation marks empty. Works as intended – SuppahHacka May 04 '17 at 02:30

1 Answers1

0

I think the best way to do this would be to use the Open Movie Database API. With this, you can get a REST response including a list of episodes for each season of a show. (Example request).

With this, you could use Gson or another parser to serialize the list of episodes:

Here is a Gist of some sample code. (There is probably a better getter method, but you get the point)

What the code does is it gets the information from the sample request above via the API, then it serializes it into a basic POJO from the Episodes.java class using Gson:

Gson gson = new Gson();
Episodes episodes = gson.fromJson(download, Episodes.class);
System.out.println(episodes);

You can then use this information to create the individual file names for the video files.

werdna3232
  • 94
  • 1
  • 2
  • 9
  • Interesting answer! I was able to find my desired TV show on the API and generate the request. Now in Java, i'd need something like the Gson jar or Jsoup, right? That's where it gets a little cloudy for me. I tried importing packages like org.jsoup.Jsoup; but the package was not found. – SuppahHacka May 04 '17 at 13:43
  • Alright, so I got the code running nicely! One last problem however. I can't find the episode list for my desired show. If you can help me out, it's Cowboy Bebop. Maybe i'm not searching in the right place? – SuppahHacka May 04 '17 at 14:11
  • Final update. It's all working now! Just need to manipulate the data pulled and use it in a batch file renamer I wrote. Thanks for the suggestion once again! – SuppahHacka May 04 '17 at 14:28
  • Yep, you bet. If you don't mind marking the question as answered, that would be great! – werdna3232 May 04 '17 at 16:23