Skip to content

Commit

Permalink
only generate thumbnails using ffmpegthumbnailer
Browse files Browse the repository at this point in the history
  • Loading branch information
thermitegod committed May 7, 2022
1 parent 43dbb37 commit 705182c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/vfs/vfs-file-info.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ vfs_file_info_is_thumbnail_loaded(VFSFileInfo* fi, bool big)
}

bool
vfs_file_info_load_thumbnail(VFSFileInfo* fi, const char* full_path, bool big)
vfs_file_info_load_thumbnail(VFSFileInfo* fi, const std::string& full_path, bool big)
{
if (big)
{
Expand Down
4 changes: 3 additions & 1 deletion src/vfs/vfs-file-info.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#pragma once

#include <string>

#include <atomic>
#include <chrono>

Expand Down Expand Up @@ -105,7 +107,7 @@ time_t* vfs_file_info_get_mtime(VFSFileInfo* fi);
time_t* vfs_file_info_get_atime(VFSFileInfo* fi);

void vfs_file_info_set_thumbnail_size(int big, int small);
bool vfs_file_info_load_thumbnail(VFSFileInfo* fi, const char* full_path, bool big);
bool vfs_file_info_load_thumbnail(VFSFileInfo* fi, const std::string& full_path, bool big);
bool vfs_file_info_is_thumbnail_loaded(VFSFileInfo* fi, bool big);

GdkPixbuf* vfs_file_info_get_big_icon(VFSFileInfo* fi);
Expand Down
119 changes: 28 additions & 91 deletions src/vfs/vfs-thumbnail-loader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,9 @@ thumbnail_loader_thread(VFSAsyncTask* task, VFSThumbnailLoader* loader)
bool load_big = (i == VFSThumbnailSize::LOAD_BIG_THUMBNAIL);
if (!vfs_file_info_is_thumbnail_loaded(req->file, load_big))
{
std::string full_path;
full_path =
g_build_filename(loader->dir->path, vfs_file_info_get_name(req->file), nullptr);
vfs_file_info_load_thumbnail(req->file, full_path.c_str(), load_big);
std::string full_path =
Glib::build_filename(loader->dir->path, vfs_file_info_get_name(req->file));
vfs_file_info_load_thumbnail(req->file, full_path, load_big);
// Slow down for debugging.
// LOG_DEBUG("DELAY!!");
// Glib::usleep(G_USEC_PER_SEC/2);
Expand Down Expand Up @@ -334,42 +333,6 @@ static GdkPixbuf*
vfs_thumbnail_load(const std::string& file_path, const std::string& uri, int size,
std::time_t mtime)
{
int w;
int h;
struct stat statbuf;
int create_size;

if (size > 256)
create_size = 512;
else if (size > 128)
create_size = 256;
else
create_size = 128;

bool file_is_video = false;
VFSMimeType* mimetype = vfs_mime_type_get_from_file_name(file_path.c_str());
if (mimetype)
{
if (!strncmp(vfs_mime_type_get_type(mimetype), "video/", 6))
file_is_video = true;
vfs_mime_type_unref(mimetype);
}

if (!file_is_video)
{
// image format cannot be recognized
if (!gdk_pixbuf_get_file_info(file_path.c_str(), &w, &h))
return nullptr;

// If the image itself is very small, we should load it directly
if (w <= create_size && h <= create_size)
{
if (w <= size && h <= size)
return gdk_pixbuf_new_from_file(file_path.c_str(), nullptr);
return gdk_pixbuf_new_from_file_at_size(file_path.c_str(), size, size, nullptr);
}
}

std::string file_name;
std::string file_hash;
std::string thumbnail_file;
Expand All @@ -384,22 +347,26 @@ vfs_thumbnail_load(const std::string& file_path, const std::string& uri, int siz

if (mtime == 0)
{
struct stat statbuf;
if (stat(file_path.c_str(), &statbuf) != -1)
mtime = statbuf.st_mtime;
}

// if mtime of video being thumbnailed is less than 5 sec ago,
// do not create a thumbnail. This means that newly created video
// files will not have a thumbnail until a refresh.
if (file_is_video && std::time(nullptr) - mtime < 5)
if (std::time(nullptr) - mtime < 5)
return nullptr;

// load existing thumbnail
GdkPixbuf* thumbnail = gdk_pixbuf_new_from_file(thumbnail_file.c_str(), nullptr);

const char* thumb_mtime;
if (thumbnail)
int w;
int h;
const char* thumb_mtime = nullptr;
GdkPixbuf* thumbnail = nullptr;
if (std::filesystem::is_regular_file(thumbnail_file))
{
thumbnail = gdk_pixbuf_new_from_file(thumbnail_file.c_str(), nullptr);

w = gdk_pixbuf_get_width(thumbnail);
h = gdk_pixbuf_get_height(thumbnail);
thumb_mtime = gdk_pixbuf_get_option(thumbnail, "tEXt::Thumb::MTime");
Expand All @@ -412,56 +379,26 @@ vfs_thumbnail_load(const std::string& file_path, const std::string& uri, int siz
g_object_unref(thumbnail);

// create new thumbnail
if (!file_is_video)
try
{
thumbnail = gdk_pixbuf_new_from_file_at_size(file_path.c_str(),
create_size,
create_size,
nullptr);
if (thumbnail)
{
std::string mtime_str;

// Note: gdk_pixbuf_apply_embedded_orientation returns a new
// pixbuf or same with incremented ref count, so unref
GdkPixbuf* thumbnail_old = thumbnail;
thumbnail = gdk_pixbuf_apply_embedded_orientation(thumbnail);
g_object_unref(thumbnail_old);
mtime_str = fmt::format("{}", mtime);
gdk_pixbuf_save(thumbnail,
thumbnail_file.c_str(),
"png",
nullptr,
"tEXt::Thumb::URI",
uri.c_str(),
"tEXt::Thumb::MTime",
mtime_str.c_str(),
nullptr);
}
ffmpegthumbnailer::VideoThumbnailer video_thumb;
// video_thumb.setLogCallback(nullptr);
// video_thumb.clearFilters();
video_thumb.setSeekPercentage(25);
video_thumb.setThumbnailSize(128);
video_thumb.setMaintainAspectRatio(true);
video_thumb.generateThumbnail(file_path,
ThumbnailerImageType::Png,
thumbnail_file,
nullptr);
}
else
catch (...) // std::logic_error
{
try
{
ffmpegthumbnailer::VideoThumbnailer video_thumb;
video_thumb.setSeekPercentage(25);
video_thumb.setThumbnailSize(128);
video_thumb.setMaintainAspectRatio(true);
// video_thumb.clearFilters();
video_thumb.generateThumbnail(file_path,
ThumbnailerImageType::Png,
thumbnail_file,
nullptr);
}
catch (...) // std::logic_error
{
// Video file cannot be opened
if (thumbnail)
g_object_unref(thumbnail);
return nullptr;
}
thumbnail = gdk_pixbuf_new_from_file(thumbnail_file.c_str(), nullptr);
// file cannot be opened
return nullptr;
}

thumbnail = gdk_pixbuf_new_from_file(thumbnail_file.c_str(), nullptr);
}

GdkPixbuf* result = nullptr;
Expand Down

1 comment on commit 705182c

@codejodler
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey t.g., i do no know how to reach you otherwise, so pls excuse the spam :| There are a lot of spacefm fans out there (i am one) and they are asking if the project will be continued. It is true that there is virtually no suitable alternative. Is your fork only for private usage or would you be able to takeover, at least formally and for urgent bug fixes ?
Perhaps drop a comment here IgnorantGuru#800

Please sign in to comment.