-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathPlugin.cs
140 lines (117 loc) · 4.59 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright © 2017 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
extern alias MediaInfoWrapper;
using MediaInfoWrapper::MediaInfo;
using QuickLook.Common.Plugin;
using System;
using System.IO;
using System.Reflection;
using System.Windows;
namespace QuickLook.Plugin.VideoViewer;
public class Plugin : IViewer
{
private static MediaInfoLib _mediaInfo;
private ViewerPanel _vp;
public int Priority => -3;
static Plugin()
{
_mediaInfo = new MediaInfoLib(Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
Environment.Is64BitProcess ? "MediaInfo-x64\\" : "MediaInfo-x86\\"));
_mediaInfo.Option("Cover_Data", "base64");
}
public void Init()
{
QLVRegistry.Register();
}
public bool CanHandle(string path)
{
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)
{
string videoCodec = _mediaInfo.Get(StreamKind.Video, 0, "Format");
if (!string.IsNullOrWhiteSpace(videoCodec)) // 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;
var windowSize = new Size
{
Width = Math.Max(100, width == 0 ? 1366 : width),
Height = Math.Max(100, height == 0 ? 768 : height)
};
if (rotation % 180 != 0)
windowSize = new Size(windowSize.Height, windowSize.Width);
context.SetPreferredSizeFit(windowSize, 0.8);
context.TitlebarAutoHide = true;
context.Theme = Themes.Dark;
context.TitlebarBlurVisibility = true;
}
else // audio
{
context.PreferredSize = new Size(500, 300);
context.CanResize = false;
context.TitlebarAutoHide = false;
context.TitlebarBlurVisibility = false;
context.TitlebarColourVisibility = false;
}
context.TitlebarOverlap = true;
}
public void View(string path, ContextObject context)
{
_vp = new ViewerPanel(context);
context.ViewerContent = _vp;
context.Title = $"{Path.GetFileName(path)}";
_vp.LoadAndPlay(path, _mediaInfo);
}
public void Cleanup()
{
_vp?.Dispose();
_vp = null;
}
}