Issues Streaming MP4s with Webtorrent
Asked Answered
E

1

10

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');
});
Everest answered 3/4, 2016 at 0:34 Comment(4)
What does the console say? The code is wrong on many points. app.get('/streamvid/:magLink... won't match for streamvid/movie.mp4 You should use the WebTorrent's torrent.createServer apiSpada
Few Issues with Your Response: 1. The createServer API doesn't stream the file, but instead makes it available for download. In my case I am trying to stream a video. 2. You don't need to go to streamvid/movie.mp4 because when you go to /streamvid/:magLink , it's response is the video tag with the MP4 file. I'm not just viewing the page that the file is being hosted on. 3. There are no console errorsEverest
1. It does, the http server will serve the file with GET & GET range support. 2. You do, because the <video> tag is loading it from '/streamvid/movie.mp4 (current dir).Spada
Do you have a link to the actual page and/or client-side code? Also, streaming MP4 is often problematic... did you encode the video with faststart?Lailalain
P
2

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.

Pirbhai answered 27/6, 2018 at 18:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.