J
J
Jedi2018-12-20 10:06:33
JavaScript
Jedi, 2018-12-20 10:06:33

How to let in a stream, a stream received from another service?

Good morning!
Help solve the problem, please. Using a third party API, I can download videos from Youtube service.
With the help of the following code, I get the video in a stream, in chunks.

let data = [];
    youtubeApi.downloadVideo('v=4P1-JwZF0Vo&t=4519s', chunk => data.push(chunk), () => {
        console.log('downloaded ', data.length);
        fs.writeFileSync('video.mp4', Buffer.concat(data));
    });

I want that when requesting the same URL, not download the video, but create it later (streaming).
Youtube -> My Nodejs App -> Client
How to implement it? Please help me to solve the problem, I will be very grateful!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2018-12-20
@PHPjedi

without knowing what kind of lib provides youtubeApi, it is problematic to do it adequately, but you can do it universally:

const {PassThrough} = require('stream');

const youtubeStream = new PassThrough();
youtubeApi.downloadVideo(
  'v=4P1-JwZF0Vo&t=4519s',
  chunk => youtubeStream.write(chunk),
  () => youtubeStream.end()
);
youtubeStream.pipe(res); // где res - выходной поток к клиенту

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question