From fc4964f4cf9f0f9032c9558d0581e929fb42dc41 Mon Sep 17 00:00:00 2001 From: fent <933490+fent@users.noreply.github.com> Date: Sat, 11 Apr 2020 14:18:56 -0400 Subject: [PATCH] feat: display download speed --- bin/ytdl.js | 21 +++++++++++++++++---- package-lock.json | 5 +++++ package.json | 1 + 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/bin/ytdl.js b/bin/ytdl.js index e2d0446..69eca8e 100755 --- a/bin/ytdl.js +++ b/bin/ytdl.js @@ -244,22 +244,35 @@ if (opts.infoJson) { // Create progress bar. const CliProgress = require('cli-progress'); + const StreamSpeed = require('streamspeed'); const bar = new CliProgress.SingleBar({ - format: '{bar} {percentage}%', + format: '{bar} {percentage}% {speed}', complete: '#', incomplete: '-', width: 50, total: size, - }, CliProgress.Presets.rect); + }, CliProgress.Presets.shades_grey); bar.start(size); + const ss = new StreamSpeed(); + ss.add(readStream); // Keep track of progress. + const getSpeed = () => ({ + speed: StreamSpeed.toHuman(ss.getSpeed(), { timeUnit: 's', precision: 3 }), + }); readStream.on('data', (data) => { - bar.increment(data.length); + bar.increment(data.length, getSpeed()); }); + // Update speed every second, in case download is rate limited, + // which is the case with `audioonly` formats. + let iid = setInterval(() => { + bar.update(null, getSpeed()); + }, 1000); + readStream.on('end', () => { - console.log(); + bar.stop(); + clearInterval(iid); }); }; diff --git a/package-lock.json b/package-lock.json index ea445a1..c60d8b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1339,6 +1339,11 @@ "resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz", "integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=" }, + "streamspeed": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/streamspeed/-/streamspeed-2.0.0.tgz", + "integrity": "sha512-RFsqRckd50tB2TOBLE3FGohvoxdOxKk7AuePNCKkoN/I3nXh44EQ4KTFHh0EEoXErcmzlgjw3Z0v9uY7MCEu1A==" + }, "string-width": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", diff --git a/package.json b/package.json index 90cc9b4..3df7c67 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "homedir": "^0.6.0", "lodash.throttle": "^4.1.1", "sanitize-filename": "^1.6.1", + "streamspeed": "^2.0.0", "ytdl-core": "^1.0.2" }, "devDependencies": {