Compare commits
15
Commits
176335dc07
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
43da2c7ac1 | ||
|
|
92d11b5fbb | ||
|
|
28c4318b00 | ||
|
|
ce457fbd17 | ||
|
|
aed13dc18f | ||
|
|
fe6dc020ca | ||
|
|
8001311b52 | ||
|
|
4fc7457052 | ||
|
|
96276d7639 | ||
|
|
070e583bd2 | ||
|
|
280c84ac6e | ||
|
|
98d54c58a0 | ||
|
|
ee378b19a3 | ||
|
|
a33cab8326 | ||
|
|
01b51efa63 |
@@ -1,2 +1,3 @@
|
||||
build
|
||||
.cache
|
||||
builds
|
||||
|
||||
+15
-14
@@ -7,6 +7,9 @@ project(YTMusicDL26)
|
||||
|
||||
add_executable(YTMusicDL26
|
||||
src/main.cpp
|
||||
src/playlist.cpp
|
||||
src/video.cpp
|
||||
src/utils.cpp
|
||||
)
|
||||
|
||||
target_include_directories(YTMusicDL26 PUBLIC
|
||||
@@ -16,16 +19,21 @@ target_include_directories(YTMusicDL26 PUBLIC
|
||||
|
||||
include(cmake/CPM.cmake)
|
||||
|
||||
# CPMAddPackage(
|
||||
# NAME id3v2lib
|
||||
# GIT_REPOSITORY https://github.com/larsbs/id3v2lib.git
|
||||
# GIT_TAG master
|
||||
# )
|
||||
set(CURL_USE_LIBPSL OFF CACHE BOOL "" FORCE)
|
||||
set(CURL_USE_SCHANNEL ON)
|
||||
|
||||
# target_link_libraries(YTMusicDL26 PUBLIC
|
||||
# )
|
||||
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
|
||||
|
||||
CPMAddPackage("gh:nlohmann/json#v3.12.0")
|
||||
CPMAddPackage("gh:curl/curl#curl-8_21_0")
|
||||
|
||||
target_link_libraries(YTMusicDL26 PUBLIC
|
||||
nlohmann_json::nlohmann_json
|
||||
CURL::libcurl
|
||||
)
|
||||
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/bin DESTINATION ${CMAKE_BINARY_DIR})
|
||||
file(COPY ${CMAKE_SOURCE_DIR}/config DESTINATION ${CMAKE_BINARY_DIR})
|
||||
|
||||
if (WIN32)
|
||||
target_link_options(YTMusicDL26 PRIVATE
|
||||
@@ -33,13 +41,6 @@ if (WIN32)
|
||||
-static-libstdc++
|
||||
-static
|
||||
)
|
||||
|
||||
add_custom_command(TARGET YTMusicDL26 POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
$<TARGET_RUNTIME_DLLS:YTMusicDL26>
|
||||
$<TARGET_FILE_DIR:YTMusicDL26>
|
||||
COMMAND_EXPAND_LISTS
|
||||
)
|
||||
endif ()
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,11 @@
|
||||
set(CMAKE_SYSTEM_NAME Windows)
|
||||
set(CMAKE_SYSTEM_PROCESSOR x86_64)
|
||||
|
||||
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
|
||||
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
|
||||
|
||||
set(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32)
|
||||
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
mkdir builds
|
||||
cd builds
|
||||
|
||||
# Build for linux
|
||||
mkdir linux
|
||||
cd linux
|
||||
cmake -GNinja ../..
|
||||
ninja
|
||||
cd ..
|
||||
|
||||
# Linux release
|
||||
rm -r linux-release
|
||||
mkdir linux-release
|
||||
cd linux-release
|
||||
cp ../linux/YTMusicDL26 .
|
||||
mkdir bin
|
||||
cp -r ../linux/bin/linux bin/
|
||||
cp -r ../linux/config .
|
||||
cd ..
|
||||
zip -r linux-release.zip linux-release
|
||||
|
||||
# Build for windows
|
||||
mkdir windows
|
||||
cd windows
|
||||
cmake -DCMAKE_TOOLCHAIN_FILE=~/TC-mingw.cmake -GNinja ../..
|
||||
ninja
|
||||
cd ..
|
||||
|
||||
# Windows release
|
||||
rm -r windows-release
|
||||
mkdir windows-release
|
||||
cd windows-release
|
||||
cp ../windows/YTMusicDL26.exe .
|
||||
mkdir bin
|
||||
cp -r ../windows/bin/windows bin/
|
||||
cp -r ../windows/config .
|
||||
cd ..
|
||||
zip -r windows-release.zip windows-release
|
||||
|
||||
cd ..
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#define TMP_DOWNLOAD_FOLDER "tmp/download"
|
||||
#define MAX_THREADS 4
|
||||
|
||||
#ifdef _WIN32
|
||||
#define BIN_FOLDER "bin/windows"
|
||||
#define BIN_YT_DLP "bin/windows/yt-dlp.exe"
|
||||
#define BIN_FFMPEG "bin/windows/ffmpeg.exe"
|
||||
#define BIN_DENO "bin/windows/deno.exe"
|
||||
#else
|
||||
#define BIN_FOLDER "bin/linux"
|
||||
#define BIN_YT_DLP "bin/linux/yt-dlp"
|
||||
#define BIN_FFMPEG "bin/linux/ffmpeg"
|
||||
#define BIN_DENO "bin/linux/deno"
|
||||
#endif
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
class Content {
|
||||
public:
|
||||
virtual ~Content() = default;
|
||||
virtual auto rip(const std::string& musicFolder, std::function<void(int)> status) -> bool = 0;
|
||||
virtual auto info(std::string& artist, std::string& title) -> void = 0;
|
||||
};
|
||||
+61
-173
@@ -1,190 +1,78 @@
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#define TMP_DOWNLOAD_FOLDER "tmp/download"
|
||||
#define MAX_THREADS 4
|
||||
#include "config.hpp"
|
||||
#include "content.hpp"
|
||||
#include "playlist.hpp"
|
||||
#include "utils.hpp"
|
||||
#include "video.hpp"
|
||||
|
||||
// This runs in shell, if put through a website interface it would allow for injection
|
||||
auto ytdlp(const std::string& args) -> bool {
|
||||
system(std::format("bin/linux/yt-dlp {}", args).c_str());
|
||||
return true;
|
||||
}
|
||||
auto main(int argc, char* argv[]) -> int {
|
||||
std::queue<std::string> urls;
|
||||
|
||||
auto metadata(const std::string& file, const std::string& artist, const std::string& album, size_t index) {
|
||||
std::string name = std::filesystem::path(file).filename().string();
|
||||
printf(
|
||||
"Welcome to YTMusicDL26! You can use playlists or videos from youtube or youtube music (youtube music is "
|
||||
"better).\n");
|
||||
printf("Type \"end\" when your finished.\n");
|
||||
|
||||
system(std::format("bin/linux/ffmpeg "
|
||||
"-hide_banner -loglevel error "
|
||||
"-i \"{}\" -c copy "
|
||||
"-metadata title=\"{}\" "
|
||||
"-metadata artist=\"{}\" "
|
||||
"-metadata album=\"{}\" "
|
||||
"-metadata track=\"{}\" "
|
||||
"\"{}/{}_{}.mp3\"",
|
||||
file, name, artist, album, index, TMP_DOWNLOAD_FOLDER, index, name)
|
||||
.c_str());
|
||||
}
|
||||
int index = 0;
|
||||
while (true) {
|
||||
std::string input;
|
||||
printf("%d: ", index);
|
||||
std::cin >> input;
|
||||
|
||||
auto cleanTmpDownloadFolder() -> bool {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) {
|
||||
std::filesystem::remove_all(entry.path());
|
||||
if (input == "end") {
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto urlsFromPlaylist(const std::string& url) -> std::vector<std::string> {
|
||||
std::vector<std::string> urls;
|
||||
|
||||
std::filesystem::remove("urls.txt");
|
||||
ytdlp(
|
||||
std::format("--js-runtimes \"deno:bin/linux/deno\" "
|
||||
"--quiet "
|
||||
"--flat-playlist "
|
||||
"--print-to-file \"%(webpage_url)s\" "
|
||||
"urls.txt \"{}\"",
|
||||
url));
|
||||
|
||||
std::ifstream f("urls.txt");
|
||||
if (!f.is_open()) {
|
||||
printf("ERROR: Couldn't open urls.txt\n");
|
||||
return urls;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(f, line)) {
|
||||
urls.push_back(line);
|
||||
}
|
||||
|
||||
if (urls.empty()) printf("ERROR: Couldn't find any urls for playlist %s\n", url.c_str());
|
||||
return urls;
|
||||
}
|
||||
|
||||
auto dataFromYtdlpPlaylist(const std::string& playlistUrl, const std::string& data) -> std::string {
|
||||
ytdlp(
|
||||
std::format("--quiet "
|
||||
"--no-warnings "
|
||||
"--flat-playlist "
|
||||
"--playlist-items 1 "
|
||||
"--print-to-file \"%({})s\" data.txt \"{}\"",
|
||||
data, playlistUrl));
|
||||
|
||||
std::ifstream f("data.txt");
|
||||
if (!f.is_open()) {
|
||||
printf("ERROR: Couldn't open data.txt\n");
|
||||
}
|
||||
|
||||
std::string s;
|
||||
std::string line;
|
||||
while (std::getline(f, line)) {
|
||||
s = line;
|
||||
}
|
||||
|
||||
return s;
|
||||
};
|
||||
|
||||
auto playlistTitle(const std::string& playlistUrl) {
|
||||
std::string s = dataFromYtdlpPlaylist(playlistUrl, "playlist_title");
|
||||
s = std::regex_replace(s, std::regex("Album - "), "");
|
||||
return s;
|
||||
}
|
||||
|
||||
auto channelName(const std::string& playlistUrl) {
|
||||
std::string s = dataFromYtdlpPlaylist(playlistUrl, "channel");
|
||||
s = std::regex_replace(s, std::regex(" - Topic"), "");
|
||||
return s;
|
||||
}
|
||||
|
||||
auto downloadVideo(const std::string& url) -> bool {
|
||||
if (url == "") {
|
||||
printf("ERROR: Cannot download video with empty url\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
ytdlp(
|
||||
std::format("--js-runtimes \"deno:bin/linux/deno\" "
|
||||
"--quiet "
|
||||
"-f \"bestaudio/best\" "
|
||||
"-x --audio-format mp3 "
|
||||
"-o \"{}/%(title)s.%(ext)s\" "
|
||||
"{}",
|
||||
TMP_DOWNLOAD_FOLDER, url));
|
||||
|
||||
printf("Finished downloading video %s.\n", url.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
auto downloadPlaylist(std::string playlistUrl) -> bool {
|
||||
std::vector<std::string> videoUrls = urlsFromPlaylist(playlistUrl);
|
||||
|
||||
if (videoUrls.empty()) {
|
||||
printf("ERROR: Tried to download empty playlist, %s\n", playlistUrl.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!cleanTmpDownloadFolder()) {
|
||||
printf("ERROR: Playlist download couldn't clean tmp download folder\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
playlistUrl = std::regex_replace(playlistUrl, std::regex("music.youtube"), "www.youtube");
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (size_t i = 0; i < videoUrls.size(); i += MAX_THREADS) {
|
||||
std::vector<std::future<void>> futures;
|
||||
|
||||
size_t end = std::min(i + MAX_THREADS, videoUrls.size());
|
||||
|
||||
for (size_t j = i; j < end; ++j) {
|
||||
futures.push_back(std::async(std::launch::async, [&videoUrls, j]() { downloadVideo(videoUrls[j]); }));
|
||||
}
|
||||
|
||||
for (auto& future : futures) {
|
||||
future.get();
|
||||
count++;
|
||||
printf("Finished [%d/%d] in playlist %s\n", count, (int)videoUrls.size(), playlistUrl.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
printf("Finished downloading playlist.\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto writeAlbumMetadataToTmpFolder(const std::string& playlistUrl) -> bool {
|
||||
std::string artist = channelName(playlistUrl);
|
||||
std::string album = playlistTitle(playlistUrl);
|
||||
printf("Starting to write metadata for %s by %s.\n", album.c_str(), artist.c_str());
|
||||
|
||||
size_t index = 1;
|
||||
for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) {
|
||||
metadata(entry.path(), artist, album, index);
|
||||
printf("Finished writting metadata for %s.\n", entry.path().c_str());
|
||||
std::filesystem::remove(entry.path());
|
||||
urls.push(input);
|
||||
index++;
|
||||
}
|
||||
|
||||
printf("Finished writing metadata for %s by %s.\n", album.c_str(), artist.c_str());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto main(int argc, char* argv[]) -> int {
|
||||
auto musicDir = "/home/ethanscharlie/Downloads/tmp";
|
||||
std::string url = "https://music.youtube.com/playlist?list=OLAK5uy_kwG9Ln_fS_R-Oa8F-JIgs8PPBoRgDe8ok";
|
||||
|
||||
std::string musicDir = musicFolder();
|
||||
std::filesystem::create_directories(TMP_DOWNLOAD_FOLDER);
|
||||
|
||||
url = std::regex_replace(url, std::regex("music.youtube"), "www.youtube");
|
||||
downloadPlaylist(url);
|
||||
writeAlbumMetadataToTmpFolder(url);
|
||||
printf("\n");
|
||||
|
||||
while (!urls.empty()) {
|
||||
const std::string& currentUrl = urls.front();
|
||||
|
||||
Content* content = nullptr;
|
||||
if (std::regex_search(currentUrl, std::regex("youtube.com/playlist"))) {
|
||||
content = new Playlist(currentUrl);
|
||||
} else if (std::regex_search(currentUrl, std::regex("youtube.com/watch"))) {
|
||||
content = new Video(currentUrl);
|
||||
}
|
||||
|
||||
if (content == nullptr) {
|
||||
printf("ERROR: Failed to create content object\n");
|
||||
urls.pop();
|
||||
delete content;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!content->rip(musicDir, [&content, &urls](int percent) {
|
||||
if (percent == 0) {
|
||||
printf("\r\033[KGathering info... (this may take a while)");
|
||||
fflush(stdout);
|
||||
} else {
|
||||
std::string artist;
|
||||
std::string album;
|
||||
content->info(artist, album);
|
||||
printf("\r\033[K%s by %s at %d%%", album.c_str(), artist.c_str(), percent);
|
||||
fflush(stdout);
|
||||
}
|
||||
})) {
|
||||
printf("Failed to rip content\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
urls.pop();
|
||||
delete content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
#include "playlist.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;
|
||||
|
||||
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, std::function<void(int)> status) -> bool {
|
||||
status(0);
|
||||
|
||||
data = writeAndReturnDataJson(playlistUrl);
|
||||
|
||||
// Get the name of the artist and album from data.json
|
||||
artist = data["entries"][0]["uploader"];
|
||||
artist = std::regex_replace(artist, std::regex(" - Topic"), "");
|
||||
album = data["title"];
|
||||
album = std::regex_replace(album, std::regex("Album - "), "");
|
||||
|
||||
status(30);
|
||||
|
||||
// Download the content as mp3
|
||||
if (!download(status)) {
|
||||
printf("ERROR: Failed to download playlsit\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
status(80);
|
||||
|
||||
std::string coverUrl = data["thumbnails"][1]["url"];
|
||||
writeMetadataToTmpFolder(artist, album, coverUrl);
|
||||
|
||||
status(95);
|
||||
|
||||
// Move the files into the users Music folder
|
||||
if (!moveAndSortTmpFilesToFolder(musicFolder, artist, album)) {
|
||||
printf("ERROR: Failed to move files to music folder\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
status(100);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto Playlist::info(std::string& artist, std::string& title) -> void {
|
||||
artist = this->artist != "" ? this->artist : "Finding...";
|
||||
title = this->album != "" ? this->album : "Finding...";
|
||||
}
|
||||
|
||||
auto Playlist::download(std::function<void(int)> status) -> bool {
|
||||
std::vector<std::string> videoUrls = urlsFromPlaylist(playlistUrl);
|
||||
|
||||
if (videoUrls.empty()) {
|
||||
printf("ERROR: Tried to download empty playlist, %s\n", playlistUrl.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!cleanTmpDownloadFolder()) {
|
||||
printf("ERROR: Playlist download couldn't clean tmp download folder\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!forEachConcurrent(videoUrls, status, [this](const std::string& url) { return downloadVideo(url); })) {
|
||||
printf("ERROR: Failed to download playlist currently\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto Playlist::forEachConcurrent(const std::vector<std::string>& urls, std::function<void(int)> status,
|
||||
std::function<bool(const std::string&)> onStart) -> bool {
|
||||
int percent = 30;
|
||||
|
||||
for (size_t i = 0; i < urls.size(); i += MAX_THREADS) {
|
||||
std::vector<std::future<bool>> futures;
|
||||
|
||||
size_t end = std::min(i + MAX_THREADS, urls.size());
|
||||
|
||||
for (size_t j = i; j < end; ++j) {
|
||||
std::string url = urls[j];
|
||||
futures.push_back(std::async(std::launch::async, [&onStart, url]() { return onStart(url); }));
|
||||
}
|
||||
|
||||
for (std::future<bool>& future : futures) {
|
||||
if (!future.get()) {
|
||||
printf("ERROR: Failed to download video from playlist concurrently\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
percent += 50 / urls.size();
|
||||
status(percent);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto Playlist::urlsFromPlaylist(const std::string& url) -> std::vector<std::string> {
|
||||
std::vector<std::string> urls;
|
||||
|
||||
std::string urlsFileLocation = "tmp/urls.txt";
|
||||
|
||||
std::filesystem::remove(urlsFileLocation);
|
||||
ytdlp(
|
||||
std::format("--js-runtimes \"deno:{}\" "
|
||||
"--quiet "
|
||||
"--flat-playlist "
|
||||
"--print-to-file \"%(webpage_url)s\" "
|
||||
"{} \"{}\"",
|
||||
BIN_DENO, urlsFileLocation, url));
|
||||
|
||||
std::ifstream f(urlsFileLocation);
|
||||
if (!f.is_open()) {
|
||||
printf("ERROR: Couldn't open urls.txt\n");
|
||||
return urls;
|
||||
}
|
||||
|
||||
std::string line;
|
||||
while (std::getline(f, line)) {
|
||||
urls.push_back(line);
|
||||
}
|
||||
|
||||
if (urls.empty()) printf("ERROR: Couldn't find any urls for playlist %s\n", url.c_str());
|
||||
return urls;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "content.hpp"
|
||||
|
||||
class Playlist : public Content {
|
||||
std::string playlistUrl;
|
||||
|
||||
std::string artist = "";
|
||||
std::string album = "";
|
||||
nlohmann::json data;
|
||||
|
||||
public:
|
||||
Playlist(const std::string& playlistUrl);
|
||||
auto rip(const std::string& musicFolder, std::function<void(int)> status) -> bool;
|
||||
auto info(std::string& artist, std::string& title) -> void;
|
||||
|
||||
private:
|
||||
auto download(std::function<void(int)> status) -> bool;
|
||||
auto forEachConcurrent(const std::vector<std::string>& urls, std::function<void(int)> status,
|
||||
std::function<bool(const std::string&)> onStart) -> bool;
|
||||
auto urlsFromPlaylist(const std::string& url) -> std::vector<std::string>;
|
||||
};
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
#include <curl/curl.h>
|
||||
#include <curl/easy.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
#include "config.hpp"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
// This runs in shell, if put through a website interface it would allow for injection
|
||||
auto ytdlp(const std::string& args) -> bool {
|
||||
system(std::format("{} {}", BIN_YT_DLP, args).c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
auto metadata(const std::string& file, const std::string& artist, const std::string& album, size_t index) -> void {
|
||||
std::string name = std::filesystem::path(file).filename().string();
|
||||
name = std::regex_replace(name, std::regex(".mp3"), "");
|
||||
name.erase(0, 1); // Remove leading underscore
|
||||
|
||||
system(std::format("{} "
|
||||
"-hide_banner -loglevel error "
|
||||
"-i \"{}\" "
|
||||
"-i \"{}/cover.jpg\" "
|
||||
"-map 0:0 "
|
||||
"-map 1:0 "
|
||||
"-c copy "
|
||||
"-id3v2_version 3 -metadata:s:v title=\"Album cover\" -metadata:s:v comment=\"Cover (front)\" "
|
||||
"-metadata title=\"{}\" "
|
||||
"-metadata artist=\"{}\" "
|
||||
"-metadata album=\"{}\" "
|
||||
"-metadata track=\"{}\" "
|
||||
"\"{}/{}.mp3\"",
|
||||
BIN_FFMPEG, file, TMP_DOWNLOAD_FOLDER, name, artist, album, index, TMP_DOWNLOAD_FOLDER, name)
|
||||
.c_str());
|
||||
}
|
||||
|
||||
auto cleanTmpDownloadFolder() -> bool {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) {
|
||||
std::filesystem::remove_all(entry.path().string());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto downloadImageToTmpFolder(std::string url) -> bool {
|
||||
CURL* curl = curl_easy_init();
|
||||
if (!curl) {
|
||||
printf("ERROR: Failed to initialize curl.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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";
|
||||
FILE* file = std::fopen(outputPath.string().c_str(), "wb");
|
||||
if (!file) {
|
||||
curl_easy_cleanup(curl);
|
||||
printf("ERROR: Failed to create cover file.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, nullptr);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
|
||||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
|
||||
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
|
||||
std::fclose(file);
|
||||
curl_easy_cleanup(curl);
|
||||
|
||||
if (res != CURLE_OK) {
|
||||
printf("ERROR: %s\n", curl_easy_strerror(res));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto downloadVideo(const std::string& url) -> bool {
|
||||
if (url == "") {
|
||||
printf("ERROR: Cannot download video with empty url\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
ytdlp(
|
||||
std::format("--js-runtimes \"deno:{}\" "
|
||||
"--quiet "
|
||||
"-f \"bestaudio/best\" "
|
||||
"-x --audio-format mp3 "
|
||||
"-o \"{}/_%(title)s.%(ext)s\" "
|
||||
"{}",
|
||||
BIN_DENO, TMP_DOWNLOAD_FOLDER, url));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto writeAndReturnDataJson(const std::string& url) -> json {
|
||||
std::filesystem::remove("tmp/data.json");
|
||||
ytdlp(
|
||||
std::format("--js-runtimes \"deno:{}\" "
|
||||
"--dump-single-json "
|
||||
"\"{}\" "
|
||||
"> tmp/data.json",
|
||||
BIN_DENO, url));
|
||||
|
||||
std::ifstream f("tmp/data.json");
|
||||
if (!f) {
|
||||
printf("ERROR: Couldn't open data.json\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return json::parse(f);
|
||||
}
|
||||
|
||||
auto moveAndSortTmpFilesToFolder(const std::string& folder, const std::string& artist, const std::string& album)
|
||||
-> bool {
|
||||
std::filesystem::path folderPath(folder);
|
||||
std::filesystem::path path = folderPath / artist / album;
|
||||
std::filesystem::create_directories(path);
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(path)) {
|
||||
std::filesystem::remove_all(entry.path().string());
|
||||
}
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(TMP_DOWNLOAD_FOLDER)) {
|
||||
std::filesystem::rename(entry, path / entry.path().filename());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auto musicFolder() -> std::string {
|
||||
#ifdef _WIN32
|
||||
const char* userProfile = std::getenv("USERPROFILE");
|
||||
if (userProfile) return std::format("{}/Music", userProfile);
|
||||
#else
|
||||
const char* home = std::getenv("HOME");
|
||||
if (home) return std::filesystem::path(home) / "Music";
|
||||
#endif
|
||||
|
||||
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);
|
||||
std::filesystem::remove(entry.path().string());
|
||||
index++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <nlohmann/json_fwd.hpp>
|
||||
#include <string>
|
||||
|
||||
auto ytdlp(const std::string& args) -> bool;
|
||||
|
||||
auto metadata(const std::string& file, const std::string& artist, const std::string& album, size_t index) -> void;
|
||||
|
||||
auto cleanTmpDownloadFolder() -> bool;
|
||||
|
||||
auto downloadImageToTmpFolder(std::string url) -> bool;
|
||||
|
||||
auto downloadVideo(const std::string& url) -> bool;
|
||||
|
||||
auto writeAndReturnDataJson(const std::string& url) -> nlohmann::json;
|
||||
|
||||
auto moveAndSortTmpFilesToFolder(const std::string& folder, const std::string& artist, const std::string& album)
|
||||
-> bool;
|
||||
|
||||
auto musicFolder() -> std::string;
|
||||
|
||||
auto writeMetadataToTmpFolder(const std::string& artist, const std::string& album, const std::string& coverUrl) -> void;
|
||||
@@ -0,0 +1,70 @@
|
||||
#include "video.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
|
||||
#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, std::function<void(int)> status) -> bool {
|
||||
status(0);
|
||||
|
||||
data = writeAndReturnDataJson(videoUrl);
|
||||
|
||||
artist = data["uploader"];
|
||||
artist = std::regex_replace(artist, std::regex(" - Topic"), "");
|
||||
album = data["title"];
|
||||
album = std::regex_replace(album, std::regex("Album - "), "");
|
||||
|
||||
status(50);
|
||||
|
||||
if (!download()) {
|
||||
printf("ERROR: Failed to download playlsit\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
status(80);
|
||||
|
||||
std::string coverUrl = data["thumbnails"][1]["url"];
|
||||
writeMetadataToTmpFolder(artist, album, coverUrl);
|
||||
|
||||
status(95);
|
||||
|
||||
if (!moveAndSortTmpFilesToFolder(musicFolder, artist, album)) {
|
||||
printf("ERROR: Failed to move files to music folder\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
status(100);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto Video::info(std::string& artist, std::string& title) -> void {
|
||||
artist = this->artist != "" ? this->artist : "Finding...";
|
||||
title = this->album != "" ? this->album : "Finding...";
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "content.hpp"
|
||||
|
||||
class Video : public Content {
|
||||
std::string videoUrl;
|
||||
|
||||
std::string artist = "";
|
||||
std::string album = "";
|
||||
nlohmann::json data;
|
||||
|
||||
public:
|
||||
Video(const std::string& videoUrl);
|
||||
auto rip(const std::string& musicFolder, std::function<void(int)> status) -> bool;
|
||||
auto info(std::string& artist, std::string& title) -> void;
|
||||
|
||||
private:
|
||||
auto download() -> bool;
|
||||
};
|
||||
Reference in New Issue
Block a user