Skip to content

Commit

Permalink
ffmpeg - Use random id for .ffmeta file to shorten name & ensure dele…
Browse files Browse the repository at this point in the history
…tion on faliure
  • Loading branch information
Inrixia committed Oct 30, 2024
1 parent 1d7661c commit e36a23c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
9 changes: 5 additions & 4 deletions src/lib/Attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import sanitize from "sanitize-filename";
import { dirname, basename, extname } from "path";

import { rename, readdir } from "fs/promises";
import { nll } from "./logging/ProgressLogger.js";

type AttachmentInfo = {
partialBytes?: number;
Expand Down Expand Up @@ -72,10 +73,10 @@ export class Attachment implements AttachmentAttributes {
});
// If the attachment existed on another path then move it.
if (attachmentInfo.filePath !== this.filePath) {
rename(this.artworkPath.replace(this.filePath, attachmentInfo.filePath), this.artworkPath).catch(() => null);
rename(this.partialPath.replace(this.filePath, attachmentInfo.filePath), this.partialPath).catch(() => null);
rename(this.muxedPath.replace(this.filePath, attachmentInfo.filePath), this.muxedPath).catch(() => null);
rename(this.nfoPath.replace(this.filePath, attachmentInfo.filePath), this.nfoPath).catch(() => null);
rename(this.artworkPath.replace(this.filePath, attachmentInfo.filePath), this.artworkPath).catch(nll);
rename(this.partialPath.replace(this.filePath, attachmentInfo.filePath), this.partialPath).catch(nll);
rename(this.muxedPath.replace(this.filePath, attachmentInfo.filePath), this.muxedPath).catch(nll);
rename(this.nfoPath.replace(this.filePath, attachmentInfo.filePath), this.nfoPath).catch(nll);
attachmentInfo.filePath = this.filePath;
}
if (attachmentInfo.videoTitle !== this.videoTitle) attachmentInfo.videoTitle = this.videoTitle;
Expand Down
79 changes: 41 additions & 38 deletions src/lib/Video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { updatePlex } from "./helpers/updatePlex.js";

import { ProgressHeadless } from "./logging/ProgressConsole.js";
import { ProgressBars } from "./logging/ProgressBars.js";
import { withContext } from "./logging/ProgressLogger.js";
import { nll, withContext } from "./logging/ProgressLogger.js";

const exec = promisify(execCallback);
const sleep = promisify(setTimeout);
Expand Down Expand Up @@ -326,7 +326,7 @@ export class Video extends Attachment {
artworkEmbed = ["-i", `${this.artworkPath}${artworkExtension}`, "-map", "2", "-disposition:0", "attached_pic"];
}

await fs.unlink(this.muxedPath).catch(() => null);
await fs.unlink(this.muxedPath).catch(nll);

const description = htmlToText(this.description);
const metadata = {
Expand All @@ -337,45 +337,48 @@ export class Video extends Attachment {
description: description,
synopsis: description,
};
const metadataFilePath = `${this.muxedPath}.ffmeta`;
const metadataFilePath = `${this.folderPath}/${Math.random()}.ffmeta`;
const metadataContent = Object.entries(metadata)
.map(([key, value]) => `${key}=${value.replaceAll(/\n/g, "\\\n")}`)
.join("\n");
await fs.writeFile(metadataFilePath, `;FFMETADATA\n${metadataContent}`);

await new Promise((resolve, reject) =>
execFile(
"./db/ffmpeg",
[
"-i",
this.partialPath,
"-i",
metadataFilePath, // Include the metadata file as an input
...artworkEmbed,
"-map",
"0",
"-map_metadata",
"1",
"-c",
"copy",
this.muxedPath,
],
(error, stdout, stderr) => {
if (error !== null) {
error.message ??= "";
error.message += stderr;
reject(error);
} else resolve(stdout);
},
),
);
await fs.unlink(metadataFilePath).catch(() => null);

await fs.unlink(this.partialPath);
// Set the files update time to when the video was released
await fs.utimes(this.muxedPath, new Date(), this.releaseDate);

(await this.attachmentInfo()).muxedBytes = await Video.pathBytes(this.muxedPath);
try {
await fs.writeFile(metadataFilePath, `;FFMETADATA\n${metadataContent}`);
await new Promise((resolve, reject) =>
execFile(
"./db/ffmpeg",
[
"-i",
this.partialPath,
"-i",
metadataFilePath, // Include the metadata file as an input
...artworkEmbed,
"-map",
"0",
"-map_metadata",
"1",
"-c",
"copy",
this.muxedPath,
],
(error, stdout, stderr) => {
if (error !== null) {
error.message ??= "";
error.message += stderr;
reject(error);
} else resolve(stdout);
},
),
);
// Remove the partial file when done
await fs.unlink(this.partialPath);
// Set the files update time to when the video was released
await fs.utimes(this.muxedPath, new Date(), this.releaseDate);

(await this.attachmentInfo()).muxedBytes = await Video.pathBytes(this.muxedPath);
} finally {
// Ensure the metadata file is removed
await fs.unlink(metadataFilePath).catch(nll);
}
}

public async postProcessingCommand(): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions src/lib/logging/ProgressLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export const withContext = (context: string) => (err: any) => {
if (err instanceof Error) throw new Error(`${context} - ${err.message}`);
throw err;
};

export const nll = () => {};

0 comments on commit e36a23c

Please sign in to comment.