mp3 conversion - done.
added nodemon for dev efficiency.
This commit is contained in:
parent
d4695ee48e
commit
8a5cb7afa8
4 changed files with 945 additions and 66 deletions
875
package-lock.json
generated
875
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -4,7 +4,8 @@
|
||||||
"description": "A simple Node app built on fastify, instantly up and running.",
|
"description": "A simple Node app built on fastify, instantly up and running.",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server.js"
|
"start": "node server.js",
|
||||||
|
"dev": "nodemon server.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dotenv": "^11.0.0",
|
"dotenv": "^11.0.0",
|
||||||
|
|
@ -33,5 +34,8 @@
|
||||||
"node",
|
"node",
|
||||||
"glitch",
|
"glitch",
|
||||||
"express"
|
"express"
|
||||||
]
|
],
|
||||||
|
"devDependencies": {
|
||||||
|
"nodemon": "^2.0.15"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ function submitForm(event) {
|
||||||
|
|
||||||
//
|
//
|
||||||
var request = new XMLHttpRequest();
|
var request = new XMLHttpRequest();
|
||||||
request.open("POST", "/");
|
request.open("POST", "/entry");
|
||||||
request.send(fd);
|
request.send(fd);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
||||||
106
server.js
106
server.js
|
|
@ -2,7 +2,7 @@
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
//ffmpeg
|
//ffmpeg
|
||||||
var ffmpeg = require('ffmpeg');
|
var ffmpeg = require('fluent-ffmpeg');
|
||||||
|
|
||||||
//nextcloud client
|
//nextcloud client
|
||||||
const { Client, Server, GetFilesRecursivelyCommand, CommandStatus } = require("nextcloud-node-client");
|
const { Client, Server, GetFilesRecursivelyCommand, CommandStatus } = require("nextcloud-node-client");
|
||||||
|
|
@ -18,8 +18,8 @@ const server = new Server({
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require('fs').promises;
|
const fs = require('fs').promises;
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const { pipeline } = require('stream');
|
// const { pipeline } = require('stream');
|
||||||
const pump = util.promisify(pipeline);
|
// const pump = util.promisify(pipeline);
|
||||||
|
|
||||||
//uuid
|
//uuid
|
||||||
const {
|
const {
|
||||||
|
|
@ -52,19 +52,66 @@ var io = require("socket.io")(fastify.server, {
|
||||||
pingTimeout: 3000
|
pingTimeout: 3000
|
||||||
});
|
});
|
||||||
|
|
||||||
//'get'
|
//view '/'
|
||||||
fastify.get("/", function (request, reply) {
|
fastify.get("/", function (request, reply) {
|
||||||
reply.view("/src/pages/parade.hbs", {});
|
reply.view("/src/pages/parade.hbs", {});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//view '/entry'
|
||||||
["/entry", "/entry/"].forEach(function(path) {
|
["/entry", "/entry/"].forEach(function(path) {
|
||||||
fastify.get(path, function (request, reply) {
|
fastify.get(path, function (request, reply) {
|
||||||
reply.view("/src/pages/entry.hbs", {});
|
reply.view("/src/pages/entry.hbs", {});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// --> https://stackoverflow.com/a/40899275
|
// --> https://stackoverflow.com/a/40899275
|
||||||
// all the regex doesn't work... last resort.
|
// all the regex didn't work for me -- a 'last resort' method
|
||||||
|
|
||||||
|
//view '/entry (post)'
|
||||||
|
fastify.post("/entry", async function (request, reply) {
|
||||||
|
|
||||||
|
// stores files to tmp dir and return paths
|
||||||
|
const files = await request.saveRequestFiles();
|
||||||
|
let audiofile = files.find(f => f.fieldname == 'audiofile');
|
||||||
|
let pixelfile = files.find(f => f.fieldname == 'pixels');
|
||||||
|
let tmpdir = path.dirname(audiofile.filepath);
|
||||||
|
// console.log(audiofile.fields.message.value);
|
||||||
|
|
||||||
|
//convert to mp3
|
||||||
|
function converter() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
//
|
||||||
|
let outputFile = tmpdir + "/converted.mp3";
|
||||||
|
// --> https://stackoverflow.com/a/36109219 (a quick tip on fluent-ffmpeg)
|
||||||
|
ffmpeg()
|
||||||
|
.addInput(audiofile.filepath)
|
||||||
|
.on("error", function(err) {
|
||||||
|
reject(err);
|
||||||
|
})
|
||||||
|
.on("end", function() {
|
||||||
|
resolve(outputFile);
|
||||||
|
})
|
||||||
|
.outputOptions('-b:a 192000')
|
||||||
|
.output(outputFile)
|
||||||
|
.run();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
await converter();
|
||||||
|
// console.log(await fs.readdir(tmpdir));
|
||||||
|
|
||||||
|
//upload
|
||||||
|
const client = new Client(server);
|
||||||
|
|
||||||
|
//create unique folder ==> timestamp + uuid
|
||||||
|
const folder = await client.createFolder("Storage/public/sound-parade/" + moment().tz('Asia/Seoul').format('YYYYMMDD-HHmmss - ') + uuidv1());
|
||||||
|
|
||||||
|
const file = await folder.createFile("audio.mp3", await fs.readFile(tmpdir + '/converted.mp3'));
|
||||||
|
const text = await folder.createFile("message.txt", Buffer.from(audiofile.fields.message.value));
|
||||||
|
const image = await folder.createFile("pixels.png", await fs.readFile(pixelfile.filepath));
|
||||||
|
|
||||||
|
reply.view("/src/pages/entry.hbs", {});
|
||||||
|
});
|
||||||
|
|
||||||
|
//view '/list'
|
||||||
["/list", "/list/"].forEach(function(path) {
|
["/list", "/list/"].forEach(function(path) {
|
||||||
fastify.get(path, async function (request, reply) {
|
fastify.get(path, async function (request, reply) {
|
||||||
|
|
||||||
|
|
@ -125,54 +172,7 @@ fastify.get("/", function (request, reply) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
//'post'
|
//socket.io
|
||||||
fastify.post("/", async function (request, reply) {
|
|
||||||
|
|
||||||
// stores files to tmp dir and return paths
|
|
||||||
const files = await request.saveRequestFiles();
|
|
||||||
let audiofile = files.find(f => f.fieldname == 'audiofile');
|
|
||||||
let pixelfile = files.find(f => f.fieldname == 'pixels');
|
|
||||||
// console.log(audiofile.fields.message.value);
|
|
||||||
|
|
||||||
const client = new Client(server);
|
|
||||||
|
|
||||||
//create unique folder ==> timestamp + uuid
|
|
||||||
const folder = await client.createFolder("Storage/public/sound-parade/" + moment().tz('Asia/Seoul').format('YYYYMMDD-HHmmss - ') + uuidv1());
|
|
||||||
|
|
||||||
//convert to mp3
|
|
||||||
let dir = path.dirname(audiofile.filepath);
|
|
||||||
function converter() {
|
|
||||||
return new Promise((resolve,reject) => {
|
|
||||||
var process = new ffmpeg(audiofile.filepath);
|
|
||||||
process.then(function (video) {
|
|
||||||
// Callback mode
|
|
||||||
video.fnExtractSoundToMP3(dir + '/converted.mp3', function (error, file) {
|
|
||||||
if (!error) {
|
|
||||||
console.log('Audio file: ' + file);
|
|
||||||
resolve()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, function (err) {
|
|
||||||
console.log('Error: ' + err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await converter();
|
|
||||||
|
|
||||||
const file = await folder.createFile(audiofile.filename, await fs.readFile(audiofile.filepath));
|
|
||||||
// const text = await folder.createFile("message.txt", Buffer.from(audiofile.fields.message.value));
|
|
||||||
// const image = await folder.createFile(pixelfile.filename, await fs.readFile(pixelfile.filepath));
|
|
||||||
|
|
||||||
// //mp3 conversion
|
|
||||||
// console.log(await fs.readdir(dir));
|
|
||||||
//
|
|
||||||
//
|
|
||||||
console.log(await fs.readdir(dir));
|
|
||||||
|
|
||||||
reply.view("/src/pages/entry.hbs", {});
|
|
||||||
});
|
|
||||||
|
|
||||||
//
|
|
||||||
var score = require("./public/score.json");
|
var score = require("./public/score.json");
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue