From 7e8a2f23a40e6374ddfb551920257846021e86fa Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 5 Aug 2019 00:37:57 +0200 Subject: Skip youtube ads in search --- include/VideoPlayer.hpp | 2 +- src/QuickMedia.cpp | 2 +- src/VideoPlayer.cpp | 35 ++++++++++++++++++++++++++--------- src/Youtube.cpp | 12 +++++++++--- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/include/VideoPlayer.hpp b/include/VideoPlayer.hpp index 5fd5e60..c52c1e5 100644 --- a/include/VideoPlayer.hpp +++ b/include/VideoPlayer.hpp @@ -25,7 +25,7 @@ namespace QuickMedia { class VideoPlayer { public: // Throws VideoInitializationException on error - VideoPlayer(unsigned int width, unsigned int height, const char *file, bool loop = false); + VideoPlayer(unsigned int width, unsigned int height, sf::WindowHandle window_handle, const char *file, bool loop = false); ~VideoPlayer(); void setPosition(float x, float y); diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 70f520a..c7d89e2 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -380,7 +380,7 @@ namespace QuickMedia { std::unique_ptr video_player; try { printf("Play video: %s\n", video_url.c_str()); - video_player = std::make_unique(window_size.x, window_size.y, video_url.c_str()); + video_player = std::make_unique(window_size.x, window_size.y, window.getSystemHandle(), video_url.c_str()); } catch(VideoInitializationException &e) { fprintf(stderr, "Failed to create video player!. TODO: Show this to the user"); } diff --git a/src/VideoPlayer.cpp b/src/VideoPlayer.cpp index e3dab43..ee4c715 100644 --- a/src/VideoPlayer.cpp +++ b/src/VideoPlayer.cpp @@ -14,8 +14,22 @@ namespace QuickMedia { VideoPlayer *video_player = (VideoPlayer*)rawVideo; ++video_player->redrawCounter; } + + static void check_error(int status) { + if(status < 0) + fprintf(stderr, "mpv api error: %s\n", mpv_error_string(status)); + } + + static void mpv_set_option_bool(mpv_handle *mpv, const char *option, bool value) { + int int_value = value; + check_error(mpv_set_option(mpv, option, MPV_FORMAT_FLAG, &int_value)); + } + + static void mpv_set_option_int64(mpv_handle *mpv, const char *option, int64_t value) { + check_error(mpv_set_option(mpv, option, MPV_FORMAT_INT64, &value)); + } - VideoPlayer::VideoPlayer(unsigned int width, unsigned int height, const char *file, bool loop) : + VideoPlayer::VideoPlayer(unsigned int width, unsigned int height, sf::WindowHandle window_handle, const char *file, bool loop) : redrawCounter(0), onPlaybackEndedCallback(nullptr), mpv(nullptr), @@ -33,20 +47,23 @@ namespace QuickMedia { if(!mpv) throw VideoInitializationException("Failed to create mpv handle"); - //mpv_set_option_string(mpv, "input-default-bindings", "yes"); - //mpv_set_option_string(mpv, "input-vo-keyboard", "yes"); - mpv_set_option_string(mpv, "cache-secs", "120"); - mpv_set_option_string(mpv, "demuxer-max-bytes", "20M"); - mpv_set_option_string(mpv, "demuxer-max-back-bytes", "10M"); + //check_error(mpv_set_option_string(mpv, "input-default-bindings", "yes")); + //check_error(mpv_set_option_string(mpv, "input-vo-keyboard", "yes")); + check_error(mpv_set_option_string(mpv, "cache-secs", "120")); + check_error(mpv_set_option_string(mpv, "demuxer-max-bytes", "20M")); + check_error(mpv_set_option_string(mpv, "demuxer-max-back-bytes", "10M")); + + //mpv_set_option_bool(mpv, "osc", true); + //mpv_set_option_int64(mpv, "wid", window_handle); if(mpv_initialize(mpv) < 0) throw VideoInitializationException("Failed to initialize mpv"); - mpv_set_option_string(mpv, "vo", "opengl-cb"); - mpv_set_option_string(mpv, "hwdec", "auto"); + check_error(mpv_set_option_string(mpv, "vo", "opengl-cb")); + check_error(mpv_set_option_string(mpv, "hwdec", "auto")); if(loop) - mpv_set_option_string(mpv, "loop", "inf"); + check_error(mpv_set_option_string(mpv, "loop", "inf")); mpvGl = (mpv_opengl_cb_context*)mpv_get_sub_api(mpv, MPV_SUB_API_OPENGL_CB); if(!mpvGl) diff --git a/src/Youtube.cpp b/src/Youtube.cpp index 4647887..780244c 100644 --- a/src/Youtube.cpp +++ b/src/Youtube.cpp @@ -1,8 +1,13 @@ #include "../plugins/Youtube.hpp" #include #include +#include namespace QuickMedia { + static bool begins_with(const char *str, const char *begin_with) { + return strncmp(str, begin_with, strlen(begin_with)) == 0; + } + SearchResult Youtube::search(const std::string &text, std::vector> &result_items) { std::string url = "https://youtube.com/results?search_query="; url += url_param_encode(text); @@ -21,8 +26,9 @@ namespace QuickMedia { auto *result_items = (std::vector>*)userdata; const char *href = quickmedia_html_node_get_attribute_value(node, "href"); const char *title = quickmedia_html_node_get_attribute_value(node, "title"); - if(href && title) { - auto item = std::make_unique(title); + // Checking for watch?v helps skipping ads + if(href && title && begins_with(href, "/watch?v=")) { + auto item = std::make_unique(title); item->url = std::string("https://www.youtube.com") + href; result_items->push_back(std::move(item)); } @@ -98,7 +104,7 @@ namespace QuickMedia { auto *result_items = (std::vector>*)userdata; const char *href = quickmedia_html_node_get_attribute_value(node, "href"); // TODO: Also add title for related media - if(href) { + if(href && begins_with(href, "/watch?v=")) { auto item = std::make_unique(""); item->url = std::string("https://www.youtube.com") + href; result_items->push_back(std::move(item)); -- cgit v1.2.3