-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube-test.js
164 lines (134 loc) · 3.86 KB
/
youtube-test.js
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
var http = require('http'),
express = require("express"),
helper = require('./custom_modules/helper.js'),
Speaker = require("speaker"),
lame = require("lame"),
_ = require("underscore"),
Stream = require('stream');
var console_ffmepg_cmd = "~/Desktop/ffmpeg -i '<%= link %>' -vn -ar 44100 -ac 2 -ab <%= encoding_rate %> -f ogg http://localhost:8001";
helper.getVideosByYoutubeId('http://www.youtube.com/watch?v=XjTSpgAm8QM', function(resp){
if(typeof(resp.error) != "undefined" && resp.error != null){
console.log(resp.error.code);
return;
}
for(i in resp.links){
// Just play from the first link
var mp4link = resp.links[i].url;
break;
}
for(i in resp.link){
if(resp.link[i].type.format == 'mp4'){
// Got the mp4 link
var mp4link = resp.link[i].url;
break;
}
}
var ffmpeg_cmd = _.template(console_ffmepg_cmd, {link:mp4link, encoding_rate:"196608"});
var terminal = require('child_process').spawn('bash');
terminal.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
terminal.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
console.log(ffmpeg_cmd);
terminal.stdin.write(ffmpeg_cmd);
terminal.stdin.end();
});
// Start a simple server
var soundBuffer=[];
var decoder = new lame.Decoder();
var buff_size = 0;
var speaker;
var thisBuffer;
var soundRequest;
var sampleSeconds = 3;
var soundServer = http.createServer(function (req, res) {
soundRequest = req;
req.on('data',function(data){
soundBuffer.push(data);
//console.log(getSecondsFromBuffer(soundBuffer)+' seconds');
/*buff_size += data.length;
if(buff_size >= (1024*1024)){
// Read out from the buffer..
// TODO: This is not an efficient way, There is a better way to concatenate buffers
thisBuffer = soundBuffer;
var playback_seconds = (buff_size*8)/196608;
console.log(playback_seconds+" seconds of playback");
// Reset the buffer
soundBuffer=[];
buff_size=0;
for(i in thisBuffer){
decoder.write(thisBuffer[i]);
}
} else {
//console.log('soundBuffer : '+soundBuffer.length);
}*/
});
}).listen(8001);
soundServer.on('connection', function(){
playFromBuffer();
});
decoder.on('format', function(format){
speaker = new Speaker(format);
this.pipe(speaker);
});
playFromBuffer = function(){
var playDuration = getSecondsFromBuffer(soundBuffer)*1000;
if(playDuration == 0){
// Stop the sound at this point
if(typeof(speaker) != "undefined"){
}
// Requires buffering maybe?
console.log('Determining speed for a sample of '+sampleSeconds+' seconds..');
computeStreamSpeed(sampleSeconds, function(speed){
console.log('speed : '+parseInt(speed)+' Kbps');
if(speed > 192){
// No buffering required
playFromBuffer();
} else {
// Buffering required..
// increase sampleSeconds by some factor.
sampleSeconds = Math.ceil(sampleSeconds*(192/speed));
playFromBuffer();
}
});
return;
}
console.log('writing to soundBuffer : '+getSecondsFromBuffer(soundBuffer)+' Seconds.');
for(i in soundBuffer){
decoder.write(soundBuffer[i]);
}
console.log('write finished!');
soundBuffer=[];
setTimeout(function(){
playFromBuffer();
},playDuration);
}
computeStreamSpeed = function(sampleSize, success_callback, fail_callback){
//get the current buffer size
startSize = getBufferArraySize(soundBuffer);
// Buffer for sampleSize seconds
setTimeout(function(){
endSize = getBufferArraySize(soundBuffer);
dataSize = endSize-startSize;
if(dataSize < 0){
fail_callback();
return;
}
// Calculate the speed
var kbps = (dataSize*8)/(sampleSize*1024);
success_callback(kbps);
},sampleSize*1000);
}
getBufferArraySize = function(buffArray){
var size=0;
for(i in buffArray){
size+=buffArray[i].length;
}
return(size);
}
getSecondsFromBuffer = function(buffArray){
var size = getBufferArraySize(buffArray);
return((size*8)/196608);
}