[Copy Siege Lion Log] Node quickly renames files to say goodbye to Potplay subtitles

Posted May 26, 20204 min read

0.png
↑ A picture at the beginning, the story is entirely edited ↑

Foreword

Copy the siege log convention, start with a picture, and start telling you a little-known story. The beginning of the story begins with the crime of downloading the udemy course from the pirated website by this big lion. One day last year, I really got in touch with "your university" udemy. 199 knives, because it happened that there was no discount, and then google all the way, and found the corresponding seed to a certain bt website. Think about it for a while, and then open a member of mine to talk about it in the next wave. Not to mention, the company ’s network is still quite explosive. The 17G file will be downloaded in a short time. Use the old driver tool PotPlay to play it. The results are all in English subtitles. Then "from entry to abandonment", just a few months ...

My English is poor

my-english-is-poor.jpg
When you see this title professionally, you can guess my "British history" level, yes, another little-known story is that I did not get the level 4 certificate, which has to mention me The ruined university era is still paying off debts for past efforts. At the beginning of the university, I thought about learning English well, probably because the English teacher deeply attracted me, and even added wx, and then it was gone, even the "hi" never blew. Afterwards, every year I passed the fourth level and hung every year. Except for the dormitory buddies, the buddies passed the final semester successfully under the "coercion and lure" of their girlfriends. But I, and English, went farther and farther, until I had to recognize and memorize the boring API names and proper nouns ... coming back to udemy, I personally feel that the above courses are still relatively powerful. I have seen some introductory courses. The lecturers are all talents, and the speech is good and the knowledge and ideas brought are relatively new. So I am quite satisfied with the downloaded learning materials, except that I can't understand English subtitles until I slowly give up.

PotPlay subtitles and translation

1png.png
Potplay is an old driver software, there are not many tables here. I was fascinated by the first use in 16 years, not only for formal learning but also special learning, which once caused nutrition to be even worse. Later, I installed the 18-year version on the left or never updated it again. It was not until recently that I learned that the new version has an online subtitle translation function. After learning some blog posts, it seems that I have mastered how to understand the plot of the film-real-time translation of subtitles. Coincidentally, the downloaded learning materials happen to have external subtitles, which meets the basic requirements for online translation of subtitles. The structure of videos without subtitles + external subtitles + online translation is more in line with the online translation of subtitles. The operation is also very simple. I use the "splashed cold water" AI, the standard version first, and the current experience is still good. Well-developed big brothers can go to a Google Cloud 300-dollar voucher to directly dock with Google Translator. Specific translation access can be used directly to explore.
2.png
Baidu translation access can refer to: PotPlayer \ _Subtitle \ _Translate \ _Baidu
Translation API configuration(estimated to register the corresponding platform account and activate the service):
3.png

\ *-en.srt rollover

In fact, there has always been a problem that bothers me. For the same file, the old version can display the subtitles normally, but the new version cannot display the subtitles normally. Through careful comparison, it was found that the old version of the player can normally read \ *-en.srt subtitle files, while the new version of the player cannot and is considered to be a subtitle of another video file. When I removed the -en in the file name of the subtitle file, I found The new version of the player can also display subtitles normally and also automatically translated.
4.png
As for whether it is a bug or not, I have no plans to delve into it. The problem is very clear, because the subtitle file name and the video file name do not match, resulting in the player can not automatically recognize. One way to solve the problem is to copy and rename all subtitle files, replacing the last "-en" in the file name with a blank character. Now that the problems and solutions are available, it's time to perform the true Copy technology ...

Node batch processing file name

The big brother on the opposite side "Life is short, he uses Python", and I "longevity, I use NodeJS". Direct Copy Code:

const fs = require("fs");
const path = require("path");
const util = require("util");
const readdir = util.promisify(fs.readdir);
const stat = util.promisify(fs.stat);
const timeStart = new Date();
const filePath = path.resolve("./");

function readDirRecur(file, callback) {
  return readdir(file) .then((files) => {
    files = files.map((item) => {
      let fullPath = file + "/" + item;
      return stat(fullPath) .then((stats) => {
        if(stats.isDirectory()) {
          return readDirRecur(fullPath, callback);
        } else {
         /* not use ignore files * /
          if(item [0]== ".") {
            //console.log(item + 'is a hide file.');
          } else {
            callback && callback(fullPath);
          }
        }
      });
    });
    return Promise.all(files);
  });
}

readDirRecur(filePath, function(filePath) {
  //Only process .srt files. Process -en.srt as .srt
  if(path.extname(filePath) === ".srt") {
    let newPath = filePath.replace(/(.*)- en /, "$1");
    if(fs.existsSync(newPath)) {
      console.log("The path already exists");
      //fs.unlinkSync(newPath)
    } else {
      fs.copyFileSync(filePath, newPath);
    }
  }
})
  .then(function() {
    console.log("done", new Date()-timeStart); //done 3.3
  })
  .catch(function(err) {
    console.log(err);
  });

Processing results(fairly ideal):
5.png
6.png
Randomly opening a video can display bilingual subtitles normally:
7.png

Conclusion

It only took a few minutes to realize such a batch processing of subtitle files and display bilingual subtitles. As a result, it took me a whole morning to write this hydrology. It can be seen how much time and effort those high-yield gangsters have spent. Salute to you! Finally, Excuse me, do you have any disciples? I am the kind that will close the door!