Fixed album covers

This commit is contained in:
2026-08-21 12:42:06 -06:00
parent fe6dc020ca
commit aed13dc18f
5 changed files with 45 additions and 63 deletions
+19 -45
View File
@@ -25,25 +25,37 @@ auto Playlist::rip(const std::string& musicFolder) -> bool {
printf("Writting to data.json\n"); printf("Writting to data.json\n");
json data = writeAndReturnDataJson(playlistUrl); json data = writeAndReturnDataJson(playlistUrl);
// Download the content as mp3
if (!download()) { if (!download()) {
printf("ERROR: Failed to download playlsit\n"); printf("ERROR: Failed to download playlsit\n");
return false; return false;
} }
if (!writeMetadata(data)) { // Get the name of the artist and album from data.json
printf("ERROR: Failed to write metatdata for playlsit\n"); std::string artist = data["entries"][0]["uploader"];
return false; artist = std::regex_replace(artist, std::regex(" - Topic"), "");
} std::string album = data["title"];
album = std::regex_replace(album, std::regex("Album - "), "");
std::string artist; printf("Starting to write metadata for %s by %s.\n", album.c_str(), artist.c_str());
std::string album;
aristAndAlbumName(data, artist, album);
// Find the album cover from data.json
printf("Finding album cover.\n");
std::string coverUrl = data["thumbnails"][1]["url"];
printf("Writting data to files.\n");
writeMetadataToTmpFolder(artist, album, coverUrl);
printf("Finished writting metadata for %s by %s.\n", album.c_str(), artist.c_str());
// Move the files into the users Music folder
if (!moveAndSortTmpFilesToFolder(musicFolder, artist, album)) { if (!moveAndSortTmpFilesToFolder(musicFolder, artist, album)) {
printf("ERROR: Failed to move files to music folder\n"); printf("ERROR: Failed to move files to music folder\n");
return false; return false;
} }
printf("Finished moving files for %s by %s.\n", album.c_str(), artist.c_str());
return true; return true;
} }
@@ -85,44 +97,6 @@ auto Playlist::download() -> bool {
return true; return true;
} }
auto Playlist::writeMetadata(const json& data) -> bool {
std::string artist;
std::string album;
aristAndAlbumName(data, artist, album);
printf("Starting to write metadata for %s by %s.\n", album.c_str(), artist.c_str());
printf("Finding album cover.\n");
std::string coverUrl = data["thumbnails"][1]["url"];
downloadImageToTmpFolder(coverUrl);
printf("Writting data to files.\n");
size_t index = 1;
for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) {
if (std::filesystem::path(entry.path().string()).filename().string() == "cover.jpg") continue;
metadata(entry.path().string(), artist, album, index);
printf("Finished writting metadata for %s.\n", entry.path().string().c_str());
std::filesystem::remove(entry.path().string());
index++;
}
printf("Finished writting metadata for %s by %s.\n", album.c_str(), artist.c_str());
return true;
}
auto Playlist::aristAndAlbumName(const nlohmann::json& data, std::string& artist, std::string& album) -> bool {
artist = data["entries"][0]["uploader"];
artist = std::regex_replace(artist, std::regex(" - Topic"), "");
album = data["title"];
album = std::regex_replace(album, std::regex("Album - "), "");
return true;
}
auto Playlist::urlsFromPlaylist(const std::string& url) -> std::vector<std::string> { auto Playlist::urlsFromPlaylist(const std::string& url) -> std::vector<std::string> {
std::vector<std::string> urls; std::vector<std::string> urls;
-2
View File
@@ -13,7 +13,5 @@ class Playlist {
private: private:
auto download() -> bool; auto download() -> bool;
auto writeMetadata(const nlohmann::json& data) -> bool;
auto aristAndAlbumName(const nlohmann::json& data, std::string& artist, std::string& album) -> bool;
auto urlsFromPlaylist(const std::string& url) -> std::vector<std::string>; auto urlsFromPlaylist(const std::string& url) -> std::vector<std::string>;
}; };
+22 -4
View File
@@ -39,9 +39,8 @@ auto metadata(const std::string& file, const std::string& artist, const std::str
"-metadata artist=\"{}\" " "-metadata artist=\"{}\" "
"-metadata album=\"{}\" " "-metadata album=\"{}\" "
"-metadata track=\"{}\" " "-metadata track=\"{}\" "
"\"{}/{}_{}.mp3\"", "\"{}/_{}.mp3\"",
BIN_FFMPEG, file, TMP_DOWNLOAD_FOLDER, name, artist, album, index, TMP_DOWNLOAD_FOLDER, index, BIN_FFMPEG, file, TMP_DOWNLOAD_FOLDER, name, artist, album, index, TMP_DOWNLOAD_FOLDER, name)
name)
.c_str()); .c_str());
} }
@@ -54,13 +53,16 @@ auto cleanTmpDownloadFolder() -> bool {
} }
auto downloadImageToTmpFolder(std::string url) -> bool { auto downloadImageToTmpFolder(std::string url) -> bool {
printf("Downloading image with url %s\n", url.c_str());
CURL* curl = curl_easy_init(); CURL* curl = curl_easy_init();
if (!curl) { if (!curl) {
printf("ERROR: Failed to initialize curl.\n"); printf("ERROR: Failed to initialize curl.\n");
return false; return false;
} }
url = std::regex_replace(url, std::regex("https"), "http"); // TODO Breaks album covers but this is needed for windows
// url = std::regex_replace(url, std::regex("https"), "http");
auto outputPath = std::filesystem::path(TMP_DOWNLOAD_FOLDER) / "cover.jpg"; auto outputPath = std::filesystem::path(TMP_DOWNLOAD_FOLDER) / "cover.jpg";
FILE* file = std::fopen(outputPath.string().c_str(), "wb"); FILE* file = std::fopen(outputPath.string().c_str(), "wb");
if (!file) { if (!file) {
@@ -164,3 +166,19 @@ auto musicFolder() -> std::string {
return ""; return "";
} }
auto writeMetadataToTmpFolder(const std::string& artist, const std::string& album, const std::string& coverUrl)
-> void {
downloadImageToTmpFolder(coverUrl);
size_t index = 1;
for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) {
if (std::filesystem::path(entry.path().string()).filename().string() == "cover.jpg") continue;
metadata(entry.path().string(), artist, album, index);
printf("Finished writting metadata for %s.\n", entry.path().string().c_str());
std::filesystem::remove(entry.path().string());
// TODO rename without underscore
index++;
}
}
+2
View File
@@ -20,3 +20,5 @@ auto moveAndSortTmpFilesToFolder(const std::string& folder, const std::string& a
auto autoDetermineRip(const std::string& musicFolder, const std::string& url) -> bool; auto autoDetermineRip(const std::string& musicFolder, const std::string& url) -> bool;
auto musicFolder() -> std::string; auto musicFolder() -> std::string;
auto writeMetadataToTmpFolder(const std::string& artist, const std::string& album, const std::string& coverUrl) -> void;
+2 -12
View File
@@ -74,21 +74,11 @@ auto Video::writeMetadata(const nlohmann::json& data) -> bool {
printf("Starting to write metadata for %s by %s.\n", album.c_str(), artist.c_str()); printf("Starting to write metadata for %s by %s.\n", album.c_str(), artist.c_str());
printf("Finding album cover.\n"); printf("Finding thumbnail.\n");
std::string coverUrl = data["thumbnails"][1]["url"]; std::string coverUrl = data["thumbnails"][1]["url"];
downloadImageToTmpFolder(coverUrl);
printf("Writting data to files.\n"); printf("Writting data to files.\n");
writeMetadataToTmpFolder(artist, album, coverUrl);
size_t index = 1;
for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) {
if (std::filesystem::path(entry.path().string()).filename().string() == "cover.jpg") continue;
metadata(entry.path().string(), artist, album, index);
printf("Finished writting metadata for %s.\n", entry.path().string().c_str());
std::filesystem::remove(entry.path().string());
index++;
}
printf("Finished writting metadata for %s by %s.\n", album.c_str(), artist.c_str()); printf("Finished writting metadata for %s by %s.\n", album.c_str(), artist.c_str());