I'm having a problem with the seed of a webtorrent, the problem is that the seed is only started after the end of the transmission, in this case I'm using the media server node and getting its stream through the FLV url. So why can't I or in this case I can do req.pipe(passThroughStream); But do I have to wait for the stream to finish? Has anyone managed to make a pipe for the webtorrent lib seed?
import NodeMediaServer from 'node-media-server';
// import WebTorrent from 'webtorrent';
import WebTorrent from 'webtorrent-hybrid';
import { fileURLToPath } from 'url';
import path from 'path';
import request from 'request';
import { PassThrough, Writable, Readable } from 'stream';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const mediaroot = path.resolve(__dirname, './media');
const config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 60,
ping_timeout: 30
},
http: {
port: 8000,
mediaroot,
allow_origin: '*'
}
};
const nms = new NodeMediaServer(config);
nms.run();
const client = new WebTorrent();
nms.on('postPublish', async (id: any, streamPath: any, args: any) => {
const passThroughStream = new PassThrough();
const req = request.get(`http://localhost:8000${streamPath}.flv`);
req.on('error', function(err) {
console.error('Erro ao baixar o vídeo:', err);
});
client.on('torrent', (torrent) => {
torrent.files.forEach((file) => {
file.createReadStream().pipe(passThroughStream);
});
});
const torrent = client.seed(passThroughStream);
torrent.on('ready', () => {
console.log(`Torrent criado: ${torrent.magnetURI}`);
});
torrent.on('error', (err) => {
console.error('Erro ao criar o torrent:', err);
});
req.pipe(passThroughStream);
});