Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
added filename to data returned
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Sep 16, 2011
1 parent 1377915 commit 49909ca
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.mp4
*.webm
node_modules
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.mp4
*.webm
node_modules
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Usage
// will be called when the download starts
dl.on('download', function(data) {
console.log('Download started');
console.log('Video size: ' + data.size);
console.log('filename: ' + data.filename);
console.log('size: ' + data.size);
});

// will be called during download progress of a video
Expand All @@ -29,6 +30,8 @@ Usage
// called when youtube-dl finishes
dl.on('end', function(data) {
console.log('\nDownload finished!');
console.log('Filename: ' + data.filename);
console.log('Size: ' + data.size);
console.log('Time Taken: ' + data.timeTaken);
console.log('Time Taken in ms: ' + data.timeTakenms);
console.log('Average Speed: ' + data.averageSpeed);
Expand All @@ -42,6 +45,8 @@ This example can be found in the *example* folder, and will produce an output th
Video size: 918.31k
00:00 100.0% at 206.12k/s
Download finished!
Filename: 90AiXO1pAiA.mp4
Size: 918.31k
Time Taken: 7 seconds, 27 ms
Time Taken in ms: 7027
Average Speed: 333.74KB/s
Expand All @@ -60,6 +65,7 @@ This example can be found in the *example* folder, and will produce an output th
console.log('url: ' + info.url);
console.log('thumbnail: ' + info.thumbnail);
console.log('description: ' + info.description);
console.log('filename: ' + info.filename);
}
// optional arguments passed to youtube-dl
Expand All @@ -72,6 +78,8 @@ Running that will produce something like
url: http://v2.lscache2.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0hPSFFQVF9FSkNOOV9JSlhJ&fexp=904410%2C907048%2C910100&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&signature=4093330AC1A5B0CAF8709A0416A4B593A75BB892.21F2F12C418003492D9877E1570DC7AEE6DBEEBA&expire=1303156800&key=yt1&ip=0.0.0.0&factor=1.25&id=58ab2368ea835e08
thumbnail: http://i4.ytimg.com/vi/WKsjaOqDXgg/default.jpg
description: An old Red Dwarf eposide where Ace Rimmer saves the Princess Bonjella.
filename: WKsjaOqDXgg.webm


For more usage info on youtube-dl and the arguments you can pass to it, do `youtube-dl -h` or go to the [youtube-dl documentation][].

Expand Down
5 changes: 4 additions & 1 deletion example/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ dl = youtubedl.download('http://www.youtube.com/watch?v=90AiXO1pAiA', './',
// will be called when the download starts
dl.on('download', function(data) {
console.log('Download started');
console.log('Video size: ' + data.size);
console.log('filename: ' + data.filename);
console.log('size: ' + data.size);
});

// will be called during download progress of a video
Expand All @@ -25,6 +26,8 @@ dl.on('error', function(err) {
// called when youtube-dl finishes
dl.on('end', function(data) {
console.log('\nDownload finished!');
console.log('Filename: ' + data.filename);
console.log('Size: ' + data.size);
console.log('Time Taken: ' + data.timeTaken);
console.log('Time Taken in ms: ' + data.timeTakenms);
console.log('Average Speed: ' + data.averageSpeed);
Expand Down
1 change: 1 addition & 0 deletions example/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ youtube.info('http://www.youtube.com/watch?v=WKsjaOqDXgg',
console.log('url: ' + info.url);
console.log('thumbnail: ' + info.thumbnail);
console.log('description: ' + info.description);
console.log('filename: ' + info.filename);
}

// optional arguments passed to youtube-dl
Expand Down
Binary file added example/lol
Binary file not shown.
28 changes: 14 additions & 14 deletions lib/youtube-dl.js

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "youtube-dl",
"description": "youtube-dl driver for node",
"keywords": ["youtube", "download"],
"version": "1.1.2",
"version": "1.2.0",
"homepage": "https://github.com/fent/node-youtube-dl",
"repository": {
"type": "git",
Expand All @@ -18,6 +18,9 @@
"install": "node ./scripts/download.js",
"update": "node ./scripts/download.js"
},
"devDependencies": {
"vows": ">=0.5.11"
},
"engines": {
"node": "*"
},
Expand Down
43 changes: 23 additions & 20 deletions src/youtube-dl.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ round = (num, n) ->

# converts from bytes, kb, mb, and gb to bytes
toBytes = (s) ->
speed = parseFloat(s.substring 0, s.length - 3)
switch s.substr(-3, 1).toLowerCase()
speed = parseFloat(s.substring 0, s.length - 1)
switch s.substr(-1, 1).toLowerCase()
when 'b'
speed
when 'k'
Expand Down Expand Up @@ -103,8 +103,9 @@ getHumanTime = (ms) ->
"#{str}#{ms} ms"


# main download function
regex = /(\d+\.\d)% of (\d+\.\d+\w) at\s+([^\s]+) ETA ((\d|-)+:(\d|-)+)/

# main download function
module.exports.download = (url, dest = './', args = []) ->
# setup settings
args = parseOpts args
Expand All @@ -115,7 +116,7 @@ module.exports.download = (url, dest = './', args = []) ->
speed = []
start = Date.now()

video = size = state = null
filename = size = state = null
emitter = new EventEmitter()

youtubedl.stdout.on 'data', (data) ->
Expand All @@ -124,13 +125,15 @@ module.exports.download = (url, dest = './', args = []) ->
# check if video is uploading so script can start
# calling the download progress function
if state is 'download' and result = regex.exec data

# if this is the first progress display, grab file size
if not size
emitter.emit state,
video: video
filename: filename
size: size = result[2]

if result[3] isnt '---b/s'
speed.push toBytes result[3]
speed.push toBytes result[3].substring(0, result[3].length - 2)
emitter.emit 'progress',
percent: result[1]
speed: result[3]
Expand All @@ -139,16 +142,12 @@ module.exports.download = (url, dest = './', args = []) ->
# about to start downloading video
else if (pos = data.indexOf '[download] ') is 0
state = 'download'
filename = data.substring(24, data.length - 1)

# check if this is any other state
else if (pos = data.indexOf ']') isnt -1
state = data.substring pos + 2, data.length - 1

# get video name
if (pos = state.indexOf ':') isnt -1
video = state.substring 0, pos
state = state.substring pos + 2
emitter.emit state, video
emitter.emit state

youtubedl.stderr.on 'data', (data) ->
data = data.toString()
Expand All @@ -165,10 +164,12 @@ module.exports.download = (url, dest = './', args = []) ->
timeTaken = Date.now() - start

emitter.emit 'end',
timeTakenms: timeTaken
timeTaken: getHumanTime timeTaken
averageSpeedBytes: round averageSpeed, 2
averageSpeed: getHumanSize(averageSpeed) + '/s'
filename : filename
size : size
timeTakenms : timeTaken
timeTaken : getHumanTime timeTaken
averageSpeedBytes : round averageSpeed, 2
averageSpeed : getHumanSize(averageSpeed) + '/s'

emitter

Expand All @@ -182,6 +183,7 @@ module.exports.info = (url, callback, args = []) ->
'--get-title'
'--get-thumbnail'
'--get-description'
'--get-filename'
].concat args
args.push url

Expand All @@ -193,10 +195,11 @@ module.exports.info = (url, callback, args = []) ->
youtubedl.stdout.on 'data', (data) ->
data = data.toString().split "\n"
info =
title: data[0]
url: data[1]
thumbnail: data[2]
description: data[3]
title : data[0]
url : data[1]
thumbnail : data[2]
description : data[3]
filename : data[4]

youtubedl.stderr.on 'data', (data) ->
data = data.toString()
Expand Down

0 comments on commit 49909ca

Please sign in to comment.