private void btnScan_Click(object sender, EventArgs e) { string[] resDir = this.txtResDirs.Text.Replace("\r\n", string.Empty).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); string[] scanExt = this.txtScanType.Text.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); var fileList = ScanHelper.ScanFilePath(resDir, scanExt); var newFileList = new List <string>(); //过滤已处理的 foreach (var item in fileList) { //已经在库中的则不再处理 if (this._DB.Videos.Count(x => x.PhysicalPath == item) > 0) { continue; } newFileList.Add(item); } this.lbResult.DataSource = newFileList; }
private void btnRun_Click(object sender, EventArgs e) { List <VideoCoverInfo> videoList = new List <VideoCoverInfo>(); this.txtLog.Text = string.Empty; #region 找出扫描的视频 if (CURRENT_SCAN_SETTING == SCAN_SETTING_ALL) { var srcVideoInfo = this._DB.Videos .Where(x => x.IsSkip != true) .ToList(); foreach (var item in srcVideoInfo) { videoList.Add(new VideoCoverInfo { VideoInfo = item, SrcVideoPath = item.PhysicalPath }); } } else if (CURRENT_SCAN_SETTING == SCAN_SETTING_PATH) { string[] scanPaths = this.txtScanPath.Text .Replace("\r\n", string.Empty) .Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); var scanTypes = GetSetting("ScanType").Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); if (scanPaths == null || scanPaths.Length <= 0) { MessageBox.Show("未设置扫描目录!"); return; } List <string> videoFilePath = ScanHelper.ScanFilePath(scanPaths, scanTypes); foreach (var videoPath in videoFilePath) { // 默认以全路径找 var videoInfo = this._DB.Videos.SingleOrDefault(x => x.PhysicalPath == videoPath); // 如果全路径匹配不到则以文件名找 if (videoInfo == null) { var videoFileName = Path.GetFileName(videoPath); videoInfo = this._DB.Videos.FirstOrDefault(x => x.PhysicalPath.EndsWith(videoFileName)); } if (videoInfo == null) { this._loger.Info(string.Format("在数据库中找不到[{0}]的视频信息,跳过处理。", videoPath)); } //if (videoInfo.IsSkip == true) //{ // this._loger.Info(string.Format("[{0}]该视频在数据库中被标记为[跳过]", videoPath)); // continue; //} videoList.Add(new VideoCoverInfo() { VideoInfo = videoInfo, SrcVideoPath = videoPath }); } } #endregion #region 处理找出来的视频 var imgDir = GetSetting("ImageDir"); var isOverride = (CURRENT_CREATE_SETTING == CREATE_SETTING_OVERRIDE); if (videoList != null && videoList.Count > 0) { foreach (var videoinfo in videoList) { var coverInfo = videoinfo.VideoInfo.Images.SingleOrDefault(x => x.IsCover == true); // 封面为空则取第一图为封面 if (coverInfo == null) { coverInfo = videoinfo.VideoInfo.Images.OrderBy(x => x.OrderCode).FirstOrDefault(); } if (coverInfo == null) { this._loger.Info(string.Format("[{0}]没有设置封面信息或第一图找不到,跳过处理。", videoinfo.SrcVideoPath)); continue; } string videoFileName = Path.GetFileNameWithoutExtension(videoinfo.SrcVideoPath); string videoDir = Path.GetDirectoryName(videoinfo.SrcVideoPath); string coverFullPath = Path.Combine(imgDir, coverInfo.Path); string coverTargetFullPath = Path.Combine(videoDir, videoFileName + "-1.jpg"); // 如果不是覆盖模式并且已经存在封面则跳过 if (!isOverride && File.Exists(coverTargetFullPath)) { this._loger.Info(string.Format("[{0}]封面已存在,跳过处理。", videoinfo.VideoInfo.PhysicalPath)); continue; } if (!File.Exists(coverFullPath)) { this._loger.Info(string.Format("[{0}]没有设置封面信息,跳过处理。", videoinfo.VideoInfo.PhysicalPath)); continue; } // 复制并覆盖封面到指定目录下的封面 File.Copy(coverFullPath, coverTargetFullPath, isOverride); this._loger.Info(string.Format("[{0}]生成封面成功!", coverTargetFullPath)); } } else { this._loger.Info("未找到任何视频信息。"); return; } this._loger.Info("处理完成。"); #endregion }
protected Task <List <string> > ScanPic(string[] paths, IPicFilter[] filters) { var result = ScanHelper.ScanFilePath(paths, new string[] { "*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif" }); int count_success = 0; int count_failed = 0; int count_skip = 0; var changeProcess = new Action <int>((currentValue) => { this.progressBar1.Maximum = result.Count; this.progressBar1.Value = currentValue; var precent = Math.Round((Convert.ToDouble(currentValue) / Convert.ToDouble(this.progressBar1.Maximum)) * 100); this.lbProcess.Text = string.Format("{0}/{1}({2})%", currentValue, this.progressBar1.Maximum, precent); this.lbCount.Text = string.Format("成功:{0} 失败:{1} 跳过:{2}", count_success, count_failed, count_skip); }); for (var i = 0; i < result.Count; i++) { Bitmap bmp = null; bool skip = false; bool error = false; var picPath = result[i]; var currentIndex = i + 1; var pic = new Model.Picture() { CreateDate = DateTime.Now, FileName = System.IO.Path.GetFileName(picPath), RealPath = picPath, Title = System.IO.Path.GetFileNameWithoutExtension(picPath) }; try { // 收集信息 bmp = new Bitmap(picPath); pic.Width = bmp.Width; pic.Height = bmp.Height; bmp.Dispose(); FileInfo fileInfo = new FileInfo(picPath); pic.Size = fileInfo.Length; // 过滤 if (filters != null && filters.Length > 0) { foreach (var f in filters) { var isAllow = f.Filter(pic); if (!isAllow) { skip = true; break; } } } // 判断已存在 skip = this._DB.Picture.Count(x => x.RealPath == pic.RealPath || ( x.FileName == pic.FileName && x.Width == pic.Width && x.Height == pic.Height && x.Size == pic.Size)) > 0; } catch { pic.IsHidden = true; error = true; } finally { // 记录失败、成功、跳过的 // 写入 if (!skip && !error) { this._DB.Picture.Add(pic); count_success++; } else if (error) { count_failed++; } else if (skip) { count_skip++; } if (currentIndex % 1000 == 0) { this._DB.SaveChanges(); } Invoke(changeProcess, currentIndex); if (bmp != null) { bmp.Dispose(); } } } this._DB.SaveChanges(); return(Task.FromResult(result)); }