Skip to content

Commit

Permalink
feat: display download speed
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Apr 11, 2020
1 parent 67cce87 commit fc4964f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
21 changes: 17 additions & 4 deletions bin/ytdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
};

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit fc4964f

Please sign in to comment.