Skip to content

Commit

Permalink
Merge pull request #68 from 2b-zipper/save-quality
Browse files Browse the repository at this point in the history
Save quality setting
  • Loading branch information
erievs authored Jan 12, 2025
2 parents 52facd0 + d8bfd19 commit 50fde39
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 2 additions & 0 deletions source/data_io/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void load_settings() {
var_community_image_size = std::min(COMMUNITY_IMAGE_SIZE_MAX, std::max(COMMUNITY_IMAGE_SIZE_MIN, load_int("community_image_size", COMMUNITY_IMAGE_SIZE_DEFAULT)));
var_autoplay_level = std::min(2, std::max(0, load_int("autoplay_level", 2)));
var_loop_mode = std::min(2, std::max(0, load_int("loop_mode", 0)));
var_video_quality = std::min(480, std::max(0, load_int("video_quality", var_is_new3ds ? 360 : 144)));
var_forward_buffer_ratio = std::max(0.1, std::min(1.0, load_double("forward_buffer_ratio", 0.8)));
var_history_enabled = load_int("history_enabled", 1);
var_video_show_debug_info = load_int("video_show_debug_info", 0);
Expand Down Expand Up @@ -83,6 +84,7 @@ void save_settings() {
add_int("community_image_size", var_community_image_size);
add_int("autoplay_level", var_autoplay_level);
add_int("loop_mode", var_loop_mode);
add_int("video_quality", var_video_quality);
add_double("forward_buffer_ratio", var_forward_buffer_ratio);
add_int("history_enabled", var_history_enabled);
add_int("video_show_debug_info", var_video_show_debug_info);
Expand Down
36 changes: 29 additions & 7 deletions source/scenes/video_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,10 +1172,30 @@ static void load_video_page(void *arg) {
for (auto i : available_qualities) if (var_is_new3ds || i <= 240) video_quality_selector_view->button_texts.push_back(std::to_string(i) + "p");
video_quality_selector_view->button_num = video_quality_selector_view->button_texts.size();

auto is_available = [&] (int p_value) { return tmp_video_info.video_stream_urls.count(p_value) || (p_value == 480 && tmp_video_info.both_stream_url != ""); };
if (!audio_only_mode && !is_available(video_p_value)) {
video_p_value = var_is_new3ds ? 360 : 144;
if (!is_available(video_p_value)) audio_only_mode = true;
auto is_available = [&] (int p_value) { return tmp_video_info.video_stream_urls.count(p_value) || ((p_value == 360 || p_value == 480) && tmp_video_info.both_stream_url != ""); };
if (var_video_quality == 0) {
audio_only_mode = true;
} else {
if (!audio_only_mode && !is_available(var_video_quality)) {
video_p_value = var_is_new3ds ? 360 : 144;
if (var_is_new3ds && !is_available(video_p_value)) {
auto it = std::find_if(available_qualities.rbegin(), available_qualities.rend(), [&is_available](int quality) {
return quality != 480 && is_available(quality);
});
if (it != available_qualities.rend()) {
video_p_value = *it;
} else {
audio_only_mode = true;
}
} else {
video_p_value = 144;
if (!is_available(video_p_value)) {
audio_only_mode = true;
}
}
} else {
video_p_value = var_video_quality;
}
}
video_quality_selector_view->selected_button = audio_only_mode ? 0 : 1 + std::find(available_qualities.begin(), available_qualities.end(), (int) video_p_value) - available_qualities.begin();
video_quality_selector_view->set_on_change([available_qualities] (const SelectorView &view) {
Expand All @@ -1194,6 +1214,8 @@ static void load_video_page(void *arg) {
vid_change_video_request = true;
if (network_decoder.ready) network_decoder.interrupt = true;
}
var_video_quality = audio_only_mode ? 0 : video_p_value;
misc_tasks_request(TASK_SAVE_SETTINGS);
});
// update playlist tab
if (cur_video_info.playlist.id != "" && cur_video_info.playlist.id == tmp_video_info.playlist.id) {
Expand Down Expand Up @@ -1698,13 +1720,13 @@ static void decode_thread(void* arg) {
if (audio_only_mode) {
result = network_decoder.init(playing_video_info.audio_stream_url, stream_downloader,
playing_video_info.is_livestream ? playing_video_info.stream_fragment_len : -1, playing_video_info.needs_timestamp_adjusting(), var_is_new3ds);
} else if ((video_p_value == 480 || video_p_value == 360) && playing_video_info.both_stream_url != "") {
} else if ((video_p_value == 360 || video_p_value == 480) && playing_video_info.both_stream_url != "") {
// itag 18 (both_stream) of a long video takes too much time and sometimes leads to a crash
result = network_decoder.init(playing_video_info.both_stream_url, stream_downloader,
playing_video_info.is_livestream ? playing_video_info.stream_fragment_len : -1, playing_video_info.needs_timestamp_adjusting(), var_is_new3ds);
} else if (playing_video_info.video_stream_urls[(int) video_p_value] != "" && playing_video_info.audio_stream_url != "") {
result = network_decoder.init(playing_video_info.video_stream_urls[(int) video_p_value], playing_video_info.audio_stream_url, stream_downloader,
playing_video_info.is_livestream ? playing_video_info.stream_fragment_len : -1, playing_video_info.needs_timestamp_adjusting(), var_is_new3ds && (video_p_value == 480 || video_p_value == 360));
playing_video_info.is_livestream ? playing_video_info.stream_fragment_len : -1, playing_video_info.needs_timestamp_adjusting(), var_is_new3ds && (video_p_value == 360 || video_p_value == 480));
} else {
result.code = -1;
result.string = "YouTube parser error";
Expand Down Expand Up @@ -2188,4 +2210,4 @@ void VideoPlayer_draw(void)
}

}
}
}
1 change: 1 addition & 0 deletions source/variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ bool var_wifi_enabled = false;
bool var_history_enabled = true;
int var_autoplay_level = 2; // 0 : never, 1 : only in a playlist, 2 : always
int var_loop_mode = false;
int var_video_quality = 0;
bool var_show_fps = false;
bool var_full_screen_mode = false;
bool var_full_dislike_like_count = false;
Expand Down
1 change: 1 addition & 0 deletions source/variables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern bool var_wifi_enabled;
extern bool var_history_enabled;
extern int var_autoplay_level;
extern int var_loop_mode;
extern int var_video_quality;
extern bool var_show_fps;
extern bool var_full_dislike_like_count;
extern bool var_full_screen_mode;
Expand Down

0 comments on commit 50fde39

Please sign in to comment.