-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
301 lines (267 loc) · 8.15 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* PARATII TRANSCODER - WORK IN PROGRESS - gitter @Ya7ya for help
*/
'use strict'
const path = require('path')
const fs = require('fs')
const os = require('os')
const { eachSeries, nextTick } = require('async')
const ipfsAPI = require('ipfs-api')
const ffmpeg = require('fluent-ffmpeg')
const config = {
FFMPEG_PATH: process.env.FFMPEG_PATH || '/usr/bin/ffmpeg',
FFPROBE_PATH: process.env.FFPROBE_PATH || '/usr/bin/ffprobe',
IPFS_API: '/ip4/127.0.0.1/tcp/5002'
}
const conversions = {
'1080': ['?x1080', '?x720', '?x480', '?x360', '?x240', '?x144'],
'720': ['?x720', '?x480', '?x360', '?x240', '?x144'],
'480': ['?x480', '?x360', '?x240', '?x144'],
'360': ['?x360', '?x240', '?x144'],
'240': ['?x240', '?x144'],
'144': ['?x144']
}
var ipfs = ipfsAPI(config.IPFS_API)
ffmpeg.setFfprobePath(config.FFPROBE_PATH)
// let testHash = '/ipfs/QmR6QvFUBhHQ288VmpHQboqzLmDrrC2fcTUyT4hSMCwFyj'
// let testHash = '/ipfs/QmaLDCUrJqjHgnv8trt8KcSuFavQaorZpMtSDogE6np8st'
let testHash = '/ipfs/QmeG4popSYeipnvuvP6u4UxuRfKWTzy6eEMyC54ArFRNiG'
// let testHash = '/ipfs/QmZgNs5jJJtv1yD83aCoQvRUYscfneSZeYQyLpjKrfyRFK'
function getResolutionPath (filePath, cb) {
ffmpeg.ffprobe(path.resolve(__dirname, filePath), (err, metadata) => {
if (err) return cb(err)
// console.log(metadata)
for (var stream of metadata.streams) {
if (stream.codec_type === 'video') {
return cb(null, {
width: stream.width,
height: stream.height,
display_aspect_ratio: stream.display_aspect_ratio
})
}
}
})
}
//
// getResolution('../../videos/The Night Manager S01E03.mp4', (err, meta) => {
// if (err) throw err
//
// console.log(meta)
// })
function getResolution (ipfsHash, cb) {
ipfs.files.get(ipfsHash, (err, filesStream) => {
if (err) return cb(err)
filesStream.on('data', (file) => {
console.log('got file Obj ', file.path, ' size: ', file.size)
if (file.content) {
ffmpeg(file.content)
.ffprobe(0, (err, metadata) => {
if (err) return cb(err)
// console.log(metadata)
for (var stream of metadata.streams) {
if (stream.codec_type === 'video') {
return cb(null, {
width: stream.width,
height: stream.height,
display_aspect_ratio: stream.display_aspect_ratio
})
}
}
})
}
})
filesStream.on('error', (err) => {
console.error('filesStream Err ', err)
})
filesStream.on('end', () => {
console.log('filesStream ended!')
})
})
}
// getResolution(testHash, (err, meta) => {
// if (err) throw err
//
// console.log('meta: ', meta)
// })
// var fileStream = fs.createWriteStream('outputfile.mp4')
function convertToHLS (filePath, cb) {
console.log('loading converter')
ffmpeg(filePath)
.inputOptions('-strict -2')
// .inputOptions('preset', 'superfast')
.addOption('-profile:v', 'baseline')
.addOption('-level', 3.0)
.addOption('-start_number', 0)
.videoCodec('libx264')
// set audio bitrate
.audioBitrate('128k')
// set audio codec
.audioCodec('aac')
// set number of audio channels
.audioChannels(2)
// set hls segments time
.addOption('-hls_time', 5)
// include all the segments in the list
.addOption('-hls_list_size', 0)
.addOption('-f', 'hls')
// .inputOptions('-strict -2 -profile:v baseline -level 3.0 -start_number 0 -hls_time 5 -hls_list_size 0 -f hls')
// .output(fileStream)
.on('end', () => {
console.log('finished processing file.')
return cb()
})
.on('error', (err) => {
console.log('an error happened: ', err)
return cb(err)
})
.save('/tmp/master.m3u8')
// .outputOptions('-strict -2 -profile:v baseline -level 3.0 -start_number 0 -hls_time 5 -hls_list_size 0 -f hls')
}
// convertToHLS('./frag_bunny.mp4', () => {
// console.log('done')
// })
function getFileStream (ipfsHash, cb) {
ipfs.files.get(ipfsHash, (err, filesStream) => {
if (err) return cb(err)
filesStream.on('data', (file) => {
console.log('got file Obj ', file.path, ' size: ', file.size)
if (file.content) {
return cb(null, file.content)
}
})
filesStream.on('error', (err) => {
console.error('filesStream Err ', err)
return cb(err)
})
filesStream.on('end', () => {
console.log('filesStream ended!')
})
})
}
// getFileStream(testHash, (err, stream) => {
// if (err) throw err
//
// console.log('typeof stream: ', typeof stream)
// ffmpeg(stream)
// .ffprobe(0, (err, metadata) => {
// if (err) throw err
//
// // console.log(metadata)
// for (var stream of metadata.streams) {
// if (stream.codec_type === 'video') {
// console.log({
// width: stream.width,
// height: stream.height,
// display_aspect_ratio: stream.display_aspect_ratio
// })
// }
// }
// })
//
// convertToHLS(stream, () => {
// console.log('done')
// })
// })
function addDirToIPFS (dirPath, cb) {
let resp = null
ipfs.files.createAddStream((err, addStream) => {
if (err) return cb(err)
addStream.on('data', (file) => {
console.log('file Added ', file)
if ('/' + file.path === dirPath) {
// console.log('this is the hash to return ')
resp = file
}
})
addStream.on('end', () => {
console.log('addStream ended')
cb(null, resp)
})
fs.readdir(dirPath, (err, files) => {
if (err) return cb(err)
eachSeries(files, (file, next) => {
let rStream = fs.createReadStream(path.join(dirPath, file))
console.log('reading file ', file)
try {
addStream.write({
path: path.join(dirPath, file),
content: rStream
})
} catch (e) {
if (e) {
console.log('gotcha ', e)
}
} finally {
}
nextTick(() => next())
}, (err) => {
if (err) return cb(err)
addStream.end()
})
})
})
}
function convertAndAdd (ipfsHash, size, cb) {
let hashes = []
getFileStream(ipfsHash, (err, stream) => {
if (err) return cb(err)
fs.mkdtemp(path.join(os.tmpdir(), 'paratii-'), (err, folder) => {
if (err) return cb(err)
let command = ffmpeg(stream)
.inputOptions('-strict -2')
.addOption('-profile:v', 'baseline')
.addOption('-level', 3.0)
.addOption('-start_number', 0)
.videoCodec('libx264')
.size(size)
// set audio bitrate
.audioBitrate('128k')
// set audio codec
.audioCodec('aac')
// set number of audio channels
.audioChannels(2)
// set hls segments time
.addOption('-hls_time', 5)
// include all the segments in the list
.addOption('-hls_list_size', 0)
.addOption('-f', 'hls')
// .inputOptions('-strict -2 -profile:v baseline -level 3.0 -start_number 0 -hls_time 5 -hls_list_size 0 -f hls')
// .output(fileStream)
// .on('progress', (progress) => {
// // percentage is not available when using an input stream
// console.log('Processing: ', progress.percentage, ' % done')
// })
.on('stderr', (err) => {
if (err) console.error('stderr: ', err)
})
.on('end', () => {
console.log('finished processing file.')
addDirToIPFS(folder, cb)
// return cb()
})
.on('error', (err) => {
console.log('an error happened: ', err)
return cb(err)
})
.output(folder + '/master.m3u8')
.run()
})
})
}
// convertAndAdd(testHash, (err, resp) => {
// if (err) {
// console.log('error : ', err)
// }
// console.log('done ', resp)
// })
getResolution(testHash, (err, meta) => {
if (err) throw err
console.log('available conversions ', conversions[String(meta.height)])
const availableSizes = conversions[String(meta.height)]
convertAndAdd(testHash, availableSizes[0], (err, resp) => {
if (err) {
console.log('error : ', err)
}
console.log('done ', resp)
})
})