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
|
const db = require('../db')
const dbFile = db.crud(db.File)
const path = require('path')
const ffprobe = require('node-ffprobe')
const execFile = require('child_process').execFile
const mimeFromExtension = require('./mimeFromExtension')
ffprobe.FFPROBE_PATH = process.env.FFPROBE_BINARY
let processing = false
module.exports = function processFiles() {
if (processing) return
processing = true
dbFile.index({ processed: false }).then( (files) => {
console.log(files.length + ' files left to process')
if (files.length === 0) {
processing = false
return
}
file = files.at(0)
console.log('processing', file.get('name'))
const mimeType = mimeFromExtension(file.get('name'))
file.set('mime', mimeType.mime)
file.set('type', mimeType.type)
switch (mimeType.type) {
case 'audio':
return processAudio(file)
default:
return processDone(file)
}
}).then( () => {
console.log('done')
if (processing) {
processing = false
setTimeout( processFiles )
}
})
}
function processAudio(file) {
const filePath = path.join(__dirname, '../..', 'public/data', String(file.get('folder_id')))
// console.log(filePath)
const fullPath = path.join(filePath, file.get('name'))
console.log(fullPath)
return new Promise ( (resolve, reject) => {
cacheFfprobe(file, fullPath)
.then(() => { return convertToAiff(fullPath) })
.then(() => { return processSpectrum(fullPath) })
.then(() => { return trimSpectrum(fullPath) })
.then(() => { return convertToMp3(fullPath) })
.then(() => { return processDone(file) })
.then(() => { return resolve() })
})
}
function processSpectrum(fullPath) {
return new Promise ( (resolve, reject) => {
console.log('process spectrum')
const aiffPath = fullPath + '.aiff'
const pngPath = fullPath + '.png'
execFile(process.env.PYTHON_BINARY, ['./python/spectrum.py', aiffPath, pngPath], (err, stdout, stderr) => {
// console.log('spectrum done')
resolve()
})
})
}
function trimSpectrum(fullPath) {
return new Promise ( (resolve, reject) => {
console.log('trim spectrum')
const thumbPath = fullPath + '.png'
execFile(process.env.CONVERT_BINARY, [thumbPath, '-trim', thumbPath], (err, stdout, stderr) => {
// console.log('trim done')
resolve()
})
})
}
function cacheFfprobe(file, fullPath) {
return new Promise ( (resolve, reject) => {
console.log('ffprobe')
ffprobe(fullPath, function(err, probeData) {
console.log(probeData)
// file.set('duration', probeData.format.duration)
file.set('analysis', JSON.stringify(probeData))
resolve()
})
})
}
function convertToAiff(fullPath) {
return new Promise ( (resolve, reject) => {
console.log('convert to aiff')
const aiffPath = fullPath + '.aiff'
execFile(process.env.FFMPEG_BINARY, ['-y', '-i', fullPath, '-acodec', 'pcm_s16le', '-ar', '44100', aiffPath], (err, stdout, stderr) => {
// console.log(stdout, stderr)
resolve()
})
})
}
function convertToMp3(fullPath) {
return new Promise ( (resolve, reject) => {
console.log('convert to mp3')
if (fullPath.match(/\.mp3$/i)) {
return resolve()
}
const mp3Path = fullPath + '.mp3'
execFile(process.env.FFMPEG_BINARY, ['-y', '-i', fullPath, '-q:a', '6', mp3Path], (err, stdout, stderr) => {
// console.log(stdout, stderr)
resolve()
})
})
}
function processImage(file) {
return new Promise ( (resolve, reject) => {
// process image?
resolve()
})
}
function processDone(file) {
return file.save({ processed: true })
}
|