-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
149 lines (131 loc) · 4.45 KB
/
server.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
// Import modules
const express = require('express');
const multer = require('multer');
const Sequelize = require('sequelize');
const fs = require('fs');
const axios = require('axios');
const path = require('path');
const FormData = require('form-data');
const { execSync: exec } = require('child_process');
const ffmpegStatic = require('ffmpeg-static');
// Create an express app
const app = express();
var upload = multer({ dest: "uploads/" });
// Define a model for videos using sequelize
const env = process.env.NODE_ENV;
const sequelize = new Sequelize(
process.env.DB,
process.env.USER,
process.env.PASSWORD,
{
host: process.env.HOST,
dialect: "postgres"
});
const Video = sequelize.define('video', {
name: Sequelize.STRING,
path: Sequelize.STRING,
});
// Sync the model with the database
Video.sync();
// Create a route to handle file uploading
app.post('/upload', upload.single('video'), (req, res) => {
// Get the file from the request
const file = req.file;
// Create a new video record in the database with the file name and path
Video.create({
name: file.originalname,
path: file.path,
})
.then((video) => {
// Send a success response with the video id
res.redirect('/videos/' + video.id);
})
.catch((err) => {
// Handle any error
console.error(err);
res.status(500).json({ success: false, error: err.message });
});
});
//create a route for transcript
app.get('/transcript/:id', (req,res) =>{
const id = req.params.id;
// Find the video record in the database by id
Video.findByPk(id)
.then((video) => {
// Check if the video exists
if (video) {
// Get the file path from the video record
const filePath = video.path;
ffmpeg(`-hide_banner -y -i ${filePath} ${filePath}.mp3`);
const audioFile = path.join(__dirname, `${filePath}.mp3`);
const model = 'whisper-1';
const formData = new FormData();
formData.append("model", model);
formData.append("file", fs.createReadStream(audioFile))
axios
.post('https://api.openai.com/v1/audio/transcriptions', formData, {
headers:{
Authorization: `Bearer ${process.env.OPENAL_KEY}`,
'Content-Type' : `multipart/form-data; boundary = ${formData._boundary} `,
}
})
.then((transcript) =>{
var transcription = transcript.data;
res.send(transcription)
})
async function ffmpeg(command) {
try {
return new Promise((resolve, reject) => {
exec(`${ffmpegStatic} ${command}`, (err, stderr, stdout) => {
if (err) reject(err);
resolve(stdout);
});
});
} catch (error) {
console.error (error); // print the error if the promise is rejected
}
}
} else {
// If the video does not exist, send a not found response
res.status(404).json({ success: false, error: 'Record does not exist' });
}
})
.catch((err) => {
// Handle any error
console.error(err);
res.status(500).json({ success: false, error: err.message });
});
})
// Create a route to serve video files
app.get('/videos/:id', (req, res) => {
// Get the video id from the request parameters
const id = req.params.id;
// Find the video record in the database by id
Video.findByPk(id)
.then((video) => {
// Check if the video exists
if (video) {
// Get the file path from the video record
const filePath = video.path;
// Set the content type header
res.set('Content-Type', 'video/mp4');
// Create a read stream from the file and pipe it to the response
const stream = fs.createReadStream(filePath);
stream.pipe(res);
} else {
// If the video does not exist, send a not found response
res.status(404).json({ success: false, error: 'Record does not exist' });
}
})
.catch((err) => {
// Handle any error
console.error(err);
res.status(500).json({ success: false, error: err.message });
});
});
app.get('/', (req,res) => {
res.send("Chrome extension api")
})
app.listen(process.env.PORT, () => {
console.log('Server running');
});