/// <summary>
        /// Performs post-processing tasks on the media object after a video has been rotated. Specifically,
        /// if the file was successfully created, update the media object instance with information
        /// about the new file. No action is taken if <paramref name="settings" /> is null.
        /// </summary>
        /// <param name="settings">An instance of <see cref="MediaConversionSettings" /> containing
        /// settings and results used in the conversion. May be null.</param>
        /// <remarks>This function is invoked only when a video is manually rotated by the user, and
        /// only for the original video file. Videos that are auto-rotated will be the optimized ones
        /// and will end up running the <see cref="OnMediaConversionCompleteOptimizedCreated(MediaConversionSettings)" />
        /// function instead of this one.</remarks>
        private static void OnMediaConversionCompleteVideoRotated(MediaConversionSettings settings)
        {
            if (settings == null)
            {
                return;
            }

            var mediaObject = Factory.LoadMediaObjectInstance(settings.MediaObjectId, true);

            if (settings.FileCreated)
            {
                string msg = String.Format(CultureInfo.CurrentCulture, "FFmpeg created file '{0}'.", Path.GetFileName(settings.FilePathDestination));
                RecordEvent(msg, settings);

                // Step 1: Update the width and height of the original video file, if we have that info.
                var originalWidth  = FFmpeg.ParseOutputVideoWidth(settings.FFmpegOutput);
                var originalHeight = FFmpeg.ParseOutputVideoHeight(settings.FFmpegOutput);

                if (originalWidth > int.MinValue)
                {
                    mediaObject.Original.Width = originalWidth;
                }

                if (originalHeight > int.MinValue)
                {
                    mediaObject.Original.Height = originalHeight;
                }

                // Step 2: Delete the original file and rename the new one to match the original.
                if ((settings.FilePathDestination != mediaObject.Original.FileNamePhysicalPath) && File.Exists(mediaObject.Original.FileNamePhysicalPath))
                {
                    var curFilePath = mediaObject.Original.FileNamePhysicalPath;
                    File.Delete(curFilePath);
                    File.Move(settings.FilePathDestination, curFilePath);
                    settings.FilePathDestination = curFilePath;
                }
                else
                {
                    // I don't expect we'll ever get here, but just to be safe...
                    mediaObject.Original.FileName             = Path.GetFileName(settings.FilePathDestination);
                    mediaObject.Original.FileNamePhysicalPath = settings.FilePathDestination;
                }

                int fileSize = (int)(mediaObject.Original.FileInfo.Length / 1024);
                mediaObject.Original.FileSizeKB = (fileSize < 1 ? 1 : fileSize);                 // Very small files should be 1, not 0.

                RefreshOriginalVideoMetadata(mediaObject);
            }

            // Step 3: Save and finish up.
            mediaObject.LastModifiedByUserName    = GlobalConstants.SystemUserName;
            mediaObject.DateLastModified          = DateTime.Now;
            mediaObject.RegenerateOptimizedOnSave = true;
            mediaObject.RegenerateThumbnailOnSave = true;
            mediaObject.Save();
        }
        /// <summary>
        /// Performs post-processing tasks on the media object after an optimized file has been created. Specifically,
        /// if the file was successfully created, update the media object instance with information
        /// about the new file. No action is taken if <paramref name="settings" /> is null.
        /// </summary>
        /// <param name="settings">An instance of <see cref="MediaConversionSettings" /> containing
        /// settings and results used in the conversion. May be null.</param>
        private static void OnMediaConversionCompleteOptimizedCreated(MediaConversionSettings settings)
        {
            if (settings == null)
            {
                return;
            }

            var mediaObject = Factory.LoadMediaObjectInstance(settings.MediaObjectId, true);

            // Step 1: Update the media object with info about the newly created file.
            if (settings.FileCreated)
            {
                string msg = String.Format(CultureInfo.CurrentCulture, "FFmpeg created file '{0}'.", Path.GetFileName(settings.FilePathDestination));
                RecordEvent(msg, settings);

                if (mediaObject.GalleryObjectType == GalleryObjectType.Video)
                {
                    var width  = FFmpeg.ParseOutputVideoWidth(settings.FFmpegOutput);
                    var height = FFmpeg.ParseOutputVideoHeight(settings.FFmpegOutput);

                    if (width > int.MinValue)
                    {
                        mediaObject.Optimized.Width = width;
                    }

                    if (height > int.MinValue)
                    {
                        mediaObject.Optimized.Height = height;
                    }
                }
                else
                {
                    mediaObject.Optimized.Width  = settings.TargetWidth;
                    mediaObject.Optimized.Height = settings.TargetHeight;
                }

                int fileSize = (int)(mediaObject.Optimized.FileInfo.Length / 1024);
                mediaObject.Optimized.FileSizeKB = (fileSize < 1 ? 1 : fileSize);                 // Very small files should be 1, not 0.

                // Step 2: If we already had an optimized file and we just created a second one, delete the first one
                // and rename the new one to match the first one.
                var optFileDifferentThanOriginal    = !String.Equals(mediaObject.Optimized.FileName, mediaObject.Original.FileName, StringComparison.OrdinalIgnoreCase);
                var optFileDifferentThanCreatedFile = !String.Equals(mediaObject.Optimized.FileName, Path.GetFileName(settings.FilePathDestination), StringComparison.OrdinalIgnoreCase);

                if (optFileDifferentThanOriginal && optFileDifferentThanCreatedFile && File.Exists(mediaObject.Optimized.FileNamePhysicalPath))
                {
                    var curFilePath = mediaObject.Optimized.FileNamePhysicalPath;
                    File.Delete(curFilePath);
                    File.Move(settings.FilePathDestination, curFilePath);
                    settings.FilePathDestination = curFilePath;
                }
                else
                {
                    // We typically get here when the media object is first added.
                    mediaObject.Optimized.FileName             = Path.GetFileName(settings.FilePathDestination);
                    mediaObject.Optimized.FileNamePhysicalPath = settings.FilePathDestination;
                }
            }

            // Step 3: Save and finish up.
            mediaObject.LastModifiedByUserName = GlobalConstants.SystemUserName;
            mediaObject.DateLastModified       = DateTime.Now;
            mediaObject.Save();
        }
        /// <summary>
        /// Performs post-processing tasks on the media object after an optimized file has been created. Specifically,
        /// if the file was successfully created, update the media object instance with information
        /// about the new file. No action is taken if <paramref name="settings" /> is null.
        /// </summary>
        /// <param name="settings">An instance of <see cref="MediaConversionSettings" /> containing
        /// settings and results used in the conversion. May be null.</param>
        private static void OnMediaConversionCompleteOptimizedCreated(MediaConversionSettings settings)
        {
            if (settings == null)
            {
                return;
            }

            var mediaObject = Factory.LoadMediaObjectInstance(settings.MediaObjectId, true);

            // Step 1: Update the media object with info about the newly created file.
            if (settings.FileCreated)
            {
                string msg = String.Format(CultureInfo.CurrentCulture, "FFmpeg created file '{0}'.", Path.GetFileName(settings.FilePathDestination));
                RecordEvent(msg, settings);

                if (mediaObject.GalleryObjectType == GalleryObjectType.Video)
                {
                    var width  = FFmpeg.ParseOutputVideoWidth(settings.FFmpegOutput);
                    var height = FFmpeg.ParseOutputVideoHeight(settings.FFmpegOutput);

                    if (width > int.MinValue)
                    {
                        mediaObject.Optimized.Width = width;
                    }

                    if (height > int.MinValue)
                    {
                        mediaObject.Optimized.Height = height;
                    }
                }
                else
                {
                    mediaObject.Optimized.Width  = settings.TargetWidth;
                    mediaObject.Optimized.Height = settings.TargetHeight;
                }

                // Step 2: If we already had an optimized file and we just created a second one, delete the first one
                // and rename the new one to match the first one.
                var optFileDifferentThanOriginal    = !String.Equals(mediaObject.Optimized.FileName, mediaObject.Original.FileName, StringComparison.InvariantCultureIgnoreCase);
                var optFileDifferentThanCreatedFile = !String.Equals(mediaObject.Optimized.FileName, Path.GetFileName(settings.FilePathDestination), StringComparison.InvariantCultureIgnoreCase);

                if (optFileDifferentThanOriginal && optFileDifferentThanCreatedFile && File.Exists(mediaObject.Optimized.FileNamePhysicalPath))
                {
                    var curFilePath = mediaObject.Optimized.FileNamePhysicalPath;
                    File.Delete(curFilePath);

                    var optFileExtDifferentThanCreatedFileExt = !Path.GetExtension(curFilePath).Equals(Path.GetExtension(settings.FilePathDestination), StringComparison.InvariantCultureIgnoreCase);
                    if (optFileExtDifferentThanCreatedFileExt)
                    {
                        // Extension of created file is different than current optimized file. This can happen, for example, when syncing after
                        // changing encoder settings to produce MP4's instead of FLV's. Use the filename of the current optimized file and combine
                        // it with the extension of the created file.
                        var newOptFileName = String.Concat(Path.GetFileNameWithoutExtension(curFilePath), Path.GetExtension(settings.FilePathDestination));
                        var newOptFilePath = String.Concat(Path.GetDirectoryName(curFilePath), Path.DirectorySeparatorChar, newOptFileName);

                        if (!settings.FilePathDestination.Equals(newOptFilePath, StringComparison.InvariantCultureIgnoreCase))
                        {
                            // Calculated file name differs from the one that was generated, so rename it, deleting any existing file first.
                            if (File.Exists(newOptFilePath))
                            {
                                File.Delete(newOptFilePath);
                            }

                            File.Move(settings.FilePathDestination, newOptFilePath);
                            settings.FilePathDestination = newOptFilePath;
                        }

                        mediaObject.Optimized.FileName             = newOptFileName;
                        mediaObject.Optimized.FileNamePhysicalPath = newOptFilePath;
                    }
                    else
                    {
                        File.Move(settings.FilePathDestination, curFilePath);
                        settings.FilePathDestination = curFilePath;
                    }
                }
                else
                {
                    // We typically get here when the media object is first added.
                    mediaObject.Optimized.FileName             = Path.GetFileName(settings.FilePathDestination);
                    mediaObject.Optimized.FileNamePhysicalPath = settings.FilePathDestination;
                }

                // Now that we have the optimized file name all set, grab it's size.
                int fileSize = (int)(mediaObject.Optimized.FileInfo.Length / 1024);
                mediaObject.Optimized.FileSizeKB = (fileSize < 1 ? 1 : fileSize);                 // Very small files should be 1, not 0.
            }

            // Step 3: Save and finish up.
            mediaObject.LastModifiedByUserName = GlobalConstants.SystemUserName;
            mediaObject.DateLastModified       = DateTime.Now;
            mediaObject.Save();
        }