Remove ffmpeg processing

- Remove ffmpeg video processing due to high cpu usages
pull/4/head
trivernis 5 years ago
parent 8351497355
commit 0bae164b9b

@ -66,6 +66,9 @@ imageFormat = "png"
# the max file size for uploading in bytes
maxFileSize = 10_485_760
# the max length of a video in seconds
maxVideoLenght = 60
# Configuration for the api rate limit
[api.rateLimit]

@ -1,5 +1,6 @@
import * as config from "config";
import * as crypto from "crypto";
import {FfprobeData} from "fluent-ffmpeg";
import * as ffmpeg from "fluent-ffmpeg";
import * as fsx from "fs-extra";
import * as path from "path";
@ -103,33 +104,14 @@ export class UploadManager {
/**
* Converts a video into a smaller format and .mp4 and returns the web path
* @param data
* @param width
* @param extension
*/
public async processAndStoreVideo(data: Buffer, width: number = 720): Promise<string> {
return new Promise(async (resolve, reject) => {
try {
const fileBasename = UploadManager.getCrypticFileName() + ".webm";
public async processAndStoreVideo(data: Buffer, extension: string): Promise<string> {
const fileBasename = UploadManager.getCrypticFileName() + extension;
await fsx.ensureDir(this.dataDir);
const filePath = path.join(this.dataDir, fileBasename);
const tempFile = filePath + ".tmp";
await fsx.writeFile(tempFile, data);
const video = ffmpeg(tempFile);
video
.size(`${width}x?`)
.toFormat("webm")
.on("end", async () => {
await fsx.unlink(tempFile);
resolve(`/${dataDirName}/${fileBasename}`);
})
.on("error", async (err) => {
await fsx.unlink(tempFile);
reject(err);
})
.save(filePath);
} catch (err) {
reject(err);
}
});
await fsx.writeFile(filePath, data);
return `/${dataDirName}/${fileBasename}`;
}
/**

@ -1,3 +1,4 @@
import * as config from "config";
import * as sqz from "sequelize";
import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript";
import markdown from "../markdown";
@ -112,7 +113,7 @@ export class Post extends Model<Post> {
public get media() {
const url = this.getDataValue("mediaUrl");
if (url) {
const type = url.endsWith(".webm") ? "VIDEO" : "IMAGE";
const type = url.endsWith(config.get("api.imageFormat")) ? "IMAGE" : "VIDEO";
return {
type,
url,

@ -206,7 +206,8 @@ export class UploadRoute extends Route {
if (is.image(postMedia.mimetype)) {
fileName = await this.uploadManager.processAndStoreImage(postMedia.data, 1080, 720, "inside");
} else if (is.video(postMedia.mimetype)) {
fileName = await this.uploadManager.processAndStoreVideo(postMedia.data, 1080);
fileName = await this.uploadManager.processAndStoreVideo(postMedia.data, postMedia.mimetype
.replace("video/", ""));
} else {
error = "Wrong type of file provided";
}

Loading…
Cancel
Save