Ejemplo n.º 1
0
        /// <summary>
        /// Generates an image for the media file at the specified <paramref name="sourceFilePath" /> and returns the output from the
        /// execution of the convert utility. The thumbnail is saved to <paramref name="destFilePath" />. The <paramref name="galleryId" />
        /// is used during error handling to associate the error, if any, with the gallery. Requires the application to be running at 
        /// Full Trust and GhostScript to be installed on the server. Returns <see cref="String.Empty" /> when the 
        /// application is running at less than Full Trust or when the convert utility is not present in the bin directory.
        /// </summary>
        /// <param name="sourceFilePath">The full file path to the source media file. Example: D:\media\myfile.eps</param>
        /// <param name="destFilePath">The full file path to store the image to. If a file with this name is already present,
        /// it is overwritten.</param>
        /// <param name="galleryId">The gallery ID.</param>
        /// <returns>Returns the text output from the execution of the convert.exe utility.</returns>
        public static string GenerateImage(string sourceFilePath, string destFilePath, int galleryId)
        {
            string convertOutput = String.Empty;

            if ((AppSetting.Instance.AppTrustLevel != ApplicationTrustLevel.Full) || (String.IsNullOrEmpty(AppSetting.Instance.ImageMagickConvertPath)))
            {
                return convertOutput;
            }

            // Create arguments. The [0] tells it to generate one image from the first page for PDF files (otherwise we get one image for every page)
            // Example: "D:\media\pic.eps[0]" "D:\media\pic.jpg"
            var args = String.Format(CultureInfo.InvariantCulture, @"""{0}[0]"" ""{1}""", sourceFilePath, destFilePath);

            var imageMagick = new ImageMagick();
            convertOutput = imageMagick.ExecuteConvert(args, galleryId);

            if (!String.IsNullOrEmpty(convertOutput))
            {
                // The utility returns an empty string when it is successful, so something went wrong. Log it.
                var ex = new BusinessException(String.Format("ImageMagick (convert.exe) threw an error while trying to generate an image for the file {0}.", sourceFilePath));
                ex.Data.Add("convert.exe output", convertOutput);

                Events.EventController.RecordError(ex, AppSetting.Instance, galleryId, Factory.LoadGallerySettings());
            }

            return convertOutput;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Run the FFmpeg executable with the specified command line arguments.
        /// </summary>
        private void Execute()
        {
            bool processCompletedSuccessfully = false;

            InitializeOutput();

            ProcessStartInfo info = new ProcessStartInfo(!MediaSettings.UseCustomTool ? AppSetting.Instance.FFmpegPath : AppSetting.Instance.FFmpegPath.Replace("ffmpeg.exe","video2mp4.exe"), MediaSettings.FFmpegArgs);
            info.UseShellExecute = false;
            info.CreateNoWindow = true;
            info.RedirectStandardError = true;
            info.RedirectStandardOutput = true;

            using (Process p = new Process())
            {
                try
                {
                    p.StartInfo = info;
                    // For some reason, FFmpeg sends data to the ErrorDataReceived event rather than OutputDataReceived.
                    p.ErrorDataReceived += ErrorDataReceived;
                    //p.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceived);
                    p.Start();

                    //p.BeginOutputReadLine();
                    p.BeginErrorReadLine();

                    processCompletedSuccessfully = p.WaitForExit(MediaSettings.TimeoutMs);

                    if (!processCompletedSuccessfully)
                        p.Kill();

                    p.WaitForExit();

                    if (!processCompletedSuccessfully || MediaSettings.CancellationToken.IsCancellationRequested)
                    {
                        File.Delete(MediaSettings.FilePathDestination);
                    }
                }
                catch (Exception ex)
                {
                    if (!ex.Data.Contains("args"))
                    {
                        ex.Data.Add("args", MediaSettings.FFmpegArgs);
                    }

                    Events.EventController.RecordError(ex, AppSetting.Instance, MediaSettings.GalleryId, Factory.LoadGallerySettings());
                }
            }

            if (!processCompletedSuccessfully)
            {
                Exception ex = new BusinessException(String.Format(CultureInfo.CurrentCulture, "FFmpeg timed out while processing the video or audio file. Consider increasing the timeout value. It is currently set to {0} milliseconds.", MediaSettings.TimeoutMs));
                ex.Data.Add("FFmpeg args", MediaSettings.FFmpegArgs);
                ex.Data.Add("FFmpeg output", Output);
                ex.Data.Add("StackTrace", Environment.StackTrace);
                Events.EventController.RecordError(ex, AppSetting.Instance, MediaSettings.GalleryId, Factory.LoadGallerySettings());
            }
        }