nodejs simple learning climbing sites ts video files into one video file ts

 Use nodejs, request packet timing climbing sites ts video interfaces, there are about 1771 files.

First, access to the site ts video segment configuration file, get to put it into a local file, to facilitate the next use.

Then, the timing function call download, download,

Paqu download process will fail, after all I crept finished, check the download fails again to download,

Finally, exec cmd command to execute the package into one file ts

const Request = the require ( "Request"); 
const FS = the require ( "FS"); 
const {Exec} = the require ( 'child_process'); 
const the EMPTYFILE = []; 
const failDownload = []; 

function reload (Results) { 
    results.forEach (EL => { 
        the let checkUrl = './download/' EL +; 
      // check whether the file already exists iF (fs.existsSync (checkUrl)) {
        // get the file information const r = fs.statSync (checkUrl );
        // If the size is 0, then the download failed IF (R & lt && r.size == 0) { emptyFile.push (EL);
          // delete the downloaded failed const = fs.unlinkSync del (checkUrl); IF (del) { the throw 'Delete the file ts abnormal ' } else { The console.log (EL + 'successfully deleted') }
          // Re-downloading downloads (EL) } } the else { downloads (EL) } }); }
// This synthesis function being given editing operation, temporarily copied to cmd running function Composite () {
  // "/ b": represents the merger in binary; without it the default character turn by the merger, will go wrong exec ( "copy / b H: \ self-study \ download streaming video \ download \ * ts H:. \ self -study \ download video stream \ downloads \ blooded chase .ts ", (error, stdout, stderr) => { IF (error) { console.error (` error executed: $ {error `}); return; } the console.log (stdout`: `$ {} stdout); console.error (stderr `:` $ {} stderr); }); } function the getList (CB) { downloads function (URL) { const the baseUrl = 'https://youku.com-qq.net/20190502/181_7ffa42fa/1000k/hls/'; const downloadUrl __dirname + = '/ downloads /' + URL; Request (URL the baseUrl + , (error, Response, body) => { IF (Response) { failDownload.push (URL); the console.log (URL + 'download successful') } IF (error) { the console.log (URL + 'download failed' ) } }) .pipe (fs.createWriteStream (downloadUrl))
  // after downloading, the flow directly written by a local file } const REG = /(\w+\.ts)/mg; const = getListFile './download/getlist .txt '; function doGetAgain () { // Get profile segment ts request.GET ( 'https://youku.com-qq.net/20190502/181_7ffa42fa/1000k/hls/index.m3u8', (error, Response, body) => { IF (typeof body == 'String') { the let body.match Results = (REG); the console.log (Results); fs.writeFileSync (getListFile, results.join ( ',')) CB CB && (Results); the console.log ( 'obtained from the server') Results return; } }) } // check if there is a local car that file iF (fs.existsSync (getListFile)) { the let Data = fs.readFileSync (getListFile, 'UTF-. 8'); CB (Data.split(',')); } if(data) { console.log ( 'from a local'); the else { return doGetAgain (CB); } } return doGetAgain (CB); }
function tickerGet (Results) { const results.length len = -1; the let I = 0; const TICKER = the setInterval (() => { IF (I <= len) { downloads (Results [I]); I ++; } the else { the clearInterval (TICKER); reload (Results); } }, 3000) }
the getList (R & lt => { // Timing for downloading tickerGet (R & lt); // re-download failed file reload (r) // synthesis Composite (); });   

  

Guess you like

Origin www.cnblogs.com/bigman-bugman/p/11779560.html