Monty Monro
Monty Monro

Reputation: 613

Express / torrent-stream: writing a file from stream, sending socket to client with url but client cannot find file

I'm working on a personal project which basically takes a magnet link, starts downloading the file and then renders the video in the torrent in the browser. I'm using an npm module called torrent-stream to do most of this. Once I create the readable stream and begin writing the file I want to send a socket message to the client with the video url so that the client can render a html5 video element and begin streaming the video.

The problem I am having is that once the client renders the video element and tries to find the source mp4 I get a 404 error not found on the video file. Any advice on this would be highly appreciated mates. :)

Controller function:

uploadVideo: function (req, res) {
    var torrentStream = require('torrent-stream');
    var mkdirp = require('mkdirp');

    var rootPath = process.cwd();
    var magnetLink = req.param('magnet_link');
    var fs = require('fs');
    var engine = torrentStream(magnetLink);

    engine.on('ready', function() {
        engine.files.forEach(function(file) {
            var fileName = file.name;
            var filePath = file.path;
            console.log(fileName + ' - ' + filePath);
            var stream = file.createReadStream();

            mkdirp(rootPath + '/assets/videos/' + fileName, function (err) {
                if (err) {
                    console.log(err);
                } else {
                    var videoPath = rootPath + '/assets/videos/' + fileName + '/video.mp4';
                    var writer = fs.createWriteStream(videoPath);
                    var videoSent = false;

                    stream.on('data', function (data) {
                        writer.write(data);
                        if (!videoSent) {
                            fs.exists(videoPath, function(exists) {
                                if (exists) {
                                    sails.sockets.broadcast(req.param('room'), 'video_ready', {videoPath: '/videos/' + fileName + '/video.mp4'});
                                    videoSent = true;
                                }
                            });
                        }
                    });
                    // stream is readable stream to containing the file content
                }
            });
        });
    });

    res.json({status: 'downloading'});
}

Client javascript:

$(document).ready(function () {
io.socket.on('connect', function () {
    console.log('mrah');
    io.socket.get('/join', {roomName: 'cowboybebop'});

    io.socket.on('message', function (data) {
        console.log(data);
    });

    io.socket.on('video_ready', function (data) {
        var video = $('<video width="320" height="240" controls>\
          <source src="' + data.videoPath + '" type="video/mp4">\
        Your browser does not support the video tag.\
        </video>');
        $('body').append(video);
    });
});

$('form').submit(function (e) {
    e.preventDefault();
    var formData = $(this).serialize();
    $.ajax({
        url: '/upload-torrent',
        method: 'POST',
        data: formData
    }).success(function (data) {
        console.log(data);
    }).error(function (err) {
        console.log(err);
    });
});
});

Form:

<form action="/upload-torrent" method="POST">
<input name="magnet_link" type="text" value="magnet:?xt=urn:btih:565DB305A27FFB321FCC7B064AFD7BD73AEDDA2B&dn=bbb_sunflower_1080p_60fps_normal.mp4&tr=udp%3a%2f%2ftracker.openbittorrent.com%3a80%2fannounce&tr=udp%3a%2f%2ftracker.publicbt.com%3a80%2fannounce&ws=http%3a%2f%2fdistribution.bbb3d.renderfarming.net%2fvideo%2fmp4%2fbbb_sunflower_1080p_60fps_normal.mp4"/>
<input type="hidden" name="room" value="cowboybebop">
<input type="submit" value="Link Torrent">

Upvotes: 5

Views: 2959

Answers (1)

KiraLT
KiraLT

Reputation: 2607

You may be interested in Torrent Stream Server. It a server that downloads and streams video at the same time, so you can watch the video without fully downloading it. It's based on the same torrent-stream library which you are exploring and has functionally you are trying to implement, so it may be a good reference for you.

Also, I suggest taking a look at webtorrent. It's a nice torrent library that works in both: NodeJs & browser and has streaming support. It sounds great, but from my experience, it doesn't have very good support in the browser.

Upvotes: 1

Related Questions