Exemple #1
0
        public override string GenerateThumbnail()
        {
            // Get temp path where thumbnail should be stored
            string outputPath = GetTempFilename("jpg");

            // Get actual video dimensions
            m_Logger.DebugFormat("Video dimensions are: {0}x{1}", m_Width, m_Height);

            // Get dimensions of the thumbnail to be generated
            Size dimensions = GetAspectImageSize(m_Width, m_Height, ThumbnailSize.Width, ThumbnailSize.Height);

            m_Logger.DebugFormat("Thumbnail will be resized to: {0}x{1}", dimensions.Width, dimensions.Height);

            // Get settings
            string ffmpeg = GetSetting("FFmpegExecutablePath");
            string args   = GetSetting("FFmpegThumbnailArgs");

            // Replace keywords
            args = args.Replace("[INPUT]", InputPath.WrapInQuotes());
            args = args.Replace("[OUTPUT]", outputPath.WrapInQuotes());
            args = args.Replace("[WIDTH]", dimensions.Width.ToString());
            args = args.Replace("[HEIGHT]", dimensions.Height.ToString());

            // Execute FFmpeg command line
            CommandLineExecuter.Execute(ffmpeg, args);

            return(outputPath);
        }
        public override string GeneratePreview()
        {
            // Get the preview file extension, assume same as input file
            string extension = (Path.GetExtension(InputPath) ?? string.Empty).ToLower();

            // Only WAV and MP3 are allowed, so default to MP3
            // if the input file is neither of these.
            extension = GetExtensionGenerated(extension);

            // Get the temp output path where generated file should be saved
            string outputPath = GetTempFilename(extension);

            // Get settings
            string ffmpeg   = GetSetting("FFmpegExecutablePath");
            int    duration = GetSetting("CropDuration", 0);

            if (duration == 0)
            {
                File.Copy(InputPath, outputPath, true);
            }
            else
            {
                string args = string.Format("-t {2} -acodec copy -i \"{0}\" \"{1}\"", InputPath, outputPath, duration);
                CommandLineExecuter.Execute(ffmpeg, args);
            }

            return(outputPath);
        }
Exemple #3
0
        private void ResizeImage(string inputFile, string outputFile, int width, int height, bool keepAspectRatio)
        {
            // Paths to command line utils required
            string colorspaceProfileCMYKPath = Settings.GetValue("ColorspaceProfileCMYK");
            string colorspaceProfileRGBPath  = Settings.GetValue("ColorspaceProfileRGB");
            string imageMagickConvertPath    = Settings.GetValue("ImageMagickConvert");

            // Get the image colorspace (CMYK or RGB)
            string colorspace = GetImageColorspace(inputFile);

            // Assume image is RGB
            string arguments = "-colorspace RGB -quality 90 -resize " + width + "x" + height + (keepAspectRatio ? " " : "! ") + "\"" + inputFile + "\"[0] \"" + outputFile + "\"";

            // Unless the colorspace returned CMYK
            if (colorspace.Contains("CMYK"))
            {
                arguments = " +profile icm -profile " + colorspaceProfileCMYKPath.WrapInQuotes() + " -profile " + colorspaceProfileRGBPath.WrapInQuotes() + " -quality 90 -resize " + width + "x" + height + (keepAspectRatio ? " " : "! ") + "\"" + inputFile + "\"[0] \"" + outputFile + "\"";
            }

            // Initialize exit code
            int exitCode;

            // Execute the command line
            CommandLineExecuter.Execute(imageMagickConvertPath, arguments, out exitCode);

            // Check exit code
            if (exitCode != 0)
            {
                throw new SystemException(string.Format("Error resizing image: {0}.  Exit code: {1}", inputFile, exitCode));
            }
        }
Exemple #4
0
        private string GetImageColorspace(string imagePath)
        {
            try
            {
                string imageMagickIdentifyPath = Settings.GetValue("ImageMagickIdentify");

                int    exitCode;
                string args = string.Format("-format %r \"{0}\"", imagePath);

                string output = CommandLineExecuter.Execute(imageMagickIdentifyPath, args, out exitCode);

                if (exitCode == 0)
                {
                    return(output);
                }

                m_Logger.WarnFormat("ImageMagickIdentify Unable to identify image colorspace. Exited with code: {0}", exitCode);
            }
            catch (Exception ex)
            {
                m_Logger.Warn(string.Format("Error getting colorspace for {0}.  Error: {1}", imagePath, ex.Message), ex);
            }

            return("RGB");
        }
Exemple #5
0
        private Size GetDotsPerInch(string imagePath)
        {
            try
            {
                string imageMagickIdentifyPath = Settings.GetValue("ImageMagickIdentify");

                int exitCode;

                string args   = string.Format("-format \"%[fx:resolution.x]x%[fx:resolution.y]\" \"{0}\"", imagePath);
                string output = CommandLineExecuter.Execute(imageMagickIdentifyPath, args, out exitCode).Trim();

                const string pattern = @"(?<X>\d.+)x(?<Y>\d.+)$";
                Match        match   = Regex.Match(output, pattern);

                if (match.Success)
                {
                    int x = Convert.ToInt32(match.Groups["X"].Value);
                    int y = Convert.ToInt32(match.Groups["Y"].Value);

                    m_Logger.DebugFormat("Got DPI from file: {0}.  X:{1}, Y:{2}", imagePath, x, y);

                    return(new Size(x, y));
                }

                m_Logger.WarnFormat("Unable to get DPI from file: {0}.  Output not parsed: {1}", imagePath, output);

                return(Size.Empty);
            }
            catch (Exception ex)
            {
                m_Logger.WarnFormat("Errorgetting DPI from file: {0}.  Error: {1}", imagePath, ex.Message);
                return(Size.Empty);
            }
        }
        /// <summary>
        /// Executes the command line and gets the response and exit code
        /// </summary>
        public static string Execute(string app, string args, out int exitCode)
        {
            CommandLineExecuter cle = new CommandLineExecuter {
                ApplicationPath = app, Arguments = args
            };

            cle.Go();
            exitCode = cle.ExitCode;
            return(cle.Output);
        }
Exemple #7
0
        private void GetVideoData()
        {
            int width    = 0;
            int height   = 0;
            int duration = 0;

            string ffmpeg = GetSetting("FFmpegExecutablePath");
            string args   = string.Format("-i \"{0}\"", InputPath);

            string output = CommandLineExecuter.Execute(ffmpeg, args);

            Regex re    = new Regex(@" (?<width>[0-9\.]{1,4})x(?<height>[0-9\.]{1,4})");
            Match match = re.Match(output);

            if (match.Success)
            {
                width  = Convert.ToInt32(match.Groups["width"].Value);
                height = Convert.ToInt32(match.Groups["height"].Value);
            }
            else
            {
                m_Logger.WarnFormat("Unable to get video dimensions from FFmpeg output.  Output content length: {0}. Content: {1}", output.Length, output);
            }

            re    = new Regex(@"Duration:\s(?<hours>\d+):(?<minutes>\d+):(?<seconds>\d+\.\d+)");
            match = re.Match(output);

            if (match.Success)
            {
                decimal hours   = Decimal.Parse(match.Groups["hours"].Value);
                decimal minutes = Decimal.Parse(match.Groups["minutes"].Value);
                decimal seconds = Decimal.Parse(match.Groups["seconds"].Value);

                seconds += (hours * 60 * 60);
                seconds += (minutes * 60);

                duration = Convert.ToInt32(Math.Round(seconds));
            }
            else
            {
                m_Logger.WarnFormat("Unable to get video duration from FFmpeg output.  Output content length: {0}. Content: {1}", output.Length, output);
            }

            m_Height = height;
            m_Width  = width;

            FileDataItems["Height"]   = height;
            FileDataItems["Width"]    = width;
            FileDataItems["Duration"] = duration;

            m_Logger.DebugFormat("Got video metadata - height: {0}, width: {1}, duration: {2} seconds", height, width, duration);
        }
Exemple #8
0
        private void ApplyWatermark(string imagePath, string watermarkImagePath)
        {
            int    exitCode;
            string imageMagickCompositePath = Settings.GetValue("ImageMagickComposite");
            string args = "-dissolve 30 -gravity center \"" + watermarkImagePath + "\" \"" + imagePath + "\" \"" + imagePath + "\"";

            CommandLineExecuter.Execute(imageMagickCompositePath, args, out exitCode);

            if (exitCode != 0)
            {
                throw new SystemException("Error stamping image. ImageMagickComposite exited with code: " + exitCode);
            }
        }
Exemple #9
0
        public override string GeneratePreview()
        {
            string extension = (Path.GetExtension(InputPath) ?? string.Empty).ToLower();

            // Get temp path where FLV should be stored
            string outputPath = GetTempFilename(GetExtensionGenerated(extension));

            // Get settings
            string ffmpeg = GetSetting("FFmpegExecutablePath");
            string args   = GetSetting("FFmpegPreviewArgs");

            // Replace keywords
            args = args.Replace("[INPUT]", InputPath.WrapInQuotes());
            args = args.Replace("[OUTPUT]", outputPath.WrapInQuotes());
            args = args.Replace("[WIDTH]", PreviewSize.Width.ToString());
            args = args.Replace("[HEIGHT]", PreviewSize.Height.ToString());

            // Apply watermark
            if (!String.IsNullOrEmpty(WatermarkPath) && File.Exists(WatermarkPath))
            {
                string watermarkArgs = GetSetting("FFmpegWatermarkArgs");

                if (String.IsNullOrEmpty(watermarkArgs))
                {
                    m_Logger.WarnFormat("Unable to apply watermark; no command arguments specified");
                }
                else
                {
                    watermarkArgs = watermarkArgs.Replace("[WATERMARK]", WatermarkPath.WrapInQuotes());
                    args          = args.Replace("[WATERMARK-ARGS]", watermarkArgs);
                }
            }

            // Remove watermark args placeholder, in case no watermark was requested or file not found
            args = args.Replace("[WATERMARK-ARGS]", string.Empty);

            // Execute FFmpeg command line
            CommandLineExecuter.Execute(ffmpeg, args);

            // Update FLV metadata (fixes duration & seek problems)
            string flvtool     = GetSetting("FLVToolExecutablePath");
            string flvtoolargs = string.Format("-U {0}", outputPath);

            CommandLineExecuter.Execute(flvtool, flvtoolargs);

            return(outputPath);
        }
        private string ExecuteCommandLine(string setting, Size size)
        {
            // Get command line app settings
            string commandLineApp  = GetSetting(setting + "CommandLine_App");
            string commandLineArgs = GetSetting(setting + "CommandLine_Args");
            string extension       = GetSetting(setting + "_Extension");

            // Get input and output paths
            string inputPath  = InputPath;
            string outputPath = GetTempFilename(extension);

            // Replace command line arg placeholders with actual values
            commandLineArgs = commandLineArgs.Replace("[INPUT]", inputPath.WrapInQuotes());
            commandLineArgs = commandLineArgs.Replace("[OUTPUT]", outputPath.WrapInQuotes());
            commandLineArgs = commandLineArgs.Replace("[WIDTH]", size.Width.ToString());
            commandLineArgs = commandLineArgs.Replace("[HEIGHT]", size.Height.ToString());

            // Execute command line
            CommandLineExecuter.Execute(commandLineApp, commandLineArgs);

            return(outputPath);
        }