#! /usr/bin/env runjs var bytes = require('component-bytes.js'); var IOUtil = Packages.me.hatter.tools.commons.io.IOUtil; var getM3u8AsList = (fn) => { var m3u8FileContents; if (/^https?:\/\/.*/i.test(fn)) { println('Download m3u8 file: ' + fn); m3u8FileContents = IOUtil.readToList($$.httpRequest().url(fn).get()); } else { if (!$$.file(fn).exists()) { println('M3U8 file not extists: ' + fn); return; } m3u8FileContents = $$.rFile(fn).list(); } return m3u8FileContents; }; // http://youku.online-downloader.com/ var main = () => { if ($ARGS.length < 1) { println('No args!'); return; } var m3u8FileContents = getM3u8AsList($ARGS[0]); var index = 0; var tsList = []; var totalMillis = 0; var totalLength = 0; var totalTsCount = 0; m3u8FileContents.forEach((x) => { if ((/^https?:\/\/.*/.test(x))) { totalTsCount++; } }); m3u8FileContents.forEach((x) => { if (!(/^https?:\/\/.*/.test(x))) { // SKIP return; } index++; var tsFileFn = repeat('0', 5 - $STR(index).length) + index + '.ts'; tsList.push(tsFileFn); println('File: ' + x + " --> " + tsFileFn + ' (' + index + '/' + totalTsCount + ')'); print('-- Downloading: '); var tsFile = new java.io.File(tsFileFn); if (tsFile.exists()) { println('EXISTS!'); } else { var _s = $$.date().millis(); var _len = 0; var fos = new java.io.FileOutputStream(tsFile); $$.httpRequest() .url(x) .addHeader('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36') .get(fos, (x) => { print('.'); _len += x; }); fos.close(); var _e = $$.date().millis(); totalMillis += _e - _s; totalLength += _len; println(', filesize: ' + bytes.showBytes(_len) + ', speed: ' + bytes.showBytes((_len * 1000) / (_e - _s)) + '/s'); } }); println('Join ts files...'); var cmdFile = $$.file('_temp_merge_ts'); var newFileName = new Date().getTime() + '.mp4' $$.rFile(cmdFile).write('ffmpeg -i "concat:' + tsList.join('|') + '" -c copy -bsf:a aac_adtstoasc ' + newFileName); $$.shell().inheritIO().commands('sh', '_temp_merge_ts').run(); cmdFile.delete(); println(); println('Finished, download video file is: ' + newFileName + ', filesize: ' + bytes.showBytes(totalLength) + ', speed: ' + bytes.showBytes((totalLength * 1000) / totalMillis) + '/s'); }; main();