ref: better import for utils

fix: inappropriate ffmpeg path in `immediatelyExtractFrameFromVideo()`
This commit is contained in:
alikia2x (寒寒) 2025-01-02 00:26:50 +08:00
parent 38185cc969
commit a0a90f2428
Signed by: alikia2x
GPG Key ID: 56209E0CCD8420C6
13 changed files with 45 additions and 17 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -41,6 +41,7 @@
"image-size": "^1.1.1", "image-size": "^1.1.1",
"jotai": "^2.11.0", "jotai": "^2.11.0",
"memory-cache": "^0.2.0", "memory-cache": "^0.2.0",
"pino": "^9.6.0",
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1", "react-dom": "^18.3.1",
"react-i18next": "^15.1.2", "react-i18next": "^15.1.2",

View File

@ -4,8 +4,8 @@ import fs from "fs";
import path, { join } from "path"; import path, { join } from "path";
import type { EncodingTask, Frame } from "./schema"; import type { EncodingTask, Frame } from "./schema";
import sizeOf from "image-size"; import sizeOf from "image-size";
import { getEncodeCommand } from "../utils/video/index.js"; import { getEncodeCommand } from "../utils/index.js";
import { getRecordingsDir, getEncodingTempDir, getScreenshotsDir } from "../utils/fs/index.js"; import { getRecordingsDir, getEncodingTempDir, getScreenshotsDir } from "../utils/index.js";
import cache from "memory-cache"; import cache from "memory-cache";
import { ENCODING_FRAME_INTERVAL, RECORD_FRAME_RATE as FRAME_RATE } from "./consts.js"; import { ENCODING_FRAME_INTERVAL, RECORD_FRAME_RATE as FRAME_RATE } from "./consts.js";

View File

@ -2,7 +2,7 @@ import * as path from "path";
import { Database } from "better-sqlite3"; import { Database } from "better-sqlite3";
import DB from "better-sqlite3"; import DB from "better-sqlite3";
import { __dirname } from "../dirname.js"; import { __dirname } from "../dirname.js";
import { getDatabaseDir } from "../utils/fs/index.js"; import { getDatabaseDir } from "../utils/index.js";
import { migrate } from "./migrate/index.js"; import { migrate } from "./migrate/index.js";
function getLibSimpleExtensionPath() { function getLibSimpleExtensionPath() {

View File

@ -1,5 +1,5 @@
import screenshot from "screenshot-desktop"; import screenshot from "screenshot-desktop";
import { getScreenshotsDir } from "../utils/fs/index.js"; import { getScreenshotsDir } from "../utils/index.js";
import { join } from "path"; import { join } from "path";
import { Database } from "better-sqlite3"; import { Database } from "better-sqlite3";
import SqlString from "sqlstring"; import SqlString from "sqlstring";

View File

@ -2,7 +2,7 @@ import { app, BrowserWindow, screen } from "electron";
import { join } from "path"; import { join } from "path";
import { __dirname } from "./dirname.js"; import { __dirname } from "./dirname.js";
import windowStateManager from "electron-window-state"; import windowStateManager from "electron-window-state";
import { hideDock, showDock } from "./utils/platform/index.js"; import { hideDock, showDock } from "./utils/index.js";
function loadURL(window: BrowserWindow, path = "", vitePort: string) { function loadURL(window: BrowserWindow, path = "", vitePort: string) {
const dev = !app.isPackaged; const dev = !app.isPackaged;

View File

@ -16,7 +16,7 @@ import { initDatabase } from "./backend/init.js";
import { Database } from "better-sqlite3"; import { Database } from "better-sqlite3";
import { startScreenshotLoop } from "./backend/screenshot.js"; import { startScreenshotLoop } from "./backend/screenshot.js";
import { __dirname } from "./dirname.js"; import { __dirname } from "./dirname.js";
import { hideDock } from "./utils/platform/index.js"; import { hideDock } from "./utils/index.js";
import { import {
checkFramesForEncoding, checkFramesForEncoding,
deleteUnnecessaryScreenshots, deleteUnnecessaryScreenshots,
@ -24,7 +24,7 @@ import {
} from "./backend/encoding.js"; } from "./backend/encoding.js";
import honoApp from "./server/index.js"; import honoApp from "./server/index.js";
import { serve } from "@hono/node-server"; import { serve } from "@hono/node-server";
import { findAvailablePort } from "./utils/network/index.js"; import { findAvailablePort } from "./utils/index.js";
import cache from "memory-cache"; import cache from "memory-cache";
import { generate as generateAPIKey } from "@alikia/random-key"; import { generate as generateAPIKey } from "@alikia/random-key";

View File

@ -10,8 +10,8 @@ import {
getRecordingsDir, getRecordingsDir,
getScreenshotsDir, getScreenshotsDir,
waitForFileExists waitForFileExists
} from "../utils/fs/index.js"; } from "../utils/index.js";
import { immediatelyExtractFrameFromVideo } from "../utils/video/index.js"; import { immediatelyExtractFrameFromVideo } from "../utils/index.js";
import { existsSync } from "fs"; import { existsSync } from "fs";
const app = new Hono(); const app = new Hono();

View File

@ -59,6 +59,15 @@ export function getDecodingTempDir() {
return decodingTempDir; return decodingTempDir;
} }
export function getLogDir() {
const dataDir = createDataDir();
const logDir = path.join(dataDir, "logs");
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true });
}
return logDir;
}
export async function waitForFileExists(filePath: string, timeout: number = 10000): Promise<void> { export async function waitForFileExists(filePath: string, timeout: number = 10000): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.access(filePath, fs.constants.F_OK, (err) => { fs.access(filePath, fs.constants.F_OK, (err) => {

View File

@ -2,3 +2,4 @@ export * from "./fs/index.js";
export * from "./platform/index.js"; export * from "./platform/index.js";
export * from "./video/index.js"; export * from "./video/index.js";
export * from "./network/index.js"; export * from "./network/index.js";
export * from "./logging/index.js";

View File

@ -0,0 +1,10 @@
import pino from "pino";
import { join } from "path";
import { getLogDir } from "../fs/index.js";
const logPath = join(getLogDir(), "log.json");
const dest = pino.destination(logPath);
const logger = pino(dest);
export { logger };

View File

@ -1,14 +1,15 @@
import path from "path"; import { join } from "path";
import os from "os"; import os from "os";
import { app } from "electron"; import { app } from "electron";
import { __dirname } from "../../dirname.js"; import { __dirname } from "../../dirname.js";
import { logger } from "../index.js";
export function getUserDataDir() { export function getUserDataDir() {
switch (process.platform) { switch (process.platform) {
case "win32": case "win32":
return path.join(process.env.APPDATA!, "OpenRewind", "Record Data"); return join(process.env.APPDATA!, "OpenRewind", "Record Data");
case "darwin": case "darwin":
return path.join( return join(
os.homedir(), os.homedir(),
"Library", "Library",
"Application Support", "Application Support",
@ -16,7 +17,7 @@ export function getUserDataDir() {
"Record Data" "Record Data"
); );
case "linux": case "linux":
return path.join(os.homedir(), ".config", "OpenRewind", "Record Data"); return join(os.homedir(), ".config", "OpenRewind", "Record Data");
default: default:
throw new Error("Unsupported platform"); throw new Error("Unsupported platform");
} }
@ -37,14 +38,20 @@ export function showDock() {
} }
export function getFFmpegPath() { export function getFFmpegPath() {
let path = "";
switch (process.platform) { switch (process.platform) {
case "win32": case "win32":
return path.join(__dirname, "bin", process.platform, "ffmpeg.exe"); path = join(__dirname, "bin", process.platform, "ffmpeg.exe");
break;
case "darwin": case "darwin":
return path.join(__dirname, "bin", process.platform, "ffmpeg"); path = join(__dirname, "bin", process.platform, "ffmpeg");
break;
case "linux": case "linux":
return path.join(__dirname, "bin", process.platform, "ffmpeg"); path = join(__dirname, "bin", process.platform, "ffmpeg");
break;
default: default:
throw new Error("Unsupported platform"); throw new Error("Unsupported platform");
} }
logger.info("FFmpeg path: %s", path);
return path;
} }

View File

@ -44,7 +44,7 @@ export function immediatelyExtractFrameFromVideo(
"1", "1",
`${outputPathArg}` `${outputPathArg}`
]; ];
const ffmpeg = spawn("ffmpeg", args); const ffmpeg = spawn(getFFmpegPath(), args);
ffmpeg.stdout.on("data", (data) => { ffmpeg.stdout.on("data", (data) => {
console.log(data.toString()); console.log(data.toString());
}); });