-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (167 loc) · 6.01 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const subprocess = require('child_process').fork('subprocess.js');
var HLSServer = require('hls-server');
var ffmpeg = require('fluent-ffmpeg');
var sleep = require('sleep');
var http = require('http');
var fs = require('fs');
var server = http.createServer();
// http://127.0.0.1:8000/streams/stream.m3u8
const fileDirs = '/home/amirsorouri00/Desktop/opt/nodejs/node-ffmpeg/files/verytmp'; // Directory that input files are stored
const util = require('util');
const execFile = util.promisify(require('child_process').execFile);
async function command_runner() {
const { stdout } = await execFile('node', ['--version']);
console.log(stdout);
}
function subst(st){
var substr = st.substring(
st.lastIndexOf("/") + 1,
st.lastIndexOf("s") + 1
).replace('segment_', '').replace('.ts', '');
substr = Number(substr) + 1
substr = 'segment_0000' + String(substr) + '.ts'
var substr1 = st.substring(
st.lastIndexOf("/") + 1,
st.lastIndexOf("s") + 1
)
rhhh = st.replace(substr1, substr)//.concat(substr)
return rhhh;
}
var hls = new HLSServer(server, {
path: '/streams', // Base URI to output HLS streams
dir: fileDirs, // Directory that input files are stored
provider: {
exists: function (req, callback) { // check if a file exists (always called before the below methods)
console.log("Inside exists")
callback(null, true) // File exists and is ready to start streaming
// callback(new Error("Server Error!")) // 500 error
// callback(null, false) // 404 error
},
getManifestStream: function (req, callback) { // return the correct .m3u8 file
// "req" is the http request
// "callback" must be called with error-first arguments
console.log("Inside getManifestStream");
var exist = false;
fs.access(req.filePath, fs.F_OK, (err) => {
if (err) {
console.error("doesn't exist any m3u8");
// /* Live Version
// subprocess.send('server');
// sleep.sleep(1);
callback(null, fs.createReadStream(req.filePath))
}
// file exists
else {
// Test
// subprocess.send('server');
// sleep.sleep(6);
callback(null, fs.createReadStream(req.filePath))
}
});
// m3ToConvertor(null);
// or
// callback(new Error("Server error!"), null)
},
getSegmentStream: function (req, callback) { // return the correct .ts file
console.log("Inside getSegmentStream")
var rhhh = subst(req.filePath);
console.log(rhhh)
const subprocess1 = require('child_process').fork('subprocess.js', [rhhh]);
subprocess1.send('segment');
sleep.msleep(200);
callback(null, fs.createReadStream(req.filePath))
// setTimeout(mine, 1000);
}
}
})
function yourMiddleware(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
console.log("inside middleware");
next()
}
var httpAttach = require('http-attach')
httpAttach(server, yourMiddleware)
server.listen(8000, () => {
console.log("hls_server is ready on port 8000:\n");
})
function m3ToConvertor(path) {
// Below is FFMPEG converting MP4 to HLS with reasonable options.
command_runner();
// https://www.ffmpeg.org/ffmpeg-formats.html#hls-2
// callback = callback
// function after() {
// callback(null, true);
// }
// ffmpeg(path, { timeout: 432000 })
// // .videoFilters({
// // filter: 'drawtext',
// // options: {
// // text: 'VERY LONG TEXT VERY VERY VERY VERY LOL!!!',
// // fontsize: 36,
// // fontcolor: 'white',
// // x: '(main_w/2-text_w/2)',
// // y: '(text_h/2)+15',
// // shadowcolor: 'black',
// // shadowx: 2,
// // shadowy: 2
// // }
// // })
// .addOptions([
// '-profile:v baseline', // baseline profile (level 3.0) for H264 video codec
// '-level 3.0',
// '-start_number 0', // start the first .ts segment at index 0
// '-hls_time 1', // 10 second segment duration
// '-hls_list_size 0', // Maxmimum number of playlist entries (0 means all entries/infinite)
// '-hls_playlist_type event',
// '-preset veryfast',
// '-f hls' // HLS format
// ])
// .output(fileDirs.concat(['/stream.m3u8']))
// .on('end', function(stdout, stderr) {
// console.log('ffmpeg done. !', req.filePath);
// })
// .run()
}
function tsToConvertor(req) {
function mine() {
callback(null, fs.createReadStream(req.filePath))
}
// ffmpeg(req.filePath, { timeout: 432000 })
// // .videoFilters({
// // filter: 'drawtext',
// // options: {
// // text: 'VERY LONG TEXT VERY VERY VERY VERY LOL!!!',
// // fontsize: 36,
// // fontcolor: 'white',
// // x: '(main_w/2-text_w/2)',
// // y: '(text_h/2)+15',
// // shadowcolor: 'black',
// // shadowx: 2,
// // shadowy: 2
// // }
// // })
// .videoCodec('libx264')
// .audioCodec('aac')
// .audioChannels(6)
// .addOption('-hls_list_size', 0)
// // .addOptions([
// // '-profile:v baseline', // baseline profile (level 3.0) for H264 video codec
// // '-level 3.0',
// // '-start_number 0', // start the first .ts segment at index 0
// // '-hls_time 1', // 10 second segment duration
// // '-hls_list_size 0', // Maxmimum number of playlist entries (0 means all entries/infinite)
// // '-hls_playlist_type event',
// // '-preset veryfast',
// // '-f hls' // HLS format
// // ])
// .format('mpegts')
// .output(name)
// .on('end', function(stdout, stderr) {
// console.log('ffmpeg done. !', req.filePath);
// callback(null, fs.createReadStream(name))
// })
// .run()
// .pipe();
// setTimeout(mine, 1000);
mine();
}