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 # the max file size for uploading in bytes
maxFileSize = 10_485_760 maxFileSize = 10_485_760
# the max length of a video in seconds
maxVideoLenght = 60
# Configuration for the api rate limit # Configuration for the api rate limit
[api.rateLimit] [api.rateLimit]

@ -1,5 +1,6 @@
import * as config from "config"; import * as config from "config";
import * as crypto from "crypto"; import * as crypto from "crypto";
import {FfprobeData} from "fluent-ffmpeg";
import * as ffmpeg from "fluent-ffmpeg"; import * as ffmpeg from "fluent-ffmpeg";
import * as fsx from "fs-extra"; import * as fsx from "fs-extra";
import * as path from "path"; 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 * Converts a video into a smaller format and .mp4 and returns the web path
* @param data * @param data
* @param width * @param extension
*/ */
public async processAndStoreVideo(data: Buffer, width: number = 720): Promise<string> { public async processAndStoreVideo(data: Buffer, extension: string): Promise<string> {
return new Promise(async (resolve, reject) => { const fileBasename = UploadManager.getCrypticFileName() + extension;
try { await fsx.ensureDir(this.dataDir);
const fileBasename = UploadManager.getCrypticFileName() + ".webm"; const filePath = path.join(this.dataDir, fileBasename);
await fsx.ensureDir(this.dataDir); await fsx.writeFile(filePath, data);
const filePath = path.join(this.dataDir, fileBasename); return `/${dataDirName}/${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);
}
});
} }
/** /**

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

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

Loading…
Cancel
Save