Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const path = require('path');
- const https = require('https');
- const parser = require('fast-xml-parser');
- let file;
- let baseURL;
- const langs = {};
- const options = {
- ignoreAttributes : false,
- parseNodeValue : true,
- parseAttributeValue : true,
- };
- if(process.argv[2]){
- loadFile(process.argv[2]);
- }
- else {
- error(`Usage: node ${path.basename(__filename)} [MDP url]`);
- }
- function error(message){
- console.log("\x1b[31m", message, "\x1b[0m");
- process.exit();
- }
- function loadFile(url) {
- baseURL = url.substr(0, url.lastIndexOf("/")+1);
- https.get(url, function (res) {
- var data = '';
- res.on('data', function (chunk) {
- data += chunk;
- });
- res.on('end', function () {
- readXML(data);
- });
- }).on('error', function (err) {
- error(err.message);
- });
- }
- function readXML(xml){
- const data = parser.parse(xml, options);
- if(data.MPD?.Period?.AdaptationSet){
- data.MPD.Period.AdaptationSet.forEach((adaptation) => {
- if(adaptation["@_contentType"]=="text"){
- langs[adaptation["@_lang"]]=adaptation.Representation.SegmentTemplate["@_media"];
- }
- });
- selectLang();
- }
- else {
- error("Invalid XML");
- }
- }
- function selectLang(){
- console.log("Availaible languages:");
- for (const lang in langs) {
- console.log(lang);
- }
- const readline = require('readline').createInterface({
- input: process.stdin,
- output: process.stdout
- });
- readline.question('Select language: ', lang => {
- downloadLanguage(lang);
- readline.close();
- });
- }
- function downloadLanguage(lang) {
- console.log(`Donwloading ${lang}...`);
- file = fs.createWriteStream(`${lang}.vtt`, { flags: 'a' });
- download(lang);
- }
- function download(lang, count=1) {
- const url = baseURL+langs[lang].replace("$Number$",count);
- https.get(url, function(res) {
- if(res.statusCode==200){
- res.on('data', (chunk) => {
- file.write(chunk);
- });
- res.on('end', () => {
- download(lang, count+1);
- });
- }
- else {
- console.log("Done!");
- file.end();
- selectLang();
- }
- }).on('error', function(err) {
- file.end();
- error(err.message);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement