Skip to content

Commit

Permalink
fix: showing downloaded size for playlist formats
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Sep 27, 2018
1 parent f71ba3b commit fc4ea20
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
23 changes: 14 additions & 9 deletions bin/ytdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ if (opts.infoJson) {
} else {
const readStream = ytdl(url, ytdlOptions);
const stdoutMutable = process.stdout && process.stdout.cursorTo && process.stdout.clearLine;
let liveBroadcast = false;
let isPlaylist = false;

readStream.on('info', (info, format) => {
if (!output) {
Expand Down Expand Up @@ -251,23 +251,26 @@ if (opts.infoJson) {
process.exit(1);
});

liveBroadcast = format.live;
isPlaylist = format.live || format.isHLS || format.isDashMPD;

// Print information about the video if not streaming to stdout.
printVideoInfo(info, liveBroadcast);
printVideoInfo(info, isPlaylist);

console.log(label('container: ') + format.container);
console.log(label('resolution: ') + format.resolution);
console.log(label('encoding: ') + format.encoding);
if (!liveBroadcast) { return; }
if (!isPlaylist) { return; }

const throttle = require('lodash.throttle');
let dataRead = 0;
const updateProgress = throttle(() => {
process.stdout.cursorTo(0);
process.stdout.clearLine(1);
process.stdout.write(label('size: ') + util.toHumanSize(dataRead) +
' (' + dataRead +' bytes)');
let line = label('size: ') + util.toHumanSize(dataRead);
if (dataRead >= 1024) {
line += ` (${dataRead} bytes)`;
}
process.stdout.write(line);
}, 500);

readStream.on('data', (data) => {
Expand All @@ -279,14 +282,16 @@ if (opts.infoJson) {

readStream.on('end', () => {
if (stdoutMutable) {
console.log(label('downloaded: ') + util.toHumanSize(dataRead));
updateProgress.flush();
console.log();
} else {
console.log('\n' + label('downloaded: ') + util.toHumanSize(dataRead));
}
console.log();
});
});

readStream.on('response', (res) => {
if (!output || liveBroadcast) { return; }
if (!output || isPlaylist) { return; }

// Print information about the format we're downloading.
const size = parseInt(res.headers['content-length'], 10);
Expand Down
25 changes: 13 additions & 12 deletions package-lock.json

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

0 comments on commit fc4ea20

Please sign in to comment.