mirror of
https://github.com/QL-Win/QuickLook.git
synced 2026-01-13 07:05:24 +08:00
Replace supported image extension list with image detection via MagickImageInfo. (#818)
* Replace supported image extension list with image detection via MagickImageInfo. * Change ImageViewer priority to -4. Change VideoViewer priority to -3 and detect audio/video via MediaInfo instead of file extensions. * Make mediaInfo a class static and initialize once. Add some notes about MediaInfo Open and Close. * Remove try/catch from Prepare and let it be handled in caller. If there was an exception due to MediaInfo it would have already occurred in CanHandle. * Upgrade ImageMagick to latest * Only check extension for well known image and animated image types. For other image formats, let ImageMagick try to detect by file content. Upgrade to latest Magick.NET Co-authored-by: Frank Becker <frank.becker@thoughtexchange.com>
This commit is contained in:
@@ -27,23 +27,19 @@ namespace QuickLook.Plugin.VideoViewer
|
||||
{
|
||||
public class Plugin : IViewer
|
||||
{
|
||||
private static readonly HashSet<string> Formats = new HashSet<string>(new[]
|
||||
{
|
||||
// video
|
||||
".3g2", ".3gp", ".3gp2", ".3gpp", ".amv", ".asf", ".avi", ".flv", ".m4v", ".mkv", ".mov", ".mp4", ".mp4v",
|
||||
".mpeg", ".mpg", ".mts", ".m2ts", ".mxf", ".ogv", ".qt", ".tp", ".ts", ".vob", ".webm", ".wmv",
|
||||
// audio
|
||||
".3gp", ".aa", ".aac", ".aax", ".act", ".aif", ".aiff", ".amr", ".ape", ".au", ".awb", ".dct", ".dss", ".dvf",
|
||||
".flac", ".gsm", ".iklax", ".ivs", ".m4a", ".m4b", ".m4p", ".m4r", ".mka", ".mmf", ".mp3", ".mpc", ".msv",
|
||||
".ogg", ".oga", ".mogg", ".opus", ".ra", ".raw", ".rm", ".tta", ".vox", ".wav", ".webm", ".wma", ".wv"
|
||||
});
|
||||
|
||||
private ContextObject _context;
|
||||
private MediaInfo.MediaInfo _mediaInfo;
|
||||
private static MediaInfo.MediaInfo _mediaInfo;
|
||||
|
||||
private ViewerPanel _vp;
|
||||
|
||||
public int Priority => -10; // make it lower than TextViewer
|
||||
public int Priority => -3;
|
||||
|
||||
static Plugin()
|
||||
{
|
||||
_mediaInfo = new MediaInfo.MediaInfo(Path.Combine(
|
||||
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
|
||||
Environment.Is64BitProcess ? "MediaInfo-x64\\" : "MediaInfo-x86\\"));
|
||||
_mediaInfo.Option("Cover_Data", "base64");
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
@@ -52,36 +48,46 @@ namespace QuickLook.Plugin.VideoViewer
|
||||
|
||||
public bool CanHandle(string path)
|
||||
{
|
||||
return !Directory.Exists(path) && Formats.Contains(Path.GetExtension(path)?.ToLower());
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
try
|
||||
{
|
||||
_mediaInfo.Open(path);
|
||||
string videoCodec = _mediaInfo.Get(StreamKind.Video, 0, "Format");
|
||||
string audioCodec = _mediaInfo.Get(StreamKind.Audio, 0, "Format");
|
||||
// Note MediaInfo.Close seems to close the dll and you have to re-create the MediaInfo
|
||||
// object like in the static class constructor above. Any call to Get methods etc.
|
||||
// will result in a "Unable to load MediaInfo library" error.
|
||||
// Ref: https://github.com/MediaArea/MediaInfoLib/blob/master/Source/MediaInfoDLL/MediaInfoDLL.cs
|
||||
// Pretty sure it doesn't leak when opening another file as the c++ code calls Close on Open
|
||||
// Ref: https://github.com/MediaArea/MediaInfoLib/blob/master/Source/MediaInfo/MediaInfo_Internal.cpp
|
||||
if (videoCodec == "Unable to load MediaInfo library") // should not happen
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!string.IsNullOrWhiteSpace(videoCodec) || !string.IsNullOrWhiteSpace(audioCodec))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Prepare(string path, ContextObject context)
|
||||
{
|
||||
_context = context;
|
||||
|
||||
try
|
||||
string videoCodec = _mediaInfo.Get(StreamKind.Video, 0, "Format");
|
||||
if (!string.IsNullOrWhiteSpace(videoCodec)) // video
|
||||
{
|
||||
_mediaInfo = new MediaInfo.MediaInfo(Path.Combine(
|
||||
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
|
||||
Environment.Is64BitProcess ? "MediaInfo-x64\\" : "MediaInfo-x86\\"));
|
||||
_mediaInfo.Option("Cover_Data", "base64");
|
||||
int.TryParse(_mediaInfo.Get(StreamKind.Video, 0, "Width"), out var width);
|
||||
int.TryParse(_mediaInfo.Get(StreamKind.Video, 0, "Height"), out var height);
|
||||
double.TryParse(_mediaInfo.Get(StreamKind.Video, 0, "Rotation"), out var rotation);
|
||||
|
||||
_mediaInfo.Open(path);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_mediaInfo?.Dispose();
|
||||
_mediaInfo = null;
|
||||
}
|
||||
|
||||
context.TitlebarOverlap = true;
|
||||
|
||||
if (_mediaInfo == null ||
|
||||
!string.IsNullOrWhiteSpace(_mediaInfo.Get(StreamKind.General, 0, "VideoCount"))) // video
|
||||
{
|
||||
int.TryParse(_mediaInfo?.Get(StreamKind.Video, 0, "Width"), out var width);
|
||||
int.TryParse(_mediaInfo?.Get(StreamKind.Video, 0, "Height"), out var height);
|
||||
double.TryParse(_mediaInfo?.Get(StreamKind.Video, 0, "Rotation"), out var rotation);
|
||||
// Correct rotation: on some machine the value "90" becomes "90000" by some reason
|
||||
if (rotation > 360)
|
||||
rotation /= 1e3;
|
||||
@@ -110,6 +116,8 @@ namespace QuickLook.Plugin.VideoViewer
|
||||
context.TitlebarBlurVisibility = false;
|
||||
context.TitlebarColourVisibility = false;
|
||||
}
|
||||
|
||||
context.TitlebarOverlap = true;
|
||||
}
|
||||
|
||||
public void View(string path, ContextObject context)
|
||||
@@ -127,11 +135,6 @@ namespace QuickLook.Plugin.VideoViewer
|
||||
{
|
||||
_vp?.Dispose();
|
||||
_vp = null;
|
||||
|
||||
_mediaInfo?.Dispose();
|
||||
_mediaInfo = null;
|
||||
|
||||
_context = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user