Advertisement
Guest User

hls.js

a guest
Apr 4th, 2020
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const axios = require("axios");
  2. var download = require("./download");
  3. const fs = require("fs");
  4. var functions = require("./functions");
  5. var folder = require("./folder");
  6. const os = require("os");
  7. const shell = require("shelljs");
  8. shell.config.silent = true;
  9. async function download_hls(link,path) {
  10.     return await new Promise((resolve,reject) => {
  11.         var schema = link.split("/")[0].replace(/:/,"");
  12.         var domain = link.split("/")[2];
  13.         var id = link.split("?id=")[1];
  14.         if(id.includes("&")) id = id.split("&")[0];
  15.         if(id) {
  16.             axios({
  17.                 method:"POST",
  18.                 url: `${schema}://${domain}/vl/${id}`,
  19.                 headers: {
  20.                     "origin": `${schema}://${domain}`
  21.                 }
  22.             }).then(async(response) => {
  23.                 var json = response.data;
  24.                 var quality = "";
  25.                 if(json.hasOwnProperty("2080p")) quality = "2080p";
  26.                 else if(json.hasOwnProperty("1080p")) quality = "1080p";
  27.                 else if(json.hasOwnProperty("720p")) quality = "720p";
  28.                 else if(json.hasOwnProperty("480p")) quality = "480p";
  29.                 else if(json.hasOwnProperty("360p")) quality = "360p";
  30.                 if(quality) {
  31.                     var md5 = json[quality].md5;
  32.                     var data = json[quality].data;
  33.                     var array = data[2];
  34.                     var file = [...new Set(array)];
  35.                     let dir = `./hls/${id}`;
  36.                     if (!fs.existsSync(dir)){
  37.                         fs.mkdirSync(dir, { recursive: true });
  38.                     }
  39.                     var concat = "";
  40.                     for(var i=0;i<file.length;i++) {
  41.                         await functions.sleep(1000);
  42.                         let tried = 0, check = false;
  43.                         let url = await axios.get(`${schema}://${domain}/getChunkLink?chunkFile=${md5}-chunk-${file[i]}.txt&t=${functions.timenow()}&mid=${md5}`,{
  44.                             headers:{
  45.                                 "referer":`${schema}://${domain}/public/dist/index.html?id=${id}`
  46.                             }
  47.                         }).then((api) => {
  48.                             return api.data;
  49.                         });
  50.                         let filename = `${dir}/${i}.mp4`;
  51.                         console.log(url);
  52.                         url = `https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?s=e23055c9ba7686b8fe583fb8318a1f88&container=focus&refresh=31536000&url=${encodeURIComponent(url)}`;
  53.                         if(os.type().includes("Windows")) var cmd = `wget.exe "${url}" -O "${filename}"`;
  54.                         else var cmd = `wget "${url}" -O "${filename}"`;
  55.                         console.log(`Bắt đầu download ${filename}`);
  56.                         console.log(cmd);
  57.                         shell.exec(cmd);
  58.                         while(functions.get_file_size(filename) < 100000 && tried < 3) {
  59.                             tried++;
  60.                             console.error(`Download ${filename} lỗi, đang thử lại lần ${tried}`);
  61.                             // fs.appendFileSync("./error.txt",url);
  62.                             await functions.sleep(1000);
  63.                             shell.exec(cmd);
  64.                         }
  65.                         if(functions.get_file_size(filename) > 100000) check = true;
  66.                         if(check) concat += `file '${i}.mp4'\r\n`;
  67.                     }
  68.                     fs.writeFileSync(`${dir}/file.txt`, concat, { flag: 'w' });
  69.                     if (fs.existsSync(path)){
  70.                         fs.unlinkSync(path);
  71.                     }
  72.                     if(os.type().includes("Windows")) var cmd = `ffmpeg.exe -f concat -safe 0 -i "${dir}/file.txt" -codec copy "${path}"`;
  73.                     else var cmd = `ffmpeg -f concat -safe 0 -i "${slug}/file.txt" -codec copy "${path}"`;
  74.                     // console.log(cmd);
  75.                     shell.exec(cmd);
  76.                     await folder.delete_folder(dir);
  77.                     console.log(`Download thành công ${path}`);
  78.                     resolve({path,quality});
  79.                 } else {
  80.                     console.error("ko lấy đc quality");
  81.                     reject();
  82.                 }
  83.             }).catch((err) => {
  84.                 console.error(err);
  85.                 reject();
  86.             })
  87.         } else {
  88.             reject();
  89.         }
  90.     })
  91. }
  92. module.exports = download_hls;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement