Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some extra features and fixed somes bugs #386

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

33 changes: 15 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
# peerflix
# stream

Streaming torrent client for Node.js

```
npm install -g peerflix
npm install -g @meteor314/stream
```

[![build status](http://img.shields.io/travis/mafintosh/peerflix.svg?style=flat)](http://travis-ci.org/mafintosh/peerflix)

## Usage

Peerflix can be used with a magnet link or a torrent file.
stream-cli can be used with a magnet link or a torrent file.
To stream a video with its magnet link use the following command.

```
peerflix "magnet:?xt=urn:btih:ef330b39f4801d25b4245212e75a38634bfc856e" --vlc
stream "magnet:?xt=urn:btih:ef330b39f4801d25b4245212e75a38634bfc856e" --vlc
```

Remember to put `"` around your magnet link since they usually contain `&`.
`peerflix` will print a terminal interface. The first line contains an address to a http server. The `--vlc` flag ensures vlc is opened when the torrent is ready to stream.
`stream` will print a terminal interface. The first line contains an address to a http server. The `--vlc` flag ensures vlc is opened when the torrent is ready to stream.

![peerflix](https://raw.github.com/mafintosh/peerflix/master/screenshot.png)
![stream](https://raw.github.com/meteor314/stream/master/screenshot.png)

To stream music with a torrent file use the following command.

```
peerflix "http://some-torrent/music.torrent" -a --vlc
stream "http://some-torrent/music.torrent" -a --vlc
```

The `-a` flag ensures that all files in the music repository are played with vlc.
Otherwise if the torrent contains multiple files, `peerflix` will choose the biggest one.
To get a full list of available options run peerflix with the help flag.
Otherwise if the torrent contains multiple files, `stream` will choose the biggest one.
To get a full list of available options run stream with the help flag.

```
peerflix --help
stream --help
```

Examples of usage of could be

```
peerflix magnet-link --list # Select from a list of files to download
peerflix magnet-link --vlc -- --fullscreen # will pass --fullscreen to vlc
peerflix magnet-link --mplayer --subtitles subtitle-file.srt # play in mplayer with subtitles
peerflix magnet-link --connection 200 # set max connection to 200
stream magnet-link --list # Select from a list of files to download
stream magnet-link --vlc -- --fullscreen # will pass --fullscreen to vlc
stream magnet-link --mplayer --subtitles subtitle-file.srt # play in mplayer with subtitles
stream magnet-link --connection 200 # set max connection to 200
```


Expand All @@ -60,7 +57,7 @@ sudo iptables -P INPUT ACCEPT

## Chromecast

If you wanna use peerflix on your chromecast checkout [peercast](https://github.com/mafintosh/peercast)
If you wanna use stream on your chromecast checkout [peercast](https://github.com/mafintosh/peercast)
or [castnow](https://github.com/xat/castnow)

## License
Expand Down
59 changes: 39 additions & 20 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ var numeral = require('numeral')
var os = require('os')
var address = require('network-address')
var proc = require('child_process')
var peerflix = require('./')
var stream = require('./')
var keypress = require('keypress')
var openUrl = require('open')
var inquirer = require('inquirer')
var parsetorrent = require('parse-torrent')
var bufferFrom = require('buffer-from')
var fs = require('fs')

var path = require('path')

process.title = 'peerflix'
process.title = 'stream'

var argv = rc('peerflix', {}, optimist
var argv = rc('stream', {}, optimist
.usage('Usage: $0 magnet-link-or-torrent [options]')
.alias('c', 'connections').describe('c', 'max connected peers').default('c', os.cpus().length > 1 ? 100 : 30)
.alias('p', 'port').describe('p', 'change the http port').default('p', 8888)
.alias('i', 'index').describe('i', 'changed streamed file (index)')
.alias('i', 'index').describe('i', 'changed streaming file (index)')
.alias('l', 'list').describe('l', 'list available files with corresponding index').boolean('l')
.alias('t', 'subtitles').describe('t', 'load subtitles file')
.alias('q', 'quiet').describe('q', 'be quiet').boolean('v')
Expand All @@ -38,13 +39,14 @@ var argv = rc('peerflix', {}, optimist
.alias('j', 'jack').describe('j', 'autoplay in omx** using the audio jack').boolean('j')
.alias('f', 'path').describe('f', 'change buffer file path')
.alias('b', 'blocklist').describe('b', 'use the specified blocklist')
.alias('n', 'no-quit').describe('n', 'do not quit peerflix on vlc exit').boolean('n')
.alias('n', 'no-quit').describe('n', 'do not quit stream on vlc exit').boolean('n')
.alias('a', 'all').describe('a', 'select all files in the torrent').boolean('a')
.alias('r', 'remove').describe('r', 'remove files on exit').boolean('r')
.alias('h', 'hostname').describe('h', 'host name or IP to bind the server to')
.alias('e', 'peer').describe('e', 'add peer by ip:port')
.alias('x', 'peer-port').describe('x', 'set peer listening port')
.alias('d', 'not-on-top').describe('d', 'do not float video on top').boolean('d')
.describe('exit', 'Exit stream on download').boolean('n')
.describe('on-downloaded', 'script to call when file is 100% downloaded')
.describe('on-listening', 'script to call when server goes live')
.describe('version', 'prints current version').boolean('boolean')
Expand All @@ -62,7 +64,7 @@ if (!filename) {
optimist.showHelp()
console.error('Options passed after -- will be passed to your player')
console.error('')
console.error(' "peerflix magnet-link --vlc -- --fullscreen" will pass --fullscreen to vlc')
console.error(' "stream magnet-link --vlc -- --fullscreen" will pass --fullscreen to vlc')
console.error('')
console.error('* Autoplay can take several seconds to start since it needs to wait for the first piece')
console.error('** OMX player is the default Raspbian video player\n')
Expand Down Expand Up @@ -128,7 +130,7 @@ var watchVerifying = function (engine) {
var ontorrent = function (torrent) {
if (argv['peer-port']) argv.peerPort = Number(argv['peer-port'])

var engine = peerflix(torrent, argv)
var engine = stream(torrent, argv)
var hotswaps = 0
var verified = 0
var invalid = 0
Expand All @@ -153,6 +155,15 @@ var ontorrent = function (torrent) {

var onready = function () {
if (interactive) {
var getDownloadedSize = function (file) {
var p = path.join(engine.path, file.path)
if (fs.existsSync(p)) {
var size = fs.statSync(p).size
return Math.round(size / file.length) * 100 // percentage
}
return 0
}

var filenamesInOriginalOrder = engine.files.map(file => file.path)
inquirer.prompt([{
type: 'list',
Expand All @@ -162,7 +173,7 @@ var ontorrent = function (torrent) {
.sort((file1, file2) => file1.path.localeCompare(file2.path))
.map(function (file, i) {
return {
name: file.name + ' : ' + bytes(file.length),
name: file.name + ' : ' + bytes(file.length) + ' (' + getDownloadedSize(file) + '%)',
value: filenamesInOriginalOrder.indexOf(file.path)
}
})
Expand Down Expand Up @@ -212,6 +223,12 @@ var ontorrent = function (torrent) {
})
}

if (argv['exit']) {
engine.on('uninterested', function () {
process.exit(0)
})
}

engine.server.on('listening', function () {
var host = argv.hostname || address()
var href = 'http://' + host + ':' + engine.server.address().port + '/'
Expand All @@ -222,7 +239,6 @@ var ontorrent = function (torrent) {
var paused = false
var timePaused = 0
var pausedAt = null

VLC_ARGS += ' --meta-title="' + filename.replace(/"/g, '\\"') + '"'

if (argv.all) {
Expand Down Expand Up @@ -343,9 +359,9 @@ var ontorrent = function (torrent) {

process.stdout.write(bufferFrom('G1tIG1sySg==', 'base64')) // clear for drawing

var interactive = !player && process.stdin.isTTY && !!process.stdin.setRawMode
var interactive = process.stdin.isTTY && !!process.stdin.setRawMode

if (interactive) {
if (!interactive) {
keypress(process.stdin)
process.stdin.on('keypress', function (ch, key) {
if (!key) return
Expand Down Expand Up @@ -408,18 +424,21 @@ var ontorrent = function (torrent) {
}
clivas.line('')
clivas.line('{yellow:info} {green:streaming} {bold:' + filename + ' (' + bytes(filelength) + ')} {green:-} {bold:' + bytes(swarm.downloadSpeed()) + '/s} {green:from} {bold:' + unchoked.length + '/' + wires.length + '} {green:peers} ')
clivas.line('{yellow:info} {green:path} {cyan:' + engine.path + '}')
clivas.line('{yellow:info} {green:downloaded} {bold:' + bytes(swarm.downloaded) + '} (' + downloadedPercentage + '%) {green:and uploaded }{bold:' + bytes(swarm.uploaded) + '} {green:in }{bold:' + runtime + 's} {green:with} {bold:' + hotswaps + '} {green:hotswaps} ')
// calculate estimated time from remaining bytes for the whole torrent and current download speed
var estimatedTime = (swarm.downloaded > 0) ? Math.floor(((engine.torrent.length) - swarm.downloaded) / swarm.downloadSpeed()) : 0;
var estimatedTimeString = ''
var estimatedHour = Math.floor(estimatedTime /(60*60))
// calculate estimated minutes romainig
var estimatedMinute = Math.floor((estimatedTime - estimatedHour*60*60)/60)
if (estimatedTime > 0) {
estimatedTimeString = '{yellow:info} {green: estimated time remaining for the complete download of this torrent} {bold:' + estimatedHour + 'h ' + estimatedMinute + 'm ' + Math.floor(estimatedTime % 60) + 's}'
clivas.line(estimatedTimeString)
}
clivas.line('{yellow:info} {green:verified} {bold:' + verified + '} {green:pieces and received} {bold:' + invalid + '} {green:invalid pieces}')
clivas.line('{yellow:info} {green:peer queue size is} {bold:' + swarm.queued + '}')
clivas.line('{80:}')

if (interactive) {
var openLoc = ' or CTRL+L to open download location}'
if (paused) clivas.line('{yellow:PAUSED} {green:Press SPACE to continue download' + openLoc)
else clivas.line('{50+green:Press SPACE to pause download' + openLoc)
}

clivas.line('{yellow:info} {green:path} {cyan:' + engine.path + '}')
clivas.line('')
linesremaining -= 9

Expand Down Expand Up @@ -471,7 +490,7 @@ var ontorrent = function (torrent) {
// we're doing some heavy lifting so it can take some time to exit... let's
// better output a status message so the user knows we're working on it :)
clivas.line('')
clivas.line('{yellow:info} {green:peerflix is exiting...}')
clivas.line('{yellow:info} {green:stream is exiting...}')
}

watchVerifying(engine)
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var torrentStream = require('torrent-stream')
var torrentstreaming = require('torrent-stream')
var http = require('http')
var fs = require('fs')
var rangeParser = require('range-parser')
Expand Down Expand Up @@ -157,7 +157,7 @@ var createServer = function (e, opts) {
if (!range) {
response.setHeader('Content-Length', file.length)
if (request.method === 'HEAD') return response.end()
pump(file.createReadStream(), response)
pump(file.torrentstreaming(), response)
return
}

Expand All @@ -181,7 +181,7 @@ module.exports = function (torrent, opts) {
// Parse blocklist
if (opts.blocklist) opts.blocklist = parseBlocklist(opts.blocklist)

var engine = torrentStream(torrent, xtend(opts, {port: opts.peerPort}))
var engine = torrentstreaming(torrent, xtend(opts, {port: opts.peerPort}))

// Just want torrent-stream to list files.
if (opts.list) return engine
Expand All @@ -197,7 +197,7 @@ module.exports = function (torrent, opts) {

engine.server = createServer(engine, opts)

// Listen when torrent-stream is ready, by default a random port.
// Listen when torrent-streaù is ready, by default a random port.
engine.on('ready', function () {
engine.server.listen(opts.port || 0, opts.hostname)
})
Expand Down
Loading