butterproject / butter-desktop

All the free parts of Popcorn Time
http://butterproject.org/
GNU Affero General Public License v3.0
4.29k stars 1.08k forks source link

webtorrent #359

Closed vankasteelj closed 8 years ago

vankasteelj commented 8 years ago

We should use that, from initial tests it's more mature now than a year ago.

Just a proof of concept done in 2min:

var magnet = ''; // set magnet here

var buffer = 5 * 1024 * 1024; // 5MB

var WebTorrent = require('webtorrent');
var streamer = new WebTorrent();
streamer.add(magnet, {
    path: Settings.tmpLocation
}, function (torrent){
    console.log('torrent started');
    console.log(torrent.name);
    console.log(torrent.files);

    var file = torrent.files[0];
    for (var i = 0; i < torrent.files.length; i++) {
        if (file.length < torrent.files[i].length ) {
            file.deselect();
            file = torrent.files[i];
        }
    }
    file.select();

    var filePath = file.path;
    var fileName = file.name;
    var torrentPath = torrent.path;

    var playerStarted;
    var interval = setInterval(function () {
        console.log('progress: ' + Math.round((torrent.downloaded / file.length)*100) + '%');
        if (!playerStarted && torrent.downloaded > buffer) {
            handleVideoFile({
                name: fileName,
                path: path.join(torrentPath,filePath)
            });
            playerStarted = true;
        }
    }, 1000);

    torrent.on('done', function () {
        streamer.destroy();
        clearInterval(interval);
    })
});

Player starts quickly, I had to set a buffer of 5MB because I had no time to dig further into the events sent by webtorrent, but it still is about 200 or 300% faster than peerflix, not in terms of download speed, which are similar, but in term of metadata retrieving (metadata we need in order to get the player running).

I'd say grosso modo webtorrent can start "playing" a file directly (after a little download) where peerflix sometimes takes around 5 to 25seconds.

VitorVRS commented 8 years ago

Maybe it helps: https://github.com/VitorVRS/popcorn-desktop/tree/feature/webtorrent

xaiki commented 8 years ago

looks good ! you still require('peerflix') is that right ? if you want to submit a PR i think it can get in

VitorVRS commented 8 years ago

You can see in this issue: https://github.com/popcorn-official/popcorn-desktop/issues/146 There are somethings that are incomplete.

xaiki commented 8 years ago

awesome, please ping us back when you get closer !

VitorVRS commented 8 years ago

@xaiki can i PR my branch with webtorrent implementation to butter? Now i have a fork of PCT, there is a way i can "change" my fork from pct to butter?

xaiki commented 8 years ago

@vitorVRS you can cherry-pick your commits on a butter tree, if you want do the PR and i'll help you clean it up.

ryananderson00 commented 8 years ago

I am interested in replacing peerflix with webtorrent. In my testing it is a lot faster than peerflix, plus it can peer with browsers which is handy. I'll work on sending a PR soon.

ryananderson00 commented 8 years ago

I just noticed that there is progress towards adding support in this PR here: https://github.com/butterproject/butter-desktop/pull/436

VitorVRS commented 8 years ago

Yeah, its almost done, just need to remove peerflix from other sources and fix stream information...

VitorVRS commented 8 years ago

@ryananderson00 Do you have something already done? Maybe you can help me to finish.