feat: compression 10

This commit is contained in:
jfmcarreira committed 2026-09-24 16:34:27 +01:00
1 parent e88c222c22
commit 01cdb3a0de
6 files changed
+31 -33

No files matched your search

+14
View File
@@ -176,3 +176,17 @@ Any OpenAI-compatible API can be used for parsing your search query in the "Find
## Contributing
Feel free to contribute to this project, I'm open to any suggestions and improvements. Please read the [CONTRIBUTING.md](./CONTRIBUTING.md) for more details.
## Video compression source files
Video compression reads Immich originals directly from read-only mounts instead of downloading them through the Immich API. Mount each Immich source root below `/originals` while preserving the path stored by Immich.
For example, if Immich stores an asset at `/usr/src/app/upload/library/2026/video.mov`, mount its host library as:
```yaml
volumes:
- /media/mobile:/originals/usr/src/app/upload/library:ro
- /media/shared:/originals/mnt/media/shared:ro
```
Power Tools will use `/originals${originalPath}` as the FFmpeg input path. Keep these mounts read-only; compressed outputs remain under `/app/data/compression`.
+3
View File
@@ -76,6 +76,9 @@ services:
image: ghcr.io/immich-power-tools/immich-power-tools:latest
volumes:
- immich-power-tools-data:/app/data
# Mirror Immich's media path below /originals so database originalPath values work directly.
# With the Immich mount above (${UPLOAD_LOCATION}:/data), use:
- ${UPLOAD_LOCATION}:/originals/data:ro
ports:
- "8001:3000"
env_file:
+4
View File
@@ -10,6 +10,10 @@ services:
- "3000:3000"
volumes:
- immich-power-tools-data:/app/data
# Mount Immich source roots read-only under /originals, preserving their Immich path.
# Example:
# - /media/mobile:/originals/usr/src/app/upload/library:ro
# - /media/shared:/originals/mnt/media/shared:ro
env_file:
- .env
+4
View File
@@ -8,6 +8,10 @@ services:
- "3000:3000"
volumes:
- immich-power-tools-data:/app/data
# Mount Immich source roots read-only under /originals, preserving their Immich path.
# Example:
# - /media/mobile:/originals/usr/src/app/upload/library:ro
# - /media/shared:/originals/mnt/media/shared:ro
env_file:
- .env
healthcheck:
+3 -13
View File
@@ -4,12 +4,10 @@ import { DEFAULT_FFMPEG_COMMAND, FFMPEG_COMMAND_SETTING } from "@/config/compres
import { and, eq } from "drizzle-orm";
import { mkdir, rm } from "node:fs/promises";
import path from "node:path";
import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import { createWriteStream } from "node:fs";
import { spawn } from "node:child_process";
const COMPRESSION_ROOT = "/app/data/compression";
const ORIGINALS_ROOT = "/originals";
export const getCompressionDirectory = (assetId: string) =>
path.join(COMPRESSION_ROOT, assetId);
@@ -19,8 +17,8 @@ const getSafeOriginalFileName = (assetId: string, originalFileName: string) => {
return fileName || `${assetId}.mp4`;
};
export const getCompressionInputPath = (assetId: string, originalFileName: string) =>
path.join(getCompressionDirectory(assetId), getSafeOriginalFileName(assetId, originalFileName));
export const getOriginalAssetPath = (originalPath: string) =>
path.join(ORIGINALS_ROOT, originalPath.replace(/^\/+/, ""));
export const getCompressionOutputFileName = (assetId: string, originalFileName: string) => {
const inputFileName = getSafeOriginalFileName(assetId, originalFileName);
@@ -67,14 +65,6 @@ export const getFfmpegCommand = async (ownerId: string) => {
return row?.value || DEFAULT_FFMPEG_COMMAND;
};
export const saveResponseBody = async (response: Response, filePath: string) => {
if (!response.body) throw new Error("Immich returned an empty asset body");
await pipeline(
Readable.fromWeb(response.body as any),
createWriteStream(filePath)
);
};
export const runFfmpegCommand = async (
command: string,
inputPath: string,
+3 -20
View File
@@ -1,15 +1,12 @@
import { ENV } from "@/config/environment";
import { db } from "@/config/db";
import { getCurrentUser } from "@/handlers/serverUtils/user.utils";
import { getUserHeaders } from "@/helpers/user.helper";
import {
clearCompressionDirectory,
ensureCompressionDirectory,
getCompressionInputPath,
getOriginalAssetPath,
getCompressionOutputPath,
getFfmpegCommand,
runFfmpegCommand,
saveResponseBody,
} from "@/helpers/compression.server";
import { assets } from "@/schema";
import { and, eq } from "drizzle-orm";
@@ -28,6 +25,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
id: assets.id,
type: assets.type,
originalFileName: assets.originalFileName,
originalPath: assets.originalPath,
})
.from(assets)
.where(and(eq(assets.id, assetId), eq(assets.ownerId, currentUser.id)));
@@ -36,32 +34,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(404).json({ message: "Video not found" });
}
const inputPath = getCompressionInputPath(asset.id, asset.originalFileName);
const inputPath = getOriginalAssetPath(asset.originalPath);
const outputPath = getCompressionOutputPath(asset.id, asset.originalFileName);
try {
await clearCompressionDirectory(asset.id);
await ensureCompressionDirectory(asset.id);
const downloadResponse = await fetch(`${ENV.IMMICH_URL}/api/assets/${asset.id}/original`, {
headers: {
...getUserHeaders(currentUser),
Accept: "application/octet-stream",
},
});
if (!downloadResponse.ok) {
throw new Error(`Failed to download original asset (${downloadResponse.status})`);
}
await saveResponseBody(downloadResponse, inputPath);
const originalStat = await stat(inputPath);
const command = await getFfmpegCommand(currentUser.id);
await runFfmpegCommand(command, inputPath, outputPath);
const outputStat = await stat(outputPath);
await rm(inputPath, { force: true });
return res.status(200).json({
assetId: asset.id,
originalSize: originalStat.size,
@@ -70,7 +54,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
resultUrl: `/api/assets/compression/result/${asset.id}`,
});
} catch (error: any) {
await rm(inputPath, { force: true }).catch(() => undefined);
await rm(outputPath, { force: true }).catch(() => undefined);
return res.status(500).json({ message: error?.message ?? "Compression failed" });
}