Skip to content

Commit

Permalink
Added artwork embed in ffmpeg muxing
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Sep 18, 2022
1 parent 029a651 commit e9b67a7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/Downloader.ts
Expand Up @@ -94,7 +94,7 @@ export default class Downloader {
} else if (this.mpb !== undefined) this.mpb.updateTask(formattedTitle, barUpdate);
}

private async processVideo(video: Video, retries = 0, allowRangeQuery = true): Promise<void> {
private async processVideo(video: Video, retries = 0): Promise<void> {
let formattedTitle: string;
if (args.headless === true) formattedTitle = `${video.channel.title} - ${video.title}`;
else if (video.channel.consoleColor !== undefined) {
Expand All @@ -119,7 +119,7 @@ export default class Downloader {
// If the video is already downloaded then just mux its metadata
if (!(await video.isDownloaded())) {
const startTime = Date.now();
const downloadRequests = await video.download(settings.floatplane.videoResolution as string, allowRangeQuery);
const downloadRequests = await video.download(settings.floatplane.videoResolution as string);

const totalBytes: number[] = [];
const downloadedBytes: number[] = [];
Expand Down Expand Up @@ -182,7 +182,7 @@ export default class Downloader {
// Handle errors when downloading nicely
if (retries < settings.floatplane.retries) {
this.updateBar(formattedTitle, { message: `\u001b[31m\u001b[1mERR\u001b[0m: ${info.message} - Retrying ${retries}/${settings.floatplane.retries}` }, true);
if (info.message.indexOf('Range Not Satisfiable')) await this.processVideo(video, ++retries, false);
if (info.message.indexOf('Range Not Satisfiable')) await this.processVideo(video, ++retries);
else await this.processVideo(video, ++retries);
} else
this.updateBar(
Expand Down
27 changes: 11 additions & 16 deletions src/lib/Video.ts
Expand Up @@ -70,6 +70,10 @@ export default class Video {
return `${this.folderPath}/${sanitize(this.fullPath.split('/').slice(-1)[0])}`;
}

public get artworkPath(): string {
return `${this.filePath}${settings.artworkSuffix}.png`;
}

/**
* Get the suffix for a video file if there are multiple videoAttachments for this video
*/
Expand Down Expand Up @@ -99,19 +103,18 @@ export default class Video {
return fileBytes === this.expectedSize;
};

public async download(quality: string, allowRangeQuery = true): Promise<ReturnType<typeof fApi.got.stream>[]> {
public async download(quality: string): Promise<ReturnType<typeof fApi.got.stream>[]> {
if (await this.isDownloaded()) throw new Error(`Attempting to download "${this.title}" video already downloaded!`);

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

// If downloading artwork is enabled download it
if (settings.extras.downloadArtwork && this.thumbnail !== null) {
const artworkFile = `${this.filePath}${settings.artworkSuffix}.png`;
fApi.got
.stream(this.thumbnail.path)
.pipe(createWriteStream(artworkFile))
.once('end', () => fs.utimes(artworkFile, new Date(), this.releaseDate));
.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

if (settings.extras.saveNfo) {
Expand Down Expand Up @@ -150,15 +153,7 @@ export default class Video {
await fs.utimes(`${this.filePath}.nfo`, new Date(), this.releaseDate);
}

let writeStreamOptions, requestOptions, downloadedBytes;
// Disable download resumption as floatplane has a poor history of edges failing to support it causing issues
// // Download resumption is not currently supported for multi part videos...
// if (this.videoAttachments.length === 1) {
// // Handle download resumption if video was partially downloaded
// if (allowRangeQuery && this.expectedSize !== undefined && (downloadedBytes = await this.fileBytes('partial')) !== -1) {
// [writeStreamOptions, requestOptions] = [{ start: downloadedBytes, flags: 'r+' }, { headers: { range: `bytes=${downloadedBytes}-${this.expectedSize}` } }];
// }
// }
let writeStreamOptions, requestOptions;

let downloadRequests = [];
for (const i in this.videoAttachments) {
Expand Down Expand Up @@ -209,6 +204,7 @@ export default class Video {
throw new Error(
`Cannot mux ffmpeg metadata for ${this.title} as its not downloaded. Expected: ${this.expectedSize}, Got: ${await this.fileBytes('partial')} bytes...`
);
const artworkEmbed: string[] = settings.extras.downloadArtwork && this.thumbnail !== null ? ['-i', this.artworkPath, '-map', '1', '-map', '0', '-disposition:0', 'attached_pic'] : [];
await Promise.all(
this.videoAttachments.map(
(a, i) =>
Expand All @@ -218,6 +214,7 @@ export default class Video {
[
'-i',
`${this.filePath}${this.multiPartSuffix(i)}.partial`,
...artworkEmbed,
'-metadata',
`title=${this.title}${this.multiPartSuffix(i)}`,
'-metadata',
Expand All @@ -230,9 +227,7 @@ export default class Video {
`description=${this.ffmpegDesc}`,
'-metadata',
`synopsis=${this.ffmpegDesc}`,
'-c:a',
'copy',
'-c:v',
'-c',
'copy',
`${this.filePath}${this.multiPartSuffix(i)}.mp4`,
],
Expand Down

0 comments on commit e9b67a7

Please sign in to comment.