From 1a3f0e332ee7255e9c896af0de7a8e90c3e8ee13 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 1 Apr 2023 16:31:04 -0500 Subject: [PATCH] Fix download podcast episode that is not mp3 --- server/managers/PodcastManager.js | 23 +++++-- server/objects/PodcastEpisodeDownload.js | 6 +- server/utils/ffmpegHelpers.js | 87 +++++++++++------------- 3 files changed, 61 insertions(+), 55 deletions(-) diff --git a/server/managers/PodcastManager.js b/server/managers/PodcastManager.js index 0cc53f12..e60238cc 100644 --- a/server/managers/PodcastManager.js +++ b/server/managers/PodcastManager.js @@ -4,7 +4,7 @@ const SocketAuthority = require('../SocketAuthority') const fs = require('../libs/fsExtra') const { getPodcastFeed } = require('../utils/podcastUtils') -const { removeFile } = require('../utils/fileUtils') +const { removeFile, downloadFile } = require('../utils/fileUtils') const filePerms = require('../utils/filePerms') const { levenshteinDistance } = require('../utils/index') const opmlParser = require('../utils/parsers/parseOPML') @@ -94,11 +94,22 @@ class PodcastManager { await filePerms.setDefault(this.currentDownload.libraryItem.path) } - // Downloads episode and tags it - let success = await ffmpegHelpers.downloadPodcastEpisode(this.currentDownload).catch((error) => { - Logger.error(`[PodcastManager] Podcast Episode download failed`, error) - return false - }) + + let success = false + if (this.currentDownload.urlFileExtension === 'mp3') { + // Download episode and tag it + success = await ffmpegHelpers.downloadPodcastEpisode(this.currentDownload).catch((error) => { + Logger.error(`[PodcastManager] Podcast Episode download failed`, error) + return false + }) + } else { + // Download episode only + success = await downloadFile(this.currentDownload.url, this.currentDownload.targetPath).then(() => true).catch((error) => { + Logger.error(`[PodcastManager] Podcast Episode download failed`, error) + return false + }) + } + if (success) { success = await this.scanAddPodcastEpisodeAudioFile() if (!success) { diff --git a/server/objects/PodcastEpisodeDownload.js b/server/objects/PodcastEpisodeDownload.js index 6c3be9ca..601b7e60 100644 --- a/server/objects/PodcastEpisodeDownload.js +++ b/server/objects/PodcastEpisodeDownload.js @@ -41,8 +41,12 @@ class PodcastEpisodeDownload { } } + get urlFileExtension() { + const cleanUrl = this.url.split('?')[0] // Remove query string + return Path.extname(cleanUrl).substring(1).toLowerCase() + } get fileExtension() { - const extname = Path.extname(this.url).substring(1).toLowerCase() + const extname = this.urlFileExtension if (globals.SupportedAudioTypes.includes(extname)) return extname return 'mp3' } diff --git a/server/utils/ffmpegHelpers.js b/server/utils/ffmpegHelpers.js index dc16430e..b82f9784 100644 --- a/server/utils/ffmpegHelpers.js +++ b/server/utils/ffmpegHelpers.js @@ -98,53 +98,44 @@ module.exports.downloadPodcastEpisode = (podcastEpisodeDownload) => { }) const ffmpeg = Ffmpeg(response.data) - ffmpeg.outputOptions([ - '-c copy', - '-metadata', - `album=${podcastEpisodeDownload.libraryItem?.media.metadata.title ?? ""}`, // Podcast Title - '-metadata', - `album-sort=${podcastEpisodeDownload.libraryItem?.media.metadata.title ?? ""}`, // Podcast Title - '-metadata', - `artist=${podcastEpisodeDownload.libraryItem?.media.metadata.author ?? ""}`, // Podcast Artist - '-metadata', - `artist-sort=${podcastEpisodeDownload.libraryItem?.media.metadata.author ?? ""}`, // Podcast Artist - '-metadata', - `comment=${podcastEpisodeDownload.podcastEpisode?.description ?? ""}`, // Episode Description - '-metadata', - `subtitle=${podcastEpisodeDownload.podcastEpisode?.subtitle ?? ""}`, // Episode Subtitle - '-metadata', - `disc=${podcastEpisodeDownload.podcastEpisode?.season ?? ""}`, // Episode Season - '-metadata', - `genre=${podcastEpisodeDownload.libraryItem?.media.metadata.genres.join(';') ?? ""}`, // Podcast Genres - '-metadata', - `language=${podcastEpisodeDownload.libraryItem?.media.metadata.language ?? ""}`, // Podcast Language - '-metadata', - `MVNM=${podcastEpisodeDownload.libraryItem?.media.metadata.title ?? ""}`, // Podcast Title - '-metadata', - `MVIN=${podcastEpisodeDownload.podcastEpisode?.episode ?? ""}`, // Episode Number - '-metadata', - `track=${podcastEpisodeDownload.podcastEpisode?.episode ?? ""}`, // Episode Number - '-metadata', - `series-part=${podcastEpisodeDownload.podcastEpisode?.episode ?? ""}`, // Episode Number - '-metadata', - `podcast=1`, - '-metadata', - `title=${podcastEpisodeDownload.podcastEpisode?.title ?? ""}`, // Episode Title - '-metadata', - `title-sort=${podcastEpisodeDownload.podcastEpisode?.title ?? ""}`, // Episode Title - '-metadata', - `year=${podcastEpisodeDownload.podcastEpisode?.pubYear ?? ""}`, // Episode Pub Year - '-metadata', - `date=${podcastEpisodeDownload.podcastEpisode?.pubDate ?? ""}`, // Episode PubDate - '-metadata', - `releasedate=${podcastEpisodeDownload.podcastEpisode?.pubDate ?? ""}`, // Episode PubDate - '-metadata', - `itunes-id=${podcastEpisodeDownload.libraryItem?.media.metadata.itunesId ?? ""}`, // Podcast iTunes ID - '-metadata', - `podcast-type=${podcastEpisodeDownload.libraryItem?.media.metadata.type ?? ""}`, // Podcast Type - '-metadata', - `episode-type=${podcastEpisodeDownload.podcastEpisode?.episodeType ?? ""}` // Episode Type - ]) + ffmpeg.outputOptions( + '-c', 'copy', + '-metadata', 'podcast=1' + ) + + const podcastMetadata = podcastEpisodeDownload.libraryItem.media.metadata + const podcastEpisode = podcastEpisodeDownload.podcastEpisode + + const taggings = { + 'album': podcastMetadata.title, + 'album-sort': podcastMetadata.title, + 'artist': podcastMetadata.author, + 'artist-sort': podcastMetadata.author, + 'comment': podcastEpisode.description, + 'subtitle': podcastEpisode.subtitle, + 'disc': podcastEpisode.season, + 'genre': podcastMetadata.genres.length ? podcastMetadata.genres.join(';') : null, + 'language': podcastMetadata.language, + 'MVNM': podcastMetadata.title, + 'MVIN': podcastEpisode.episode, + 'track': podcastEpisode.episode, + 'series-part': podcastEpisode.episode, + 'title': podcastEpisode.title, + 'title-sort': podcastEpisode.title, + 'year': podcastEpisode.pubYear, + 'date': podcastEpisode.pubDate, + 'releasedate': podcastEpisode.pubDate, + 'itunes-id': podcastMetadata.itunesId, + 'podcast-type': podcastMetadata.type, + 'episode-type': podcastMetadata.episodeType + } + + for (const tag in taggings) { + if (taggings[tag]) { + ffmpeg.addOption('-metadata', `${tag}=${taggings[tag]}`) + } + } + ffmpeg.addOutput(podcastEpisodeDownload.targetPath) ffmpeg.on('start', (cmd) => { @@ -160,4 +151,4 @@ module.exports.downloadPodcastEpisode = (podcastEpisodeDownload) => { }) ffmpeg.run() }) -} \ No newline at end of file +}