/// <summary> /// Generate the thumbnail image for this display object and save it to the file system. The routine may decide that /// a file does not need to be generated, usually because it already exists. However, it will always be /// created if the relevant flag is set on the parent <see cref="IGalleryObject" />. (Example: If /// <see cref="IGalleryObject.RegenerateThumbnailOnSave" /> = true, the thumbnail file will always be created.) No data is /// persisted to the data store. /// </summary> public override void GenerateAndSaveFile() { // If necessary, generate and save the thumbnail version of the video. if (!(IsThumbnailImageRequired())) { return; // No thumbnail image required. } IGallerySettings gallerySetting = Factory.LoadGallerySetting(GalleryObject.GalleryId); // Generate a temporary filename to store the thumbnail created by FFmpeg. string tmpVideoThumbnailPath = Path.Combine(AppSetting.Instance.TempUploadDirectory, String.Concat(Guid.NewGuid().ToString(), ".jpg")); // Request that FFmpeg create the thumbnail. If successful, the file will be created. FFmpeg.GenerateThumbnail(GalleryObject.Original.FileNamePhysicalPath, tmpVideoThumbnailPath, gallerySetting.VideoThumbnailPosition, GalleryObject.GalleryId); // Verify image was created from video, trying again using a different video position setting if necessary. ValidateVideoThumbnail(tmpVideoThumbnailPath, gallerySetting.VideoThumbnailPosition); // Determine file name and path of the thumbnail image. If a file name has already been previously // calculated for this media object, re-use it. Otherwise generate a unique name. var newFilename = GalleryObject.Thumbnail.FileName; var newFilePath = GalleryObject.Thumbnail.FileNamePhysicalPath; if (String.IsNullOrEmpty(newFilePath)) { var thumbnailPath = HelperFunctions.MapAlbumDirectoryStructureToAlternateDirectory(this.GalleryObject.Original.FileInfo.DirectoryName, gallerySetting.FullThumbnailPath, gallerySetting.FullMediaObjectPath); newFilename = GenerateJpegFilename(thumbnailPath, gallerySetting.ThumbnailFileNamePrefix); newFilePath = Path.Combine(thumbnailPath, newFilename); } if (File.Exists(tmpVideoThumbnailPath)) { // FFmpeg successfully created a thumbnail image the same size as the video. Now resize it to the width and height we need. var size = ImageHelper.SaveImageFileAsJpeg(tmpVideoThumbnailPath, newFilePath, gallerySetting.MaxThumbnailLength, false, gallerySetting.ThumbnailImageJpegQuality); var rotatedSize = ExecuteAutoRotation(newFilePath, gallerySetting.ThumbnailImageJpegQuality); if (!rotatedSize.IsEmpty) { size = rotatedSize; } GalleryObject.Thumbnail.Width = (int)size.Width; GalleryObject.Thumbnail.Height = (int)size.Height; //using (Bitmap originalBitmap = new Bitmap(tmpVideoThumbnailPath)) //{ // var newSize = CalculateWidthAndHeight(new Size(originalBitmap.Width, originalBitmap.Height), gallerySetting.MaxThumbnailLength, false); // // Get JPEG quality value (0 - 100). This is ignored if imgFormat = GIF. // int jpegQuality = gallerySetting.ThumbnailImageJpegQuality; // // Generate the new image and save to disk. // var size = ImageHelper.SaveImageFile(originalBitmap, newFilePath, ImageFormat.Jpeg, newSize.Width, newSize.Height, jpegQuality); // var rotatedSize = ExecuteAutoRotation(newFilePath, jpegQuality); // if (!rotatedSize.IsEmpty) // { // size = rotatedSize; // } // GalleryObject.Thumbnail.Width = (int)size.Width; // GalleryObject.Thumbnail.Height = (int)size.Height; //} try { // Now delete the thumbnail image created by FFmpeg, but no worries if an error happens. The file is in the temp directory // which is cleaned out each time the app starts anyway. File.Delete(tmpVideoThumbnailPath); } catch (IOException ex) { EventController.RecordError(ex, AppSetting.Instance, GalleryObject.GalleryId, Factory.LoadGallerySettings()); } catch (UnauthorizedAccessException ex) { EventController.RecordError(ex, AppSetting.Instance, GalleryObject.GalleryId, Factory.LoadGallerySettings()); } catch (NotSupportedException ex) { EventController.RecordError(ex, AppSetting.Instance, GalleryObject.GalleryId, Factory.LoadGallerySettings()); } } else { // FFmpeg didn't run or no thumbnail image was created by FFmpeg. Build a generic video thumbnail. var thumbImagePath = Path.Combine(AppSetting.Instance.WebRootPath, GlobalConstants.GenericThumbnailDirectory, GlobalConstants.GenericVideoThumbnailFileName); var size = ImageHelper.SaveImageFileAsJpeg(thumbImagePath, newFilePath, gallerySetting.MaxThumbnailLength, true, gallerySetting.ThumbnailImageJpegQuality); GalleryObject.Thumbnail.Width = size.Width; GalleryObject.Thumbnail.Height = size.Height; //using (var ms = new MemoryStream(Resources.GenericThumbnailImage_Video)) //{ // using (var originalBitmap = new Bitmap(ms)) // { // var newSize = CalculateWidthAndHeight(new Size(originalBitmap.Width, originalBitmap.Height), gallerySetting.MaxThumbnailLength, true); // // Get JPEG quality value (0 - 100). // int jpegQuality = gallerySetting.ThumbnailImageJpegQuality; // // Generate the new image and save to disk. // var size = ImageHelper.SaveImageFile(originalBitmap, newFilePath, ImageFormat.Jpeg, newSize.Width, newSize.Height, jpegQuality); // GalleryObject.Thumbnail.Width = (int)size.Width; // GalleryObject.Thumbnail.Height = (int)size.Height; // } //} } GalleryObject.Thumbnail.FileName = newFilename; GalleryObject.Thumbnail.FileNamePhysicalPath = newFilePath; int fileSize = (int)(GalleryObject.Thumbnail.FileInfo.Length / 1024); GalleryObject.Thumbnail.FileSizeKB = (fileSize < 1 ? 1 : fileSize); // Very small files should be 1, not 0. }