-
Notifications
You must be signed in to change notification settings - Fork 0
/
fun.js
90 lines (73 loc) · 2.52 KB
/
fun.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
function transcribeVideoAudioWithNativeAPI(videoElement) {
// Create an audio context
const audioContext = new (window.AudioContext || window.webkitAudioContext)()
// Create a media element source
const mediaElementSource = audioContext.createMediaElementSource(videoElement)
// Connect the source to the destination (output)
mediaElementSource.connect(audioContext.destination)
// Create a script processor node to capture audio data
const scriptProcessor = audioContext.createScriptProcessor(4096, 1, 1)
scriptProcessor.onaudioprocess = function (event) {
const inputBuffer = event.inputBuffer
const audioData = inputBuffer.getChannelData(0)
// Send audioData to the server for transcription
sendAudioDataToServer(audioData)
}
// Connect the script processor node to the source
mediaElementSource.connect(scriptProcessor)
scriptProcessor.connect(audioContext.destination)
}
function sendAudioDataToServer(audioData) {
// Convert audioData to a format suitable for sending to the server
const audioBlob = new Blob([audioData], { type: 'audio/wav' })
// Create a FormData object to send the audio data
const formData = new FormData()
formData.append('audio', audioBlob, 'audio.wav')
// Send the audio data to the server using fetch or XMLHttpRequest
fetch('/transcribe', {
method: 'POST',
body: formData,
})
.then((response) => response.json())
.then((data) => {
console.log('Transcription:', data.transcription)
})
.catch((error) => {
console.error('Error:', error)
})
}
//server.js
const express = require('express')
const multer = require('multer')
const fs = require('fs')
const speech = require('@google-cloud/speech')
const app = express()
const upload = multer({ dest: 'uploads/' })
app.post('/transcribe', upload.single('audio'), async (req, res) => {
const client = new speech.SpeechClient()
const audio = {
content: fs.readFileSync(req.file.path).toString('base64'),
}
const config = {
encoding: 'LINEAR16',
sampleRateHertz: 44100,
languageCode: 'zh-CN',
}
const request = {
audio: audio,
config: config,
}
try {
const [response] = await client.recognize(request)
const transcription = response.results
.map((result) => result.alternatives[0].transcript)
.join('\n')
res.json({ transcription })
} catch (error) {
console.error('Error:', error)
res.status(500).json({ error: 'Transcription failed' })
}
})
app.listen(3000, () => {
console.log('Server is running on port 3000')
})