From ca875b39ac35ac1499c303672f7217e2a7bbcab1 Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Sat, 19 Jun 2021 14:10:44 +0200
Subject: Fix youtube audio download downloading video when not needed

---
 src/QuickMedia.cpp      | 22 ++++++++++++++++++----
 src/VideoPlayer.cpp     |  2 +-
 src/plugins/Youtube.cpp |  2 ++
 3 files changed, 21 insertions(+), 5 deletions(-)

(limited to 'src')

diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index c02b9ab..9613199 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -6453,8 +6453,15 @@ namespace QuickMedia {
         bool start() override {
             remove(output_filepath.c_str());
 
-            std::vector<const char*> args = { "youtube-dl", "-f", "bestvideo+bestaudio/best", "--no-warnings", "--no-continue", "--output", output_filepath.c_str(), "--newline" };
-            if(no_video) args.push_back("-x");
+            std::vector<const char*> args = { "youtube-dl", "--no-warnings", "--no-continue", "--output", output_filepath.c_str(), "--newline" };
+            if(no_video) {
+                args.push_back("-f");
+                args.push_back("bestaudio/best");
+                args.push_back("-x");
+            } else {
+                args.push_back("-f");
+                args.push_back("bestvideo+bestaudio/best");
+            }
             args.insert(args.end(), { "--", url.c_str(), nullptr });
 
             if(exec_program_pipe(args.data(), &read_program) != 0)
@@ -6580,8 +6587,15 @@ namespace QuickMedia {
         if(download_use_youtube_dl) {
             task_result = run_task_with_loading_screen([this, url, &filename]{
                 std::string json_str;
-                std::vector<const char*> args = { "youtube-dl", "-f", "bestvideo+bestaudio/best", "--skip-download", "--print-json", "--no-warnings" };
-                if(no_video) args.push_back("-x");
+                std::vector<const char*> args = { "youtube-dl", "--skip-download", "--print-json", "--no-warnings" };
+                if(no_video) {
+                    args.push_back("-f");
+                    args.push_back("bestaudio/best");
+                    args.push_back("-x");
+                } else {
+                    args.push_back("-f");
+                    args.push_back("bestvideo+bestaudio/best");
+                }
                 args.insert(args.end(), { "--", url, nullptr });
                 if(exec_program(args.data(), accumulate_string, &json_str) != 0)
                     return false;
diff --git a/src/VideoPlayer.cpp b/src/VideoPlayer.cpp
index 1d854de..caeaa15 100644
--- a/src/VideoPlayer.cpp
+++ b/src/VideoPlayer.cpp
@@ -28,7 +28,7 @@ namespace QuickMedia {
             result += "[CHAPTER]\n";
             result += "TIMEBASE=1/1\n";
             result += "START=" + std::to_string(chapter.start_seconds) + "\n";
-            result += "END=" + std::to_string(i + 1 == chapters.size() ? chapter.start_seconds : chapters[i].start_seconds) + "\n";
+            result += "END=" + std::to_string(i + 1 == chapters.size() ? chapter.start_seconds : chapters[i + 1].start_seconds) + "\n";
             std::string title = chapter.title;
             string_replace_all(title, '\n', ' ');
             result += "title=" + std::move(title) + "\n\n";
diff --git a/src/plugins/Youtube.cpp b/src/plugins/Youtube.cpp
index 717bc8f..11ff7fa 100644
--- a/src/plugins/Youtube.cpp
+++ b/src/plugins/Youtube.cpp
@@ -1983,6 +1983,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.
+    // Or maybe move this logic to QuickMedia video_content_page when mpv fails to start up (mpv will exit with exit code 2 and the message "Failed to recognize file format." when this happens).
+    // But that might be too slow for pinephone.
     static std::string get_playback_url_recursive(std::string playback_url) {
         const int max_redirects = 5;
         for(int i = 0; i < max_redirects; ++i) {
-- 
cgit v1.2.3-70-g09d2