以前、fenrir(B@r09u3 Style Op.2)の代替として自分用に作成した検索ソフトにサムネイル機能を追加。
※追記(ファイル添付)
使い方
・解凍後、ffmpeg.exeをフォルダの中に保存。(サムネイル表示しないなら不要)
・scan.txtの中に検索したいパスを記入。(複数行可)
・CreateIndex.exeを実行。
・FileSearch.exeを実行。
・複数検索はバーティカルバー(|)で区切る。AND検索。
・検索結果のリストボックス上:ファイル名をダブルクリックで直接開く。Ctrl+ダブルクリックでフォルダを開く。
・テキストボックス上:Ctrl+Enterで、動画ファイルのサムネイル表示。(数字入力のダイアログは何秒目のサムネイルにするか指定)
・サムネイル上:サムネイルをダブルクリックで直接開く。Ctrl+ダブルクリックでフォルダを開く。
指定フォルダ以下のファイルをインデックスしておき、インクリメンタルサーチの一覧をもとにFFmpegでサムネイルを作成する。
CreateThumbnail1()はUIの部分だけ固まらないようにしているが、サムネイルの作成は並列化していない。CreateThumbnail2()はサムネイルの作成まで並列化している。
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using System.Xml.Serialization; namespace FileSearch { public partial class Form1 : Form { string RemoteIndexPath = Application.StartupPath + @"\remote_index_path.txt"; string IndexPath = Application.StartupPath + @"\index.dat"; List<string> RawItems = new List<string>(); System.Collections.Concurrent.ConcurrentBag<string> FilterItems = new System.Collections.Concurrent.ConcurrentBag<string>(); List<SubForm> Children = new List<SubForm>(); int VisibleCount = 1000; int RawItemCount = 0; string WriteTime = ""; string Needle = ""; object LockObject = new object(); bool IsExit = true; public Form1() { InitializeComponent(); if (!IndexLoad()) return; LoadPosition(); var delayExecute = new DelayExecute(); delayExecute.Execute += (s, e) => SetupDatasource(); textBox1.TextChanged += (s, e) => { lock (LockObject) { IsExit = false; } Needle = textBox1.Text; delayExecute.ReserveExecute(); }; textBox1.KeyDown += (s, e) => OpenSubForm(s, e); listBox1.MouseDoubleClick += (s, e) => OpenFile(s, e); this.SizeChanged += (s, e) => { if (this.WindowState == FormWindowState.Minimized || this.WindowState == FormWindowState.Maximized) return; SavePosition(); }; this.FormClosing += (s, e) => { if (Children.Count > 0) { MessageBox.Show("フォームを全て閉じてください。"); e.Cancel = true; return; } lock (LockObject) { if (!IsExit) e.Cancel = true; } }; } private void OpenSubForm(object sender, KeyEventArgs e) { lock (LockObject) { if (!IsExit) return; } if (FilterItems.Count == 0) return; if (e.Control && e.KeyCode == Keys.Enter) { string startTime = "0"; var input = new InputBox(); input.StartPosition = FormStartPosition.Manual; input.Left = DesktopLocation.X + 10; input.Top = DesktopLocation.Y + 10; input.ShowDialog(); if (input.IsClick) { int.TryParse(input.Value, out int result); startTime = result.ToString(); } var f = new SubForm(textBox1.Text, new List<string>(FilterItems), startTime); string handle = Guid.NewGuid().ToString("N"); f.Id = handle; f.FormClosed += (ss, ee) => { Children = new List<SubForm>(Children.Where(x => x.Id != handle)); }; Children.Add(f); f.Show(); } } private bool IndexLoad() { string remoteIndexPath = ""; if (File.Exists(RemoteIndexPath)) { remoteIndexPath = File.ReadAllText(RemoteIndexPath); } if (!File.Exists(IndexPath) && !File.Exists(remoteIndexPath)) // ローカル、リモートにない場合 { MessageBox.Show("インデックスファイルが見つかりませんでした。"); return false; } else if (File.Exists(IndexPath) && !File.Exists(remoteIndexPath)) // ローカルにあって、リモートにない場合 { RawItems = File.ReadAllLines(IndexPath).ToList(); RawItemCount = RawItems.Count(); WriteTime = File.GetLastWriteTime(IndexPath).ToString("yyyy/MM/dd HH:mm:ss"); } else if (!File.Exists(IndexPath) && File.Exists(remoteIndexPath)) // ローカルになくて、リモートにある場合 { File.Copy(remoteIndexPath, IndexPath); RawItems = File.ReadAllLines(IndexPath).ToList(); RawItemCount = RawItems.Count(); WriteTime = File.GetLastWriteTime(IndexPath).ToString("yyyy/MM/dd HH:mm:ss"); } else if (File.Exists(IndexPath) && File.Exists(remoteIndexPath)) // ローカル、リモートにある場合 { if (File.GetLastWriteTime(IndexPath) < File.GetLastWriteTime(remoteIndexPath)) // リモートの方が最新の場合 { if (DialogResult.Yes == MessageBox.Show("最新のインデックスファイルが存在します。更新しますか?","", MessageBoxButtons.YesNo)) { File.Copy(remoteIndexPath, IndexPath, true); } RawItems = File.ReadAllLines(IndexPath).ToList(); RawItemCount = RawItems.Count(); WriteTime = File.GetLastWriteTime(IndexPath).ToString("yyyy/MM/dd HH:mm:ss"); } else // ローカルの方が最新の場合 { RawItems = File.ReadAllLines(IndexPath).ToList(); RawItemCount = RawItems.Count(); WriteTime = File.GetLastWriteTime(IndexPath).ToString("yyyy/MM/dd HH:mm:ss"); } } return true; } private void SetupDatasource() { var stopwatch = new Stopwatch(); stopwatch.Start(); IEnumerable<string> items = new List<string>(); if (Needle == "") { items = RawItems.Take(VisibleCount); } else { List<string> tmpList = new List<string>(RawItems); foreach (string tmpString in Needle.Split('|')) { tmpList = tmpList.AsParallel().Where(x => x.ToLower().Contains(tmpString.ToLower())).ToList(); } items = tmpList.Take(VisibleCount); } var itemClassList = new System.Collections.Concurrent.ConcurrentBag<ItemClass>(); FilterItems = new System.Collections.Concurrent.ConcurrentBag<string>(); System.Threading.Tasks.Parallel.ForEach(items, item => { itemClassList.Add(new ItemClass() { DisplayItemPath = Path.GetFileName(item) + " . . . ■" + Path.GetDirectoryName(item), ItemPath = item }); FilterItems.Add(item); }); stopwatch.Stop(); Invoke(new Action(() => { listBox1.DataSource = itemClassList.ToList(); listBox1.DisplayMember = "DisplayItemPath"; Text = items.Count() + "/" + RawItemCount + "件表示 (" + stopwatch.ElapsedMilliseconds.ToString() + "ms):" + WriteTime + "インデックス"; })); lock (LockObject) { IsExit = true; } } private void OpenFile(object sender, MouseEventArgs e) { if (listBox1.SelectedItem == null) return; var itemPath = ((ItemClass)listBox1.SelectedItem).ItemPath; if (!File.Exists(itemPath)) { MessageBox.Show("ファイルを開くことができませんでした。"); return; } if (ModifierKeys == Keys.Control) { var info = new ProcessStartInfo("explorer.exe"); info.Arguments = $"/select, \"{itemPath}\""; Process.Start(info); } else { Process.Start(itemPath); } } private void LoadPosition() { if (!File.Exists(Application.StartupPath + @"\dat.xml")) return; this.StartPosition = FormStartPosition.Manual; XmlSerializer xs = new XmlSerializer(typeof(List<int>)); using (StreamReader sr = new StreamReader(Application.StartupPath + @"\dat.xml", Encoding.UTF8)) { try { List<int> items = (List<int>)xs.Deserialize(sr); if (0 > (int)items[2]) { this.StartPosition = FormStartPosition.WindowsDefaultLocation; return; } this.Width = (int)items[0]; this.Height = (int)items[1]; this.Top = (int)items[2]; this.Left = (int)items[3]; } catch { this.StartPosition = FormStartPosition.WindowsDefaultLocation; } } } private void SavePosition() { if (this.WindowState == FormWindowState.Maximized) return; XmlSerializer xs = new XmlSerializer(typeof(List<int>)); using (StreamWriter sw = new StreamWriter(Application.StartupPath + @"\dat.xml", false, Encoding.UTF8)) { List<int> items = new List<int>(); items.Add(this.Width); items.Add(this.Height); items.Add(this.Top); items.Add(this.Left); xs.Serialize(sw, items); } } } struct ItemClass { public string DisplayItemPath { set; get; } public string ItemPath { set; get; } } class DelayExecute { public event EventHandler Execute; // SetupDatasource()を登録 private int DelayTime = 500; System.Threading.Timer Timer; public DelayExecute() { Timer = new System.Threading.Timer(x => { Execute(this, EventArgs.Empty); }); } public void ReserveExecute() // TextChangeで発火 { Timer.Change(DelayTime, Timeout.Infinite); // Timeout.Infiniteは一度だけ呼ぶ } } } |
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Linq; namespace FileSearch { public partial class SubForm : Form { public string Id { set; get; } object LockObject = new object(); bool IsExit = false; public SubForm(string needle, List<string> filterItems, string startTime) { InitializeComponent(); var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; listView1.MultiSelect = false; listView1.MouseDoubleClick += (s, e) => { string itemPath = listView1.SelectedItems[0].SubItems[1].Text; if (ModifierKeys == Keys.Control) { var info = new ProcessStartInfo("explorer.exe"); info.Arguments = $"/select, \"{itemPath}\""; Process.Start(info); } else { Process.Start(itemPath); } }; this.FormClosing += (s, e) => { lock (LockObject) { if (!IsExit) { tokenSource.Cancel(); e.Cancel = true; } } }; CreateThumbnail2(needle, filterItems, startTime, token); } private async void CreateThumbnail2(string needle, List<string> filterItems, string startTime, CancellationToken token) { ImageList il = new ImageList(); il.ImageSize = new Size(200, 200); il.ColorDepth = ColorDepth.Depth32Bit; listView1.LargeImageList = il; var listItems = new System.Collections.Concurrent.ConcurrentBag<ListViewItem>(); var tasks = new List<Task>(); foreach (string f in filterItems) { if (!File.Exists(f)) continue; if (Path.GetExtension(f) != ".mp4" && Path.GetExtension(f) != ".mkv" && Path.GetExtension(f) != ".flv") continue; var t = Task.Run(() => { if (token.IsCancellationRequested) return; while (!IsHandleCreated) { } int itemCount = 0; string option = " -ss " + startTime + " -i \"" + f + "\" -vframes 1 -f image2 pipe:1"; ProcessStartInfo ps = new ProcessStartInfo(Application.StartupPath + @"\ffmpeg.exe", option); ps.RedirectStandardOutput = true; ps.CreateNoWindow = true; ps.UseShellExecute = false; using (Process p = new Process()) { try { p.StartInfo = ps; if (!p.Start()) throw new Exception(); using (Image img = Image.FromStream(Stream.Synchronized(p.StandardOutput.BaseStream))) { Image thumb = img.GetThumbnailImage(200, 200, null, IntPtr.Zero); Invoke(new Action(() => { il.Images.Add(thumb); itemCount = il.Images.Count; })); } } catch (Exception) { Image img = new Bitmap(200, 200); Invoke(new Action(() => { il.Images.Add(img); itemCount = il.Images.Count; })); } } string fileName = FileNameComp(Path.GetFileName(f), FileSizeUnit(new FileInfo(f).Length)); var item = new ListViewItem(fileName, itemCount-1); item.SubItems.Add(f); listItems.Add(item); Invoke((Action)(() => { Text = "集計 (" + itemCount.ToString() + "件) : " + startTime + "秒 : " + needle; })); }, token); tasks.Add(t); } try { await Task.WhenAll(tasks.ToArray()); } catch { } listView1.Items.AddRange(listItems.ToArray()); Text = "完了 (" + il.Images.Count.ToString() + "件) : " + startTime + "秒 : "+ needle; lock (LockObject) { IsExit = true; } } private async void CreateThumbnail1(string needle, List<string> filterItems, CancellationToken token) { ImageList il = new ImageList(); il.ImageSize = new Size(200, 200); il.ColorDepth = ColorDepth.Depth32Bit; listView1.LargeImageList = il; int itemIndex = 0; await Task.Run(() => { while (!IsHandleCreated) { } foreach (string f in filterItems) { if (token.IsCancellationRequested) break; if (!File.Exists(f)) continue; if (Path.GetExtension(f) != ".mp4" && Path.GetExtension(f) != ".mkv" && Path.GetExtension(f) != ".flv") continue; string ss = "60"; string option = " -ss " + ss + " -i \"" + f + "\" -vframes 1 -f image2 pipe:1"; ProcessStartInfo ps = new ProcessStartInfo(Application.StartupPath + @"\ffmpeg.exe", option); ps.RedirectStandardOutput = true; ps.CreateNoWindow = true; ps.UseShellExecute = false; using (Process p = new Process()) { try { p.StartInfo = ps; if (!p.Start()) throw new Exception(); using (Image img = Image.FromStream(p.StandardOutput.BaseStream)) { Image thumb = img.GetThumbnailImage(200, 200, null, IntPtr.Zero); Invoke(new Action(() => { il.Images.Add(thumb); })); } } catch (Exception) { Image img = new Bitmap(200, 200); Invoke(new Action(() => { il.Images.Add(img); })); } } string fileName = FileNameComp(Path.GetFileName(f), FileSizeUnit(new FileInfo(f).Length)); var item = new ListViewItem(fileName , itemIndex); item.SubItems.Add(f); itemIndex++; Invoke((Action)(() => { Text = "集計 (" + itemIndex.ToString() + "件) : " + needle; listView1.Items.Add(item); })); } Invoke(new Action(() => { Text = "完了 (" + (itemIndex).ToString() + "件) : " + needle; })); }, token); lock (LockObject) { IsExit = true; } } private string FileSizeUnit(long b) { float k = b / 1024; float m = k / 1024; float g = m / 1024; float t = g / 1024; if (t >= 1) return "(" + t.ToString("0.00") + "TB)"; if (g >= 1) return "(" + g.ToString("0.00") + "GB)"; if (m >= 1) return "(" + m.ToString("0.00") + "MB)"; if (k >= 1) return "(" + k.ToString("0.00") + "KB)"; return ""; } private string FileNameComp(string rawFileName, string fileSize) { string fileName = rawFileName; while (TextRenderer.MeasureText(fileName + fileSize, new Font("メイリオ", 9)).Width > 200) { fileName = fileName.Substring(0, fileName.Length - 1); } return fileName + fileSize; } } } |