Skip to content

Commit

Permalink
Fixed video artwork using the wrong file extension for #174
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed May 2, 2023
1 parent bb8c97f commit 913d9b3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"floatplane": "^4.5.0",
"html-to-text": "^9.0.5",
"json5": "^2.2.3",
"mime-types": "^2.1.35",
"multi-progress-bars": "^5.0.3",
"process.argv": "^0.6.1",
"prompts": "^2.4.2",
Expand All @@ -35,6 +36,7 @@
},
"devDependencies": {
"@types/html-to-text": "^9.0.0",
"@types/mime-types": "^2.1.1",
"@types/multi-progress": "^2.0.3",
"@types/prompts": "^2.4.4",
"@types/semver": "^7.3.13",
Expand All @@ -47,4 +49,4 @@
"pkg": "^5.8.1",
"typescript": "^5.0.4"
}
}
}
46 changes: 36 additions & 10 deletions src/lib/Video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import fs from "fs/promises";
import { constants } from "fs";
import { settings, fApi } from "./helpers.js";

import { extension } from "mime-types";
import { dirname, basename, extname } from "path";

const exec = promisify(execCallback);

import { htmlToText } from "html-to-text";
Expand Down Expand Up @@ -80,7 +83,7 @@ export class Video {

this.folderPath = this.filePath.substring(0, this.filePath.lastIndexOf("/"));

this.artworkPath = `${this.filePath}${settings.artworkSuffix}.png`;
this.artworkPath = `${this.filePath}${settings.artworkSuffix}`;
this.nfoPath = `${this.filePath}.nfo`;
this.partialPath = `${this.filePath}.partial`;
this.muxedPath = `${this.filePath}.mp4`;
Expand All @@ -90,6 +93,16 @@ export class Video {
return Object.values(Video.Attachments).filter(filter);
}

private static async GetFileExtension(filePath: string) {
const fileDir = dirname(filePath);
const fileName = basename(filePath);

const filesInDir = await fs.readdir(fileDir);
const matchingFile = filesInDir.find((file) => file.startsWith(fileName));
if (matchingFile) return extname(matchingFile);
return undefined;
}

public static FilePathOptions = ["%channelTitle%", "%year%", "%month%", "%day%", "%hour%", "%minute%", "%second%", "%videoTitle%"] as const;
private formatString(string: string): string {
const formatLookup: Record<ValueOfA<typeof Video.FilePathOptions>, string> = {
Expand Down Expand Up @@ -197,16 +210,25 @@ export class Video {

public async downloadArtwork() {
if (!this.thumbnail) return;
if (await fileExists(this.artworkPath)) return;
// If the file already exists
if (await Video.GetFileExtension(this.artworkPath)) return;

// Make sure the folder for the video exists
await fs.mkdir(this.folderPath, { recursive: true });

fApi.got
.stream(this.thumbnail.path)
.pipe(createWriteStream(this.artworkPath))
.once("end", () => fs.utimes(this.artworkPath, new Date(), this.releaseDate));
// Save the thumbnail with the same name as the video so plex will use it
// Fetch the thumbnail and get its content type
const response = await fApi.got(this.thumbnail.path, { responseType: "buffer" });
const contentType = response.headers["content-type"];

// Map the content type to a file extension
const fileExtension = contentType ? extension(contentType) : "png"; // Default to jpg if no extension found

// Update the artworkPath with the correct file extension
const artworkPathWithExtension = `${this.artworkPath}.${fileExtension}`;

// Save the thumbnail with the correct file extension
await fs.writeFile(artworkPathWithExtension, response.body);
await fs.utimes(artworkPathWithExtension, new Date(), this.releaseDate);
}

// The number of available slots for making delivery requests,
Expand Down Expand Up @@ -286,9 +308,13 @@ export class Video {
}

public async muxffmpegMetadata(): Promise<void> {
if ((await this.getState()) !== VideoState.Partial) throw new Error(`Cannot mux ffmpeg metadata for ${this.title} as its not downloaded.`);
const artworkEmbed: string[] =
settings.extras.downloadArtwork && this.thumbnail !== null ? ["-i", this.artworkPath, "-map", "1", "-map", "0", "-disposition:0", "attached_pic"] : [];
if ((await this.getState()) !== VideoState.Partial) throw new Error(`Cannot mux ffmpeg metadata! Video not downloaded.`);

let artworkEmbed: string[] = [];
const artworkExtension = await Video.GetFileExtension(this.artworkPath);
if (settings.extras.downloadArtwork && this.thumbnail !== null && artworkExtension) {
artworkEmbed = ["-i", `${this.artworkPath}.${artworkExtension}`, "-map", "1", "-map", "0", "-disposition:0", "attached_pic"];
}

await fs.unlink(this.muxedPath).catch(() => null);
await new Promise((resolve, reject) =>
Expand Down

0 comments on commit 913d9b3

Please sign in to comment.