Can download videos as singles

This commit is contained in:
2026-08-20 11:13:34 -06:00
parent ee378b19a3
commit 98d54c58a0
5 changed files with 136 additions and 4 deletions
+1
View File
@@ -8,6 +8,7 @@ project(YTMusicDL26)
add_executable(YTMusicDL26 add_executable(YTMusicDL26
src/main.cpp src/main.cpp
src/playlist.cpp src/playlist.cpp
src/video.cpp
src/utils.cpp src/utils.cpp
) )
+9 -3
View File
@@ -9,14 +9,20 @@
#include "config.hpp" #include "config.hpp"
#include "playlist.hpp" #include "playlist.hpp"
#include "utils.hpp" #include "utils.hpp"
#include "video.hpp"
auto main(int argc, char* argv[]) -> int { auto main(int argc, char* argv[]) -> int {
auto musicDir = "/home/ethanscharlie/Downloads/tmp"; auto musicDir = "/home/ethanscharlie/Downloads/tmp";
std::string url = "https://music.youtube.com/playlist?list=OLAK5uy_kwG9Ln_fS_R-Oa8F-JIgs8PPBoRgDe8ok"; std::string url = "https://music.youtube.com/playlist?list=OLAK5uy_kwG9Ln_fS_R-Oa8F-JIgs8PPBoRgDe8ok";
std::string videoUrl = "https://www.youtube.com/watch?v=WpmOb-Xanlg";
std::filesystem::create_directories(TMP_DOWNLOAD_FOLDER); std::filesystem::create_directories(TMP_DOWNLOAD_FOLDER);
url = std::regex_replace(url, std::regex("music.youtube"), "www.youtube"); // url = std::regex_replace(url, std::regex("music.youtube"), "www.youtube");
auto playlist = Playlist(url); // auto playlist = Playlist(url);
playlist.rip(musicDir); // playlist.rip(musicDir);
videoUrl = std::regex_replace(videoUrl, std::regex("music.youtube"), "www.youtube");
auto video = Video(videoUrl);
video.rip(musicDir);
} }
+3 -1
View File
@@ -17,7 +17,9 @@
using json = nlohmann::json; using json = nlohmann::json;
Playlist::Playlist(const std::string& playlistUrl) : playlistUrl(playlistUrl) {} Playlist::Playlist(const std::string& playlistUrl) {
this->playlistUrl = std::regex_replace(playlistUrl, std::regex("music.youtube"), "www.youtube");
}
auto Playlist::rip(const std::string& musicFolder) -> bool { auto Playlist::rip(const std::string& musicFolder) -> bool {
printf("Writting to data.json\n"); printf("Writting to data.json\n");
+106
View File
@@ -0,0 +1,106 @@
#include "video.hpp"
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <format>
#include <fstream>
#include <future>
#include <nlohmann/json.hpp>
#include <regex>
#include <string>
#include <vector>
#include "config.hpp"
#include "utils.hpp"
using json = nlohmann::json;
Video::Video(const std::string& videoUrl) {
this->videoUrl = std::regex_replace(videoUrl, std::regex("music.youtube"), "www.youtube");
}
auto Video::rip(const std::string& musicFolder) -> bool {
printf("Writting to data.json\n");
json data = writeAndReturnDataJson(videoUrl);
if (!download()) {
printf("ERROR: Failed to download playlsit\n");
return false;
}
if (!writeMetadata(data)) {
printf("ERROR: Failed to write metatdata for playlsit\n");
return false;
}
std::string artist;
std::string album;
aristAndAlbumName(data, artist, album);
if (!moveAndSortTmpFilesToFolder(musicFolder, artist, album)) {
printf("ERROR: Failed to move files to music folder\n");
return false;
}
return true;
}
auto Video::download() -> bool {
if (videoUrl == "") {
printf("ERROR: Tried to download empty video, %s\n", videoUrl.c_str());
return false;
}
if (!cleanTmpDownloadFolder()) {
printf("ERROR: Video download couldn't clean tmp download folder\n");
return false;
}
if (!downloadVideo(videoUrl)) {
printf("ERROR: Failed to download video\n");
return false;
}
printf("Finished downloading video %s\n", videoUrl.c_str());
return true;
}
auto Video::writeMetadata(const nlohmann::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()).filename().string() == "cover.jpg") continue;
metadata(entry.path(), artist, album, index);
printf("Finished writting metadata for %s.\n", entry.path().c_str());
std::filesystem::remove(entry.path());
index++;
}
printf("Finished writting metadata for %s by %s.\n", album.c_str(), artist.c_str());
return true;
}
auto Video::aristAndAlbumName(const nlohmann::json& data, std::string& artist, std::string& album) -> bool {
artist = data["uploader"];
artist = std::regex_replace(artist, std::regex(" - Topic"), "");
album = data["title"];
album = std::regex_replace(album, std::regex("Album - "), "");
return true;
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <nlohmann/json_fwd.hpp>
#include <string>
class Video {
std::string videoUrl;
public:
Video(const std::string& videoUrl);
auto rip(const std::string& musicFolder) -> bool;
private:
auto download() -> bool;
auto writeMetadata(const nlohmann::json& data) -> bool;
auto aristAndAlbumName(const nlohmann::json& data, std::string& artist, std::string& album) -> bool;
};