diff --git a/.gitignore b/.gitignore index fb11540..e0ae145 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,7 @@ build/ .DS_Store -.vscode \ No newline at end of file +.vscode + +audio +challenges \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 05ddb03..f1d2846 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,31 +1,57 @@ #include "stx-execpipe.h" -#include +#include #include #include +#include static const std::vector links = { - "2gQ--SCEfmI"}; + "2gQ--SCEfmI", + "9xBeLSXOvSY", + "gv7QwZqMFYc", + "tFRStkEkhbY", + "OecoHvuxkxY", + "7SVKgjhpTHY", + "KF63Z27NPTE", + "7fKPxTbFt88", + "ZccDikDUHmA", + "PjnHPuEUbjU" +}; -static const std::string YTDLP_BINARY = "/usr/bin/yt-dlp"; +static const std::string YTDLP_BINARY = "/home/simon/.local/bin/yt-dlp"; -void DownloadFile() +std::string GetAudioPath(const std::string& id) { + return "audio/" + id + ".ogg"; +} + +void DownloadFile(const std::string &id) { stx::ExecPipe ep; - std::string link = "https://music.youtube.com/watch?v=" + links.front(); + std::string link = "https://music.youtube.com/watch?v=" + id; - std::vector args = {YTDLP_BINARY, link.c_str(), "--extract-audio", "--audio-format", "vorbis", "-o", "%(id)s.%(ext)s"}; + std::vector args = {YTDLP_BINARY, link.c_str(), "--extract-audio", "--audio-format", "vorbis", "--audio-quality", "10", "-o", "audio/%(id)s.%(ext)s"}; ep.add_exec(&args); ep.run(); } -float GetDuration() +void DownloadFiles() { + for (const std::string& id : links) { + DownloadFile(id); + } +} + +/** + * std::vector args = {"/usr/bin/ffmpeg", "-hide_banner", "-loglevel", "panic", "-i", "pipe:0", "-b:a", "32k", "-map", + "a", "-c:a", "libopus", "-f", "ogg", "-y", "pipe:1"}; + */ + +float GetDuration(const std::string &id) { stx::ExecPipe ep; - std::string fileName = links.front() + ".ogg"; + std::string fileName = GetAudioPath(id); std::vector args = {"/usr/bin/ffprobe", "-v", "error", "-show_entries", "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", fileName}; @@ -37,12 +63,54 @@ float GetDuration() return std::stof(output); } +/** + * \brief Returns a random real number between min and max. + * \param min The minimum value. + * \param max The maximum value. + * \return A random real number between min and max. + */ +float GetRandomReal(float min, float max) +{ + static std::random_device randomDevice; + static std::mt19937 generator(randomDevice()); + std::uniform_real_distribution distrib(min, max); + return distrib(generator); +} + +void ExtractAudio(const std::string &id, float begin, float end, const std::string& outputName) +{ + stx::ExecPipe ep; + + std::vector args = {"/usr/bin/ffmpeg", "-y", "-loglevel", "panic", "-i", GetAudioPath(id), "-ss", std::to_string(begin), "-to", std::to_string(end), outputName}; + + ep.add_exec(&args); + ep.run(); +} + +std::string GetRandomLink() { + return links.at(rand() % links.size()); +} + +void GenRandomChallenge() { + std::string randomLink = GetRandomLink(); + float duration = GetDuration(randomLink); + + float randomSeek = GetRandomReal(duration / 4.0f, duration * 3.0f / 4.0f); + + ExtractAudio(randomLink, randomSeek, randomSeek + 0.5f, "challenges/1.ogg"); + ExtractAudio(randomLink, randomSeek, randomSeek + 1.0f, "challenges/2.ogg"); + ExtractAudio(randomLink, randomSeek, randomSeek + 1.5f, "challenges/3.ogg"); + ExtractAudio(randomLink, randomSeek, randomSeek + 2.0f, "challenges/4.ogg"); + + std::cout << "Done !\n"; +} + + int main(int argc, char **argv) { - - float duration = GetDuration(); - - std::cout << "Durée : " << duration << std::endl; + srand(time(0)); + + GenRandomChallenge(); return 0; }