diff options
author | dec05eba <dec05eba@protonmail.com> | 2022-03-04 01:43:44 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2022-03-04 01:43:44 +0100 |
commit | d037e38d8e65e8dc20e783e03fdb7474ed93cf4c (patch) | |
tree | bd3cccacd29c6a102ad99d9760720a0c2752673d | |
parent | a13c19e31cb033730fa179a90f0bc5bd961bd3dc (diff) |
Do not call ffprobe for thumbnails unless its guaranteed to be a local video file
-rw-r--r-- | include/FileAnalyzer.hpp | 3 | ||||
-rw-r--r-- | src/AsyncImageLoader.cpp | 4 | ||||
-rw-r--r-- | src/FileAnalyzer.cpp | 35 |
3 files changed, 31 insertions, 11 deletions
diff --git a/include/FileAnalyzer.hpp b/include/FileAnalyzer.hpp index acc62d7..851a3e4 100644 --- a/include/FileAnalyzer.hpp +++ b/include/FileAnalyzer.hpp @@ -50,6 +50,8 @@ namespace QuickMedia { public: FileAnalyzer(); bool load_file(const char *filepath, bool load_file_metadata = true); + // Cached + bool load_metadata(); const std::string& get_filepath() const; ContentType get_content_type() const; @@ -66,5 +68,6 @@ namespace QuickMedia { std::optional<Dimensions> dimensions; std::optional<double> duration_seconds; bool loaded; + bool metadata_loaded; }; }
\ No newline at end of file diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp index b0efb05..2919f41 100644 --- a/src/AsyncImageLoader.cpp +++ b/src/AsyncImageLoader.cpp @@ -160,7 +160,7 @@ namespace QuickMedia { void AsyncImageLoader::load_create_thumbnail(const Path &thumbnail_path, const Path &thumbnail_path_resized, ThumbnailData *thumbnail_data, mgl::vec2i resize_target_size) { FileAnalyzer file_analyzer; - if(!file_analyzer.load_file(thumbnail_path.data.c_str(), true)) { + if(!file_analyzer.load_file(thumbnail_path.data.c_str(), false)) { fprintf(stderr, "Failed to convert %s to a thumbnail\n", thumbnail_path.data.c_str()); thumbnail_data->image = std::make_unique<mgl::Image>(); thumbnail_data->loading_state = LoadingState::FINISHED_LOADING; @@ -168,7 +168,7 @@ namespace QuickMedia { } if(is_content_type_video(file_analyzer.get_content_type())) { - if(video_get_first_frame(file_analyzer, thumbnail_path_resized.data.c_str(), resize_target_size.x, resize_target_size.y)) { + if(file_analyzer.load_metadata() && video_get_first_frame(file_analyzer, thumbnail_path_resized.data.c_str(), resize_target_size.x, resize_target_size.y)) { thumbnail_data->loading_state = LoadingState::READY_TO_LOAD; } else { fprintf(stderr, "Failed to get video frame of %s\n", thumbnail_path.data.c_str()); diff --git a/src/FileAnalyzer.cpp b/src/FileAnalyzer.cpp index 5b67b8d..976d2cf 100644 --- a/src/FileAnalyzer.cpp +++ b/src/FileAnalyzer.cpp @@ -207,7 +207,7 @@ namespace QuickMedia { return true; } - FileAnalyzer::FileAnalyzer() : content_type(ContentType::UNKNOWN), file_size(0), loaded(false) { + FileAnalyzer::FileAnalyzer() : content_type(ContentType::UNKNOWN), file_size(0), loaded(false), metadata_loaded(false) { } @@ -263,20 +263,37 @@ namespace QuickMedia { } } - if(load_file_metadata && content_type != ContentType::UNKNOWN) { - if(!ffprobe_extract_metadata(filepath, dimensions, duration_seconds)) { - // This is not an error, matrix allows files to be uploaded without metadata - fprintf(stderr, "Failed to extract metadata from file: %s, is ffprobe not installed?\n", filepath); - } - if(is_content_type_image(content_type)) - duration_seconds = std::nullopt; - } + if(load_file_metadata && content_type != ContentType::UNKNOWN) + load_metadata(); this->filepath = filepath; loaded = true; return true; } + bool FileAnalyzer::load_metadata() { + if(!loaded) { + fprintf(stderr, "File not loaded\n"); + return false; + } + + if(metadata_loaded) + return true; + + metadata_loaded = true; + + if(!ffprobe_extract_metadata(filepath.c_str(), dimensions, duration_seconds)) { + // This is not an error, matrix allows files to be uploaded without metadata + fprintf(stderr, "Failed to extract metadata from file: %s, is ffprobe not installed?\n", filepath.c_str()); + return false; + } + + if(is_content_type_image(content_type)) + duration_seconds = std::nullopt; + + return true; + } + const std::string& FileAnalyzer::get_filepath() const { return filepath; } |