Last active
October 2, 2019 11:42
-
-
Save valentineus/9439f9735664b53d479c483a849b8a2a to your computer and use it in GitHub Desktop.
An example of recording and converting to mp3 to ElectronJS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ffmpeg from 'fluent-ffmpeg'; | |
import { settings } from '../../../../system'; | |
export default (incomingPatchFile, outgoingPatchFile) => { | |
const ffmpegPath = settings.get('system.ffmpeg.path', ''); | |
ffmpeg.setFfmpegPath(ffmpegPath); | |
return new Promise((resolve, reject) => { | |
const command = ffmpeg(incomingPatchFile) | |
.audioBitrate('32k') | |
.audioChannels(1) | |
.audioCodec('libmp3lame') | |
.audioFrequency(22050) | |
.audioQuality(4) | |
.format('mp3') | |
.noVideo(); | |
command.on('error', (error) => reject(error)); | |
// command.on('start', (commandLine) => { | |
// logger.debug('Успешно инициализирован ffmpeg', { commandLine }); | |
// }); | |
command.on('end', () => resolve()); | |
command.output(outgoingPatchFile).run(); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
export default (filePath, body, type) => new Promise((resolve, reject) => { | |
fs.appendFile(filePath, body, type, (error) => { | |
if (error) { reject(error); } | |
resolve(); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { action, computed, observable, observe } from 'mobx'; | |
import { ipcRenderer } from 'electron'; | |
import getUserMedia from 'getusermedia'; | |
const eventName = ''; | |
class MessageStore { | |
@observable dataChunks = []; | |
@observable fileReader = new FileReader(); | |
@observable recorder = null; | |
@observable recording = false; | |
constructor() { | |
this.fileReader.addEventListener('loadend', () => { | |
// this.send(); | |
}); | |
getUserMedia({ | |
audio: true, | |
video: false | |
}, (error, stream) => { | |
if (error) { return; } | |
this.initRecorder(stream); | |
}); | |
} | |
initRecorder = (stream) => { | |
this.recorder = new MediaRecorder(stream); | |
this.recorder.addEventListener('dataavailable', ({ data }) => { | |
this.dataChunks.push(data); | |
}); | |
this.recorder.addEventListener('start', () => { | |
this.recording = true; | |
}); | |
this.recorder.addEventListener('stop', () => { | |
const audioBlob = new Blob(this.dataChunks); | |
this.fileReader.readAsDataURL(audioBlob); | |
this.dataChunks.clear(); | |
this.recording = false; | |
}); | |
} | |
@computed get isRecording() { | |
return this.recording; | |
} | |
@action startRecording() { | |
if (!this.isDisabled) { | |
this.recorder.start(); | |
} | |
} | |
@action stopRecording() { | |
if (this.isRecording) { | |
this.recorder.stop(); | |
} | |
} | |
} | |
export default new MessageStore(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MemoryStore from 'better-queue-memory'; | |
import Queue from 'better-queue'; | |
import { createHash } from 'crypto'; | |
import { ipcMain } from 'electron'; | |
import fileConverter from './send-messages/file-converter'; | |
import fileSaver from './send-messages/file-saver'; | |
import { settings, temp } from '../../../system'; | |
const eventName = ''; | |
const queueSendRequest = new Queue(async ({ account, data, event, user }, callback) => { | |
const clientId = settings.get('system.machine.id', ''); | |
const host = settings.get('system.stream.host', ''); | |
const port = settings.get('system.stream.port', ''); | |
const timestamp = Date.now(); | |
// Подготовка хранения файла | |
const dirPath = temp.mkdirSync('audio'); | |
const fileHash = createHash('sha256').update(`${account}${timestamp}`).digest('hex'); | |
const filePath = `${dirPath}/${fileHash}`; | |
// Подготовка удалённого адреса файла | |
const remoteFileName = `voice_${user.id}_${clientId}_${timestamp}`; | |
const remoteFilePath = `http://${host}:${port}/stream/${remoteFileName}.mp3`; | |
// Формирование тела файла | |
const body = data.replace(/^data:.+\/(.+);base64,/gm, ''); | |
// Запись исходного файла | |
await fileSaver(filePath, body, 'base64'); | |
// Конвертирование файла | |
await fileConverter(filePath, remoteFilePath); | |
event.sender.send(`${eventName}:response`, { | |
data: content, | |
error: null | |
}); | |
callback(null, { result: 'OK' }); | |
}, { store: new MemoryStore() }); | |
const handlerSendAudioMessages = (event, { account, data, user }) => { | |
queueSendRequest.push({ account, data, event, user }); | |
}; | |
ipcMain.on(eventName, handlerSendAudioMessages); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment