Skip to content

Commit

Permalink
video worker added
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Jun 28, 2024
1 parent ff4690b commit 7fba408
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 66 deletions.
2 changes: 1 addition & 1 deletion lib/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function _encodeImages(
//number of concurrent process.
/* 50 percentage of core count If there is no cpu allocation in Settings */
const threadCount: number =
cpuAllocation ?? Math.round((50 * cpus().length) / 100);
cpuAllocation ?? Math.floor((70 * cpus().length) / 100);

const pool = new ImagePool(threadCount);

Expand Down
104 changes: 104 additions & 0 deletions lib/video.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { encodeAll } from "handstop";
import { freemem } from "os";
import { basename, dirname, extname, join, relative } from "path";
import { currentTime, makeDirf } from "./utils";

import ffmpegIns from "@ffmpeg-installer/ffmpeg";
import ffprobeIns from "@ffprobe-installer/ffprobe";
import ffmpeg from "fluent-ffmpeg";

ffmpeg.setFfmpegPath(ffmpegIns.path);
ffmpeg.setFfprobePath(ffprobeIns.path);

function _thumbnailGenerator(
videoPath: string,
seekPercentage: number,
basepath: string,
): Promise<string> {
const outputFramePath: string = join(
basepath,
relative(".", dirname(videoPath)),
"thumbnails",
);
const filename: string =
basename(videoPath, extname(videoPath)) + ".jpg";

//create dir if not exist
makeDirf(outputFramePath);

return new Promise((resolve, reject) => {
// Get video duration
ffmpeg.ffprobe(videoPath, (err: Error, metadata) => {
if (err) {
reject("Error while getting video duration:" + err.message);
}

const durationInSeconds: number = metadata.format?.duration ?? 0;

// Calculate seek time based on percentage
const seekTime: number = (seekPercentage / 100) * durationInSeconds;

// Create the ffmpeg command
ffmpeg(videoPath)
.screenshot({
count: 1,
folder: outputFramePath,
filename: filename,
timestamps: [seekTime],
})
.on("end", () => {
resolve(
`Screenshot saved in ${join(outputFramePath, filename)}`,
);
})
.on("error", (err: Error) => {
reject("Error while taking screenshot:" + err.message);
});
});
});
}

export async function videoWorker(
videoPaths: string[],
codecType: "wav1" | "mav1" | "mx265" = "wav1",
encodeLevel: 1 | 2 | 3 = 1,
basepath: string = "dist",
thumbnailSeekPercent: number = 15, //1-100
) {
const availmem: number = Math.floor(freemem() / 1024 / 1024);

let batchSize: number = Math.floor(availmem / 1500);
batchSize = batchSize ? batchSize : 1;

try {
console.log(`\n[${currentTime()}] +++> Video Encoding started.`);

console.log(`Number of videos in queue: ${videoPaths.length}`);
console.log(`Number of encodes at a time: ${batchSize}`);

const { success } = await encodeAll(
videoPaths,
basepath,
codecType,
encodeLevel,
batchSize,
);

if (!success) {
console.log("Passive error while encoding");
}

console.log(`[${currentTime()}] ===> Videos were encoded.`);

console.log(`\n[${currentTime()}] +++> Thumbnail generation started.`);

for (const videoPath of videoPaths) {
await _thumbnailGenerator(videoPath, thumbnailSeekPercent, basepath);
}

console.log(`[${currentTime()}] ===> Thumbnails were generated.`);
} catch (error: any) {
console.log(error);
process.exit(1);
}
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dr": "cls && yarn dev && yarn rp",
"rp": "node ./dist/minomax.js",
"test": "jest --runInBand",
"build": "cls && eslint && rimraf dist && tsc -p tsconfig.json && ncp ./minomax.config.json ./dist/minomax.config.json",
"build": "cls && rimraf dist && tsc -p tsconfig.json && ncp ./minomax.config.json ./dist/minomax.config.json",
"watch": "tsc --watch",
"clean": "cls && rimraf dist",
"deploy": "yarn test && yarn build && yarn publish --access public && git push"
Expand Down Expand Up @@ -76,19 +76,20 @@
"cheerio": "1.0.0-rc.12",
"fluent-ffmpeg": "2.1.2",
"glob": "10.4.2",
"handstop": "1.0.0",
"lodash": "4.17.21",
"pixteroid": "A:/Gitocal/Pixteroid",
"puppeteer": "21.11.0",
"sharp": "0.32.6",
"svgo": "3.3.2",
"twohandbrake": "^1.0.8",
"yargs": "17.7.2"
},
"devDependencies": {
"@babel/core": "7.24.5",
"@babel/preset-env": "7.24.5",
"@babel/preset-typescript": "7.24.1",
"@jest/test-sequencer": "29.7.0",
"@types/fluent-ffmpeg": "^2.1.24",
"@types/jest": "29.5.12",
"@types/node": "^20.14.8",
"babel-jest": "29.7.0",
Expand All @@ -101,4 +102,4 @@
"jest": {
"testSequencer": "./test/custom_test_sequence.js"
}
}
}
Binary file not shown.
Binary file added test/samples/videos/FaceRecon.mp4
Binary file not shown.
9 changes: 5 additions & 4 deletions tscdev.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "CommonJS",
"moduleResolution": "Node10",
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"baseUrl": ".",
"rootDir": ".",
"outDir": "dist",
Expand All @@ -28,7 +28,8 @@
"noUnusedParameters": true,
"noUncheckedIndexedAccess": false,
"sourceMap": false,
"removeComments": true
"removeComments": true,
"skipLibCheck": true
},
"exclude": [
"node_modules",
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"noUnusedParameters": true,
"noUncheckedIndexedAccess": false,
"sourceMap": false,
"removeComments": true
"removeComments": true,
"skipLibCheck": true
},
"exclude": [
"node_modules",
Expand Down
73 changes: 16 additions & 57 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@
"@types/istanbul-lib-coverage" "^2.0.0"
collect-v8-coverage "^1.0.0"

"@jest/test-sequencer@^29.7.0":
"@jest/test-sequencer@29.7.0", "@jest/test-sequencer@^29.7.0":
version "29.7.0"
resolved "https://registry.yarnpkg.com/@jest/test-sequencer/-/test-sequencer-29.7.0.tgz#6cef977ce1d39834a3aea887a1726628a6f072ce"
integrity sha512-GQwJ5WZVrKnOJuiYiAF52UNUJXgTZx1NHjFSEB0qEMmSZKAkdMoIzw/Cj6x6NF4AvV23AUqDpFzQkN/eYCYTxw==
Expand Down Expand Up @@ -1509,6 +1509,13 @@
dependencies:
"@babel/types" "^7.20.7"

"@types/fluent-ffmpeg@^2.1.24":
version "2.1.24"
resolved "https://registry.yarnpkg.com/@types/fluent-ffmpeg/-/fluent-ffmpeg-2.1.24.tgz#f53c57700bc4360ac638554545c8da2c465434c1"
integrity sha512-g5oQO8Jgi2kFS3tTub7wLvfLztr1s8tdXmRd8PiL/hLMLzTIAyMR2sANkTggM/rdEDAg3d63nYRRVepwBiCw5A==
dependencies:
"@types/node" "*"

"@types/graceful-fs@^4.1.3":
version "4.1.9"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.9.tgz#2a06bc0f68a20ab37b3e36aa238be6abdf49e8b4"
Expand Down Expand Up @@ -2169,16 +2176,6 @@ csso@^5.0.5:
dependencies:
css-tree "~2.2.0"

current-module-paths@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/current-module-paths/-/current-module-paths-1.1.1.tgz#f0ee3298cd1a6839503b46796aac9e19add3f9e6"
integrity sha512-8Ga5T8oMXBaSsHq9Gj+bddX7kHSaJKsl2vaAd3ep51eQLkr4W18eFEmEZM5bLo1zrz8tt3jE1U8QK9QGhaLR4g==

data-uri-to-buffer@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e"
integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==

data-uri-to-buffer@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-6.0.2.tgz#8a58bb67384b261a38ef18bea1810cb01badd28b"
Expand Down Expand Up @@ -2498,14 +2495,6 @@ fd-slicer@~1.1.0:
dependencies:
pend "~1.2.0"

fetch-blob@^3.1.2, fetch-blob@^3.1.4:
version "3.2.0"
resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9"
integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==
dependencies:
node-domexception "^1.0.0"
web-streams-polyfill "^3.0.3"

file-type@^3.8.0:
version "3.9.0"
resolved "https://registry.yarnpkg.com/file-type/-/file-type-3.9.0.tgz#257a078384d1db8087bc449d107d52a52672b9e9"
Expand Down Expand Up @@ -2552,13 +2541,6 @@ foreground-child@^3.1.0:
cross-spawn "^7.0.0"
signal-exit "^4.0.1"

formdata-polyfill@^4.0.10:
version "4.0.10"
resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423"
integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==
dependencies:
fetch-blob "^3.1.2"

fs-constants@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad"
Expand Down Expand Up @@ -2672,6 +2654,14 @@ graceful-fs@^4.1.10, graceful-fs@^4.1.6, graceful-fs@^4.2.0, graceful-fs@^4.2.9:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.11.tgz#4183e4e8bf08bb6e05bbb2f7d2e0c8f712ca40e3"
integrity sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/handstop/-/handstop-1.0.0.tgz#1749cb11eab117581e7dafbead05b191901c5f44"
integrity sha512-l8SB0fJuPJ1CQaF7WovqtfsEDMfHm0Z1HDwP9qXj3BR1g5N3uCcyZ7rjDnvngnGGrFyqhs/YYChPifGnzQJV0g==
dependencies:
decompress "^4.2.1"
rimraf "5.0.5"

has-flag@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
Expand Down Expand Up @@ -3492,27 +3482,13 @@ node-addon-api@^6.1.0:
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-6.1.0.tgz#ac8470034e58e67d0c6f1204a18ae6995d9c0d76"
integrity sha512-+eawOlIgy680F0kBzPUNFhMZGtJ1YmqM6l4+Crf4IkImjYrO/mqPwRMh352g23uIaQKFItcQ64I7KMaJxHgAVA==

node-domexception@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==

node-fetch@^2.6.12:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
dependencies:
whatwg-url "^5.0.0"

node-fetch@^3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.2.tgz#d1e889bacdf733b4ff3b2b243eb7a12866a0b78b"
integrity sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==
dependencies:
data-uri-to-buffer "^4.0.0"
fetch-blob "^3.1.4"
formdata-polyfill "^4.0.10"

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down Expand Up @@ -3954,13 +3930,6 @@ [email protected]:
dependencies:
glob "^10.3.7"

rimraf@^5.0.5:
version "5.0.7"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-5.0.7.tgz#27bddf202e7d89cb2e0381656380d1734a854a74"
integrity sha512-nV6YcJo5wbLW77m+8KjH8aB/7/rxQy9SZ0HY5shnwULfS+9nmTtVXAJET5NdZmCzA4fPI/Hm1wo/Po/4mopOdg==
dependencies:
glob "^10.3.7"

safe-buffer@^5.0.1, safe-buffer@^5.1.1, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
Expand Down Expand Up @@ -4401,16 +4370,6 @@ tunnel-agent@^0.6.0:
dependencies:
safe-buffer "^5.0.1"

twohandbrake@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/twohandbrake/-/twohandbrake-1.0.8.tgz#53b3a8c47b28993262ba638488e2451bbf873068"
integrity sha512-t9ieOyPcrqNOnCzSXDJ08mZIwBT+Ut0I51ii3T8CQIZN9OW9nsxCOTnGW9G8GRlZrol54+WXvbNKUMXSFTbvMw==
dependencies:
current-module-paths "^1.1.1"
decompress "^4.2.1"
node-fetch "^3.3.2"
rimraf "^5.0.5"

[email protected]:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
Expand Down

0 comments on commit 7fba408

Please sign in to comment.