var browser require airplay createBrowser var charm require charm var

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
var browser = require('airplay').createBrowser();
var charm = require('charm');
var keypress = require('keypress');
var timespan = require('timespan');
keypress(process.stdin);
browser.on('deviceOnline', function(device) {
browser.stop();
playOnDevice(device);
});
browser.start();
function formatTime(seconds) {
function pad(n, z) {
z = z || '0';
n = n + '';
return n.length >= 2 ? n : new Array(2 - n.length + 1).join(z) + n;
}
var t = timespan.fromSeconds(seconds);
if (!t) return "";
var timeString = pad(t.minutes) + ':' + pad(t.seconds);
if (t.hours) {
timeString = t.hours + ':' + timeString;
}
return timeString;
}
function ProgressBar(device) {
this.device = device;
this.total = 100;
this.position = 0;
this.charm = charm();
this.charm.pipe(process.stdout);
process.stdin.on('keypress', function(ch, key) {
this.charm.delete('char', 1);
if (key && key.ctrl && key.name == 'c') {
console.log('\n');
process.exit(0);
}
var digit = parseInt(ch);
if (ch && (ch.length == 1) && digit > 0 && digit < 10) {
this.device.scrub(this.total / 10 * digit);
} else if (key.name == 'space') {
console.log(this.device);
if (this.device.rateValue !== 0) {
this.device.rate(0);
this.device.rateValue = 0;
} else {
this.device.rate(1);
this.device.rateValue = 1;
}
} else if (key.name == 'right') {
this.device.rateValue = Math.min(10, this.device.rateValue + 1);
this.device.rate(this.device.rateValue);
} else if (key.name == 'left') {
// todo: reverse
this.device.rateValue = Math.max(-10, this.device.rateValue - 1);
this.device.rate(this.device.rateValue);
}
this.draw();
}.bind(this));
process.stdin.setRawMode(true);
process.stdin.resume();
this.charm.write('\n\n\n');
}
ProgressBar.prototype.draw = function() {
this.charm.background('black');
this.charm.left(999);
this.charm.erase('line');
this.charm.write('Contols: 1-9 seek, t go to time, space pause');
this.charm.left(999);
this.charm.down(1);
this.charm.erase('line');
var current = formatTime(this.position);
this.charm.background('blue');
function getSymbol(pos, self) {
if (pos < Math.round(self.position / self.total * 100)) {
self.charm.background('blue');
self.charm.foreground('white');
} else {
self.charm.background('white');
self.charm.foreground('black');
}
if (!current || current.length <= 0) {
return " ";
}
var before = Math.ceil(current.length / 2);
var after = Math.floor(current.length / 2);
if ((pos > (50 - before)) && (pos < (50 + after))) {
var index = pos - (50 - before);
return current[pos - (50 - before)];
}
return " ";
}
for (var i = 0; i < 100; ++i)
this.charm.write(getSymbol(i, this));
this.charm.background('black');
this.charm.right(1);
this.charm.background('black');
this.charm.foreground('white');
if (this.total) {
this.charm.write(formatTime(this.total));
}
this.charm.down(1);
this.charm.left(999);
this.charm.erase('line');
this.charm.write('State: ');
this.charm.write('Playing');
this.charm.up(1);
this.charm.up(1);
this.charm.left(999);
};
function playOnDevice(device) {
var content = 'https://r15---sn-nwj7knlz.googlevideo.com/videoplayback?dur=581.520&sver=3&key=yt5&ms=au&cwbhb=yes&initcwndbps=1637500&mv=m&sparams=cwbhb%2Cdur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&source=youtube&itag=22&upn=ywYBgMgOEx0&mm=31&signature=13A203BEAE899E7FF6A154B5F13A3C2ED7D86832.9660E1B0427375F5B088336D620340B7922C8F30&mt=1419669460&expire=1419691116&mime=video%2Fmp4&id=o-AAeH9gKlDHd03haEYJYmcDUI230ojFrdYsXwT9J_7TCk&fexp=900718%2C904732%2C904844%2C914951%2C924621%2C927622%2C932404%2C936117%2C941004%2C943607%2C943917%2C947209%2C947218%2C948124%2C952302%2C952605%2C952808%2C952901%2C955301%2C957103%2C957105%2C957201&ipbits=0&ratebypass=yes&requiressl=yes&ip=50.152.158.217';
//var content = 'http://cs518604v4.vk.me/u148196065/videos/2aede8152d.720.mp4?extra=eMUsc6gJD6MpId6zDLN8QCzL2uxck_scnoD-9-aQYMf58nqgaO5vdrBtN4pAZRdBOYuOegNKyBxqIxHmtEglzzPdnGFXEK1u0Q';
var startPosition = 0;
console.log(device);
device.play(content, startPosition, function(res) {
console.log(content);
if (res) {
} else {
console.error('failed to play file on device');
}
var bar = new ProgressBar(device);
setInterval(function() {
device.status(function(res) {
/*
{ duration: 8453.597,
position: 61.601224606,
rate: 1,
playbackBufferEmpty: true,
playbackBufferFull: false,
playbackLikelyToKeepUp: true,
readyToPlay: true,
loadedTimeRanges: [ { duration: 1242.302263006, start: 0.041736994 } ],
seekableTimeRanges: [ { duration: 8453.597, start: 0 } ] }
*/
device.lastStatus = res;
device.rateValue = res.rate;
bar.total = res.duration;
bar.position = res.position;
bar.draw();
});
}, 300);
});
}