Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues Streaming MP4s with Webtorrent

I'm running a Node Server that I want to stream videos from magnet links that uses WebTorrent(https://webtorrent.io/docs). When I run this, it appears as if the file is not being correctly referenced even though I have set a variable as the .mp4 file.

Just to be clear, I added in a given torrentID(magnet link) in this example to eliminate any problems I may have with express and the URLs. This magnet link leads to a download of a music video in MP4 format.

The video player is showing up, but no video is being played. I'm assuming this means that I am not trying to access the correct file. If you need to know more about WebTorrent to help me, you can read about it at https://webtorrent.io/docs

var fs = require("fs"),
    http = require("http"),
    url = require("url"),
    path = require("path"),
    request = require('request'),
    host = '127.0.0.1',
    port = 3000,
    express = require("express"),
    app = express(),
    server = http.createServer(app),
    WebTorrent = require('webtorrent'),
    client = new WebTorrent();

app.get('/streamvid/:magLink', function(req, res){
    //var torrentID = req.params.magLink;
    var torrentID = 'magnet:?xt=urn:btih:84123E8B4E850A796403736E0CF02E409F0EF00B';


    client.add(torrentID, function (torrent) {  
        var file = torrent.files[0]
        file.name = 'movie.mp4';
        if (req.url != "/movie.mp4") {
            res.writeHead(200, { "Content-Type": "text/html" });
            res.end('<video width="1024" height="768" controls> <source src="movie.mp4" type="video/mp4"> <source src="movie.ogg" type="video/ogg"> Your browser does not support the video tag. </video>');
        } else {
            var range = req.headers.range;
            var positions = range.replace(/bytes=/, "").split("-");
            var start = parseInt(positions[0], 10);

        fs.stat(file, function(err, stats) {
            var total = stats.size;
            var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
            var chunksize = (end - start) + 1;

        res.writeHead(206, {
            "Content-Range": "bytes " + start + "-" + end + "/" + total,
            "Accept-Ranges": "bytes",
            "Content-Length": chunksize,
            "Content-Type": "video/mp4"
        });

        var stream = fs.createReadStream(file, { start: start, end: end })
            .on("open", function() {
                stream.pipe(res);
            }).on("error", function(err) {
                res.end(err);
            });
        });
     }
     })
});

var server = http.createServer(app);

var server = app.listen(port, host);

server.on('error', function(err) {
    console.log('error:' + err);
});

server.on('listening', function(){
    console.log('Server is Up and Running');
});
like image 883
Jasch1 Avatar asked Apr 03 '16 00:04

Jasch1


1 Answers

You need to either pipe the file data by reading it.

var readFile = fs.createReadStream("path/to/movie.mp4");
readFile.pipe(res);

Or have the file in a public route. app.use(express.static('public')) and put movie.mp4 in the public/ folder. Then in your src, do a full url link. http://localhost:3000/movie.mp4.

like image 156
jemiloii Avatar answered Nov 16 '22 01:11

jemiloii