From a26d0fcc0a30a28ce0e458ea275fc0787c693bc6 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 8 Mar 2022 16:39:55 +0100 Subject: Save youtube watch progress and resume next time the video is played --- src/plugins/Lbry.cpp | 5 ++-- src/plugins/LocalAnime.cpp | 2 +- src/plugins/MediaGeneric.cpp | 3 ++- src/plugins/Peertube.cpp | 6 ++++- src/plugins/Soundcloud.cpp | 3 ++- src/plugins/Youtube.cpp | 54 ++++++++++++++++++++++++++++++++++++-------- 6 files changed, 57 insertions(+), 16 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/Lbry.cpp b/src/plugins/Lbry.cpp index 73c37ba..feac2c5 100644 --- a/src/plugins/Lbry.cpp +++ b/src/plugins/Lbry.cpp @@ -328,7 +328,7 @@ namespace QuickMedia { } static PluginResult video_get_stream_url(Page *page, const std::string &video_url, std::string &streaming_url, std::string &err_str) { - std::string url = "https://api.na-backend.odysee.com/api/v1/proxy?m=resolve"; + std::string url = "https://api.na-backend.odysee.com/api/v1/proxy?m=get"; Json::Value request_params_json(Json::objectValue); request_params_json["save_file"] = false; @@ -391,9 +391,10 @@ namespace QuickMedia { return ""; } - PluginResult LbryVideoPage::load(std::string &title, std::string&, std::vector&, std::string &err_str) { + PluginResult LbryVideoPage::load(std::string &title, std::string&, double &duration, std::vector&, std::string &err_str) { streaming_url.clear(); title = this->title; + duration = 0.0; return video_get_stream_url(this, url, streaming_url, err_str); } } \ No newline at end of file diff --git a/src/plugins/LocalAnime.cpp b/src/plugins/LocalAnime.cpp index 0de33b4..1bc8ca8 100644 --- a/src/plugins/LocalAnime.cpp +++ b/src/plugins/LocalAnime.cpp @@ -490,7 +490,7 @@ namespace QuickMedia { } } - void LocalAnimeVideoPage::set_watch_progress(int64_t time_pos_sec) { + void LocalAnimeVideoPage::set_watch_progress(int64_t time_pos_sec, int64_t) { std::string filename_relative_to_anime_dir = anime_path_to_item_name(url); FileAnalyzer file_analyzer; diff --git a/src/plugins/MediaGeneric.cpp b/src/plugins/MediaGeneric.cpp index 5ee5fbc..c1044b0 100644 --- a/src/plugins/MediaGeneric.cpp +++ b/src/plugins/MediaGeneric.cpp @@ -224,8 +224,9 @@ namespace QuickMedia { return video_url; } - PluginResult MediaGenericVideoPage::load(std::string&, std::string&, std::vector&, std::string &err_msg) { + PluginResult MediaGenericVideoPage::load(std::string&, std::string&, double &duration, std::vector&, std::string &err_msg) { video_url.clear(); + duration = 0.0; if(!search_page->video_custom_handler) { video_url = url; return PluginResult::OK; diff --git a/src/plugins/Peertube.cpp b/src/plugins/Peertube.cpp index a5c5865..a9620f2 100644 --- a/src/plugins/Peertube.cpp +++ b/src/plugins/Peertube.cpp @@ -370,7 +370,7 @@ namespace QuickMedia { } // TODO: Media chapters - PluginResult PeertubeVideoPage::load(std::string &title, std::string &channel_url, std::vector&, std::string &err_str) { + PluginResult PeertubeVideoPage::load(std::string &title, std::string &channel_url, double &duration, std::vector&, std::string &err_str) { Json::Value json_root; std::string err_msg; DownloadResult download_result = download_json(json_root, server + "/api/v1/videos/" + url, {}, true, &err_msg); @@ -386,6 +386,10 @@ namespace QuickMedia { if(name_json.isString()) title = name_json.asString(); + const Json::Value &duration_json = json_root["duration"]; + if(duration_json.isInt64()) + duration = duration_json.asInt64(); + const Json::Value &channel_json = json_root["channel"]; if(channel_json.isObject()) { const Json::Value &channel_url_json = channel_json["url"]; diff --git a/src/plugins/Soundcloud.cpp b/src/plugins/Soundcloud.cpp index 9da65ee..0412aa6 100644 --- a/src/plugins/Soundcloud.cpp +++ b/src/plugins/Soundcloud.cpp @@ -468,8 +468,9 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult SoundcloudAudioPage::load(std::string &title, std::string&, std::vector&, std::string&) { + PluginResult SoundcloudAudioPage::load(std::string &title, std::string&, double &duration, std::vector&, std::string&) { title = this->title; + duration = 0.0; return PluginResult::OK; } diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp index d80d86c..2d92cd8 100644 --- a/src/plugins/Youtube.cpp +++ b/src/plugins/Youtube.cpp @@ -7,6 +7,7 @@ #include "../../include/VideoPlayer.hpp" #include "../../include/Utils.hpp" #include "../../include/Theme.hpp" +#include "../../plugins/WatchProgress.hpp" #include #include extern "C" { @@ -1858,6 +1859,30 @@ namespace QuickMedia { VideoPage::set_url(std::move(new_url)); } + std::string YoutubeVideoPage::get_url_timestamp() { + if(!timestamp.empty()) + return timestamp; + + std::string video_id; + if(!youtube_url_extract_id(url, video_id)) { + fprintf(stderr, "Failed to extract youtube id from %s\n", url.c_str()); + return ""; + } + + std::unordered_map watch_progress = get_watch_progress_for_plugin("youtube"); + auto it = watch_progress.find(video_id); + if(it == watch_progress.end()) + return ""; + + // If we are very close to the end then start from the beginning. + // This is the same behavior as mpv. + // This is better because we dont want the video player to stop immediately after we start playing and we dont get any chance to seek. + if(it->second.time_pos_sec + 10.0 >= it->second.duration_sec) + return ""; + else + return std::to_string(it->second.time_pos_sec); + } + BodyItems YoutubeVideoPage::get_related_media(const std::string &url) { comments_continuation_token.clear(); BodyItems result_items; @@ -2213,6 +2238,7 @@ namespace QuickMedia { video_details.author.clear(); video_details.views.clear(); video_details.description.clear(); + video_details.duration = 0.0; } PluginResult YoutubeVideoPage::parse_video_response(const Json::Value &json_root, std::string &title, std::string &channel_url, std::vector &chapters, std::string &err_str) { @@ -2274,11 +2300,18 @@ namespace QuickMedia { const Json::Value &author_json = video_details_json["author"]; const Json::Value &view_count_json = video_details_json["viewCount"]; const Json::Value &short_description_json = video_details_json["shortDescription"]; + const Json::Value &length_seconds_json = video_details_json["lengthSeconds"]; if(title_json.isString()) video_details.title = title_json.asString(); if(author_json.isString()) video_details.author = author_json.asString(); if(view_count_json.isString()) video_details.views = view_count_json.asString(); if(short_description_json.isString()) video_details.description = short_description_json.asString(); + if(length_seconds_json.isString()) { + const char *length_seconds_str = length_seconds_json.asCString(); + int duration = 0; + to_num(length_seconds_str, strlen(length_seconds_str), duration); + video_details.duration = duration; + } title = video_details.title; if(!video_details.description.empty()) @@ -2333,7 +2366,7 @@ namespace QuickMedia { return PluginResult::OK; } - PluginResult YoutubeVideoPage::load(std::string &title, std::string &channel_url, std::vector &chapters, std::string &err_str) { + PluginResult YoutubeVideoPage::load(std::string &title, std::string &channel_url, double &duration, std::vector &chapters, std::string &err_str) { std::string video_id; if(!youtube_url_extract_id(url, video_id)) { fprintf(stderr, "Failed to extract youtube id from %s\n", url.c_str()); @@ -2408,6 +2441,7 @@ R"END( PluginResult result = parse_video_response(json_root, title, channel_url, chapters, err_str); if(result == PluginResult::OK) { err_str.clear(); + duration = video_details.duration; return PluginResult::OK; } } @@ -2483,15 +2517,6 @@ R"END( if(!format.isObject()) continue; - if(is_adaptive) { - // TODO: Fix. Some streams use &sq=num instead of index - const Json::Value &index_range_json = format["indexRange"]; - if(index_range_json.isNull()) { - fprintf(stderr, "Ignoring adaptive stream without indexRange\n"); - continue; - } - } - // TODO: Support HDR? const Json::Value &quality_label_json = format["qualityLabel"]; if(quality_label_json.isString() && strstr(quality_label_json.asCString(), "HDR")) continue; @@ -2574,4 +2599,13 @@ R"END( const Json::Value &adaptive_formats_json = streaming_data_json["adaptiveFormats"]; parse_format(adaptive_formats_json, true); } + + void YoutubeVideoPage::set_watch_progress(int64_t time_pos_sec, int64_t duration_sec) { + std::string video_id; + if(!youtube_url_extract_id(url, video_id)) { + show_notification("QuickMedia", "Failed to extract youtube id from " + url); + return; + } + set_watch_progress_for_plugin("youtube", video_id, time_pos_sec, duration_sec, video_id); + } } -- cgit v1.2.3