From e671784144174c4fceaa6df3737ba9b4de4a6c63 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 17 Jul 2021 09:43:20 +0200 Subject: Youtube: remove dependency on youtube-dl for downloads (also fixes downloads of age restricted videos) --- src/plugins/Youtube.cpp | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'src/plugins/Youtube.cpp') diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index eb95c05..2fd193f 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -266,9 +266,8 @@ R"END( // Sometimes youtube returns a redirect url (not in the header but in the body...). // TODO: Find why this happens and if there is a way bypass it. - static std::string get_playback_url_recursive(std::string playback_url, int &content_length) { + static std::string get_playback_url_recursive(std::string playback_url, int64_t &content_length) { std::vector additional_args = get_cookies(); - additional_args.push_back({ "-r", "0-4096" }); const int max_redirects = 5; for(int i = 0; i < max_redirects; ++i) { @@ -307,7 +306,7 @@ R"END( return playback_url; } - bool youtube_custom_redirect(std::string &video_url, std::string &audio_url, int &video_content_length, int &audio_content_length, std::function active_handler) { + bool youtube_custom_redirect(std::string &video_url, std::string &audio_url, int64_t &video_content_length, int64_t &audio_content_length, std::function active_handler) { // TODO: Do this without threads int num_total_tasks = 0; AsyncTask tasks[2]; @@ -2164,10 +2163,6 @@ R"END( return url.substr(index, end - index); } - static void print_chosen_format(const YoutubeVideoFormat &format) { - fprintf(stderr, "Choosing youtube video format: width: %d, height: %d, fps: %d, bitrate: %d, mime type: %s\n", format.width, format.height, format.fps, format.base.bitrate, format.base.mime_type.c_str()); - } - static const YoutubeVideoFormat* get_highest_resolution_mp4_non_av1(const std::vector &video_formats, int max_height) { for(const YoutubeVideoFormat &video_format : video_formats) { if(video_format.height <= max_height && video_format.base.mime_type.find("mp4") != std::string::npos && video_format.base.mime_type.find("av01") == std::string::npos) @@ -2184,14 +2179,14 @@ R"END( return nullptr; } - std::string YoutubeVideoPage::get_video_url(int max_height, bool &has_embedded_audio) { + std::string YoutubeVideoPage::get_video_url(int max_height, bool &has_embedded_audio, std::string &ext) { if(!livestream_url.empty()) { has_embedded_audio = true; return livestream_url; } if(video_formats.empty()) { - has_embedded_audio = true; + has_embedded_audio = false; return ""; } @@ -2208,17 +2203,31 @@ R"END( if(!chosen_video_format) chosen_video_format = &video_formats.back(); - print_chosen_format(*chosen_video_format); + fprintf(stderr, "Choosing youtube video format: width: %d, height: %d, fps: %d, bitrate: %d, mime type: %s\n", chosen_video_format->width, chosen_video_format->height, chosen_video_format->fps, chosen_video_format->base.bitrate, chosen_video_format->base.mime_type.c_str()); has_embedded_audio = chosen_video_format->has_embedded_audio; + + if(chosen_video_format->base.mime_type.find("mp4") != std::string::npos) + ext = ".mp4"; + else if(chosen_video_format->base.mime_type.find("webm") != std::string::npos) + ext = ".webm"; + return chosen_video_format->base.url; } - std::string YoutubeVideoPage::get_audio_url() { + std::string YoutubeVideoPage::get_audio_url(std::string &ext) { if(audio_formats.empty()) return ""; const YoutubeAudioFormat *chosen_audio_format = &audio_formats.front(); fprintf(stderr, "Choosing youtube audio format: bitrate: %d, mime type: %s\n", chosen_audio_format->base.bitrate, chosen_audio_format->base.mime_type.c_str()); + + if(chosen_audio_format->base.mime_type.find("mp4") != std::string::npos) + ext = ".m4a"; + else if(chosen_audio_format->base.mime_type.find("webm") != std::string::npos) + ext = ".webm"; + else if(chosen_audio_format->base.mime_type.find("opus") != std::string::npos) + ext = ".opus"; + return chosen_audio_format->base.url; } -- cgit v1.2.3