Comments (10)
ytld.getInfo()
is NOT synchronous, it needs to travel to youtube servers to get video info.
ytdl.getInfo('https://www.youtube.com/watch?v=rHLbd3nvu88', { downloadURL: true }, function(err, songInfo) {
if (err) throw err;
ytdl.downloadFromInfo(songInfo, { filter: 'audioonly' }).pipe(res);
});
ytdl()
is synchronous because it returns a readable stream, but its data isn't immediately available.
from node-ytdl-core.
I see.. My knowledge on node is crippling me at the moment. This is what I'm ultimately trying:
var app = require('express')(),
ytdl = require('ytdl-core'),
ffmpeg = require('fluent-ffmpeg'),
archiver = require('archiver'),
async = require('async');
app.get('/', function(req, res) {
var archive = archiver.create('zip', {});
ytdl.getInfo('https://www.youtube.com/watch?v=rHLbd3nvu88', { downloadURL: true }, function(err, songInfo) {
archive.append(
ffmpeg(
ytdl('https://www.youtube.com/watch?v=rHLbd3nvu88', { filter: 'audioonly' })
).format('mp3').pipe(), {
name: Date.now() + '.mp3'
}
);
});
res.setHeader('Content-disposition', 'attachment; filename=u2fy_' + Date.now() + '.zip');
res.setHeader('Content-type', 'application/zip');
archive.finalize().pipe(res);
});
When I do this without getInfo()
, it works perfectly; the user recieves a zip file with the audio without it being created on the server. However, when I perform the same thing inside the callback function, I get an error by archiver
: append: queue closed. Passing songInfo
to ytdl()
doesn't seem to work for me either.
Any hint?
from node-ytdl-core.
That sounds like it might be an error with ytdl, but why do you need to call ytdl.getInfo()
if you're not using songInfo
?
You could also listen for the info
event on the stream it returns.
var video = ytdl('https://www.youtube.com/watch?v=rHLbd3nvu88', { filter: 'audioonly' });
video.on('info', function() {
});
from node-ytdl-core.
I couldn't get that work, because of said [Error: append: queue closed]
from archiver
. Switched to:
ffmpeg(
ytdl.downloadFromInfo(songInfo, { filter: 'audioonly' })
)
Same thing though.
About the info
event, would that work in my case? Because I need to pass a stream to ffmpeg()
. How would that look like if I was to use the info
event callback?
from node-ytdl-core.
do you need the info
?
from node-ytdl-core.
Oh I forgot, I used Date.now()
instead of the actual thing I'm supposed to use, which is the title of the video. Sorry about that confusion :P
from node-ytdl-core.
Oh, then something like
var video = ytdl('https://www.youtube.com/watch?v=rHLbd3nvu88', { filter: 'audioonly' });
video.on('info', function(info) {
archive.append(ffmpeg(video).format('mp3').pipe(), { name: info.title});
});
from node-ytdl-core.
No luck :( Seems like archive.append()
doesn't like being inside a callback.
from node-ytdl-core.
Ok, I've gone a different direction. My guess is, archive.finalize()
gets piped to the response way to early, the queue closes and no more appends can happen.
So now I'm returning the archive but only finalizing it when everything is in:
var archive = archiver.create('zip', {}),
songs = [
'https://www.youtube.com/watch?v=vK_3AS2gavY'
],
entries = 0;
var video = ytdl(songs[0], { filter: 'audioonly' });
video.on('info', function(info) {
archive.append(ffmpeg(video).format('mp3').pipe(), { name: info.title + '.mp3'});
});
archive.on('entry', function(data) {
entries++;
if (songs.length === entries) {
archive.finalize();
}
});
res.setHeader('Content-disposition', 'attachment; filename=u2fy_' + Date.now() + '.zip');
res.setHeader('Content-type', 'application/zip');
archive.pipe(res);
It works, however, I can't figure out a way to make a loop in order to download more than 1 video. If I was able to apply the listener to ytdl
instead of a specific var video = ytdl(songs[0], { filter: 'audioonly' });
, I wouldn't have this problem I guess. Or if ytdl accepted an array of URLs so the info
event can be triggered more than once.
By the way, the entry
event gets triggered once an entry has been processed.
from node-ytdl-core.
I don't understand why this wouldn't work for more than 1 video.
wouldn't you just loop for the list of video urls and append for each?
from node-ytdl-core.
Related Issues (20)
- SyntaxError: Invalid or unexpected token HOT 19
- Timeout error starting -- no code change, no change in payload length HOT 1
- Is this repository no longer being maintained? HOT 2
- Downloaded has no sounds @latest version HOT 3
- Question HOT 1
- Error in audio stream for xxxx: MinigetError: Status code: 403 HOT 1
- like and dislike null value even the video has a like and dislike value HOT 1
- Method getInfo(url) with most of the hasVideo or hasAudio false and download only audio or video. Not a proper video. HOT 1
- MP3 id3 tag issue?
- [YouTube] Itag 18 not loading anymore HOT 7
- FFMPEG Get FileStream rather than writing straight to a file HOT 2
- Handling stream error events
- Banned dependency HOT 1
- Proxy not working HOT 1
- MinigetError: Status code: 573 HOT 2
- `audioTrack` not in typescript type for `formats`, `lang` parameter ignored
- Problem with duration
- Download a private video youtube HOT 1
- status code 410
- 410 status error HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-ytdl-core.