protected override void Initialize()
        {
            base.Initialize();

            if (this.State == ConversionState.Failed)
            {
                return;
            }

            if (this.ConversionPreset == null)
            {
                throw new System.Exception("The conversion preset must be valid.");
            }

            // Initialize converters.
            if (this.ConversionPreset.OutputType == OutputType.Pdf)
            {
                this.intermediateFilePath = this.OutputFilePath;
            }
            else
            {
                // Generate intermediate file path.
                string fileName = Path.GetFileNameWithoutExtension(this.InputFilePath);
                string tempPath = Path.GetTempPath();
                this.intermediateFilePath = PathHelpers.GenerateUniquePath(tempPath + fileName + ".pdf");

                ConversionPreset intermediatePreset = new ConversionPreset("Pdf to image", this.ConversionPreset, "pdf");
                this.pdf2ImageConversionJob = ConversionJobFactory.Create(intermediatePreset, this.intermediateFilePath);
                this.pdf2ImageConversionJob.PrepareConversion(this.OutputFilePaths);
            }
        }
        protected override void Initialize()
        {
            base.Initialize();

            if (this.ConversionPreset == null)
            {
                throw new Exception("The conversion preset must be valid.");
            }

            // Generate intermediate file path.
            string fileName = Path.GetFileName(this.OutputFilePath);
            string tempPath = Path.GetTempPath();

            this.intermediateFilePath = PathHelpers.GenerateUniquePath(tempPath + fileName + ".png");

            // Convert input in png file to send it to ffmpeg for the ico conversion.
            ConversionPreset intermediatePreset = new ConversionPreset("To compatible image", OutputType.Png, this.ConversionPreset.InputTypes.ToArray());

            intermediatePreset.SetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2, "True");
            intermediatePreset.SetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageMaximumSize, "256");
            this.pngConversionJob = ConversionJobFactory.Create(intermediatePreset, this.InputFilePath);
            this.pngConversionJob.PrepareConversion(this.intermediateFilePath);

            // Convert png file into ico.
            this.icoConversionJob = new ConversionJob_FFMPEG(this.ConversionPreset, this.intermediateFilePath);
            this.icoConversionJob.PrepareConversion(this.OutputFilePath);
        }
Ejemplo n.º 3
0
        static VideoSizeHelper()
        {
            AllSizes = new ReadOnlyDictionary <string, VideoSize>(EnumHelper <VideoSizeEnum> .GetValues()
                                                                  .Where(e => e > VideoSizeEnum.Automatic)
                                                                  .Reverse()
                                                                  .Select(v => new VideoSize(v))
                                                                  .ToDictionary(v => v.DimensionsString, StringComparer.OrdinalIgnoreCase));

            IList <VideoSizeEnum> sizes = new List <VideoSizeEnum>();
            string tmpConfig            = ConfigurationManager.AppSettings[KEY_ALLOWED_VIDEO_CONVERSIONS]?.Trim();

            if (!string.IsNullOrEmpty(tmpConfig))
            {
                string[] parts = tmpConfig.Split(',');

                foreach (string t in parts)
                {
                    string part = t.Trim();
                    if (string.IsNullOrEmpty(part))
                    {
                        continue;
                    }

                    if (Enum.TryParse(part, true, out VideoSizeEnum sizeName))
                    {
                        sizes.Add(sizeName);
                    }
                    else if (AllSizes.TryGetValue(part, out VideoSize videoSize))
                    {
                        sizes.Add(videoSize.Value);
                    }
                }
            }

            if (sizes.Count == 0)
            {
                sizes.Add(VideoSizeEnum.FWQVGA);             //240
                sizes.Add(VideoSizeEnum.nHD);                //360p
            }

            SetEnabledSizes(sizes.Distinct().ToArray());

            tmpConfig = ConfigurationManager.AppSettings[KEY_CONVERSION_RESTRICTION]?.Trim();
            if (string.IsNullOrEmpty(tmpConfig) || !Enum.TryParse(tmpConfig, true, out __conversionSearchRestriction))
            {
                __conversionSearchRestriction = ConversionSearchRestriction.None;
            }

            tmpConfig = ConfigurationManager.AppSettings[KEY_CONVERSION_PRESET]?.Trim();
            if (string.IsNullOrEmpty(tmpConfig) || !Enum.TryParse(tmpConfig, true, out __conversionPreset))
            {
                __conversionPreset = ConversionPreset.slow;
            }

            tmpConfig    = ConfigurationManager.AppSettings[KEY_MAX_THREADS]?.Trim();
            __maxThreads = tmpConfig.To(0).Within(-1, byte.MaxValue);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes an instance of <see cref="ConversionRequest"/>.
 /// </summary>
 public ConversionRequest(
     string ffmpegCliFilePath,
     string outputFilePath,
     ConversionFormat format,
     ConversionPreset preset)
 {
     FFmpegCliFilePath = ffmpegCliFilePath;
     OutputFilePath    = outputFilePath;
     Format            = format;
     Preset            = preset;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Compute the argument needed to change the number of channels in an audio file.
        /// </summary>
        /// <param name="conversionPreset">The conversion preset</param>
        /// <returns>The argument string.</returns>
        /// https://trac.ffmpeg.org/wiki/AudioChannelManipulation
        private static string ComputeAudioChannelArgs(ConversionPreset conversionPreset)
        {
            string channelArgs  = string.Empty;
            int    channelCount = conversionPreset.GetSettingsValue <int>(ConversionPreset.ConversionSettingKeys.AudioChannelCount);

            if (channelCount > 0)
            {
                channelArgs = $"-ac {channelCount}";
            }

            return(channelArgs);
        }
        public static ConversionJob Create(ConversionPreset conversionPreset, string inputFilePath)
        {
            string inputFileExtension = System.IO.Path.GetExtension(inputFilePath);

            inputFileExtension = inputFileExtension.ToLowerInvariant().Substring(1, inputFileExtension.Length - 1);
            if (inputFileExtension == "cda")
            {
                return(new ConversionJob_ExtractCDA(conversionPreset, inputFilePath));
            }

            if (inputFileExtension == "docx" || inputFileExtension == "odt" || inputFileExtension == "doc")
            {
                return(new ConversionJob_Word(conversionPreset, inputFilePath));
            }

            if (inputFileExtension == "xlsx" || inputFileExtension == "ods" || inputFileExtension == "xls")
            {
                return(new ConversionJob_Excel(conversionPreset, inputFilePath));
            }

            if (inputFileExtension == "pptx" || inputFileExtension == "odp" || inputFileExtension == "ppt")
            {
                return(new ConversionJob_PowerPoint(conversionPreset, inputFilePath));
            }

            if (conversionPreset.OutputType == OutputType.Ico)
            {
                return(new ConversionJob_Ico(conversionPreset, inputFilePath));
            }

            if (conversionPreset.OutputType == OutputType.Gif)
            {
                return(new ConversionJob_Gif(conversionPreset, inputFilePath));
            }

            if (conversionPreset.OutputType == OutputType.Pdf)
            {
                return(new ConversionJob_ImageMagick(conversionPreset, inputFilePath));
            }

            if (Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Image ||
                Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Document)
            {
                return(new ConversionJob_ImageMagick(conversionPreset, inputFilePath));
            }

            return(new ConversionJob_FFMPEG(conversionPreset, inputFilePath));
        }
Ejemplo n.º 7
0
        public ConversionJob(ConversionPreset conversionPreset, string inputFilePath) : this()
        {
            if (conversionPreset == null)
            {
                throw new ArgumentNullException(nameof(conversionPreset));
            }

            if (string.IsNullOrEmpty(inputFilePath))
            {
                throw new ArgumentNullException(nameof(inputFilePath));
            }

            this.initialInputPath = inputFilePath;
            this.InputFilePath    = inputFilePath;
            this.ConversionPreset = conversionPreset;
            this.UserState        = Properties.Resources.ConversionStatePrepareConversion;
        }
Ejemplo n.º 8
0
        protected override void Initialize()
        {
            base.Initialize();

            if (this.ConversionPreset == null)
            {
                throw new Exception("The conversion preset must be valid.");
            }

            string extension = System.IO.Path.GetExtension(this.InputFilePath);

            extension = extension.ToLowerInvariant().Substring(1, extension.Length - 1);

            string inputFilePath = string.Empty;

            // If the output is an image start to convert it into png before send it to ffmpeg.
            if (Helpers.GetExtensionCategory(extension) == Helpers.InputCategoryNames.Image && extension != "png")
            {
                // Generate intermediate file path.
                string fileName = Path.GetFileName(this.OutputFilePath);
                string tempPath = Path.GetTempPath();
                this.intermediateFilePath = PathHelpers.GenerateUniquePath(tempPath + fileName + ".png");

                // Convert input in png file to send it to ffmpeg for the gif conversion.
                ConversionPreset intermediatePreset = new ConversionPreset("To compatible image", OutputType.Png, this.ConversionPreset.InputTypes.ToArray());
                this.pngConversionJob = ConversionJobFactory.Create(intermediatePreset, this.InputFilePath);
                this.pngConversionJob.PrepareConversion(this.intermediateFilePath);

                inputFilePath = this.intermediateFilePath;
            }
            else
            {
                inputFilePath = this.InputFilePath;
            }

            // Convert png file into ico.
            this.gifConversionJob = new ConversionJob_FFMPEG(this.ConversionPreset, inputFilePath);
            this.gifConversionJob.PrepareConversion(this.OutputFilePath);
        }
Ejemplo n.º 9
0
 /// <inheritdoc />
 public IConversion SetPreset(ConversionPreset preset)
 {
     _preset = $"-preset {preset.ToString().ToLower()} ";
     return(this);
 }
Ejemplo n.º 10
0
 public Converter(VideoClient videoClient, FFmpeg ffmpeg, ConversionPreset preset)
 {
     _videoClient = videoClient;
     _ffmpeg      = ffmpeg;
     _preset      = preset;
 }
Ejemplo n.º 11
0
 public ConversionJob_ImageMagick(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
 {
     this.IsCancelable = true;
 }
 public ConversionJob_Excel(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
 {
 }
 public ConversionJob_PowerPoint(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
 {
 }
Ejemplo n.º 14
0
 /// <inheritdoc />
 public IConversion SetPreset(ConversionPreset preset)
 {
     _parameters.Add(new ConversionParameter($"-preset {preset.ToString().ToLower()}", ParameterPosition.PostInput));
     return(this);
 }
 protected ConversionJob_Office(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
 {
 }
 public ConversionJob_ExtractCDA(ConversionPreset conversionPreset, string inputFilePath) : base(conversionPreset, inputFilePath)
 {
     this.IsCancelable = true;
 }
Ejemplo n.º 17
0
        private static string ComputeTransformArgs(ConversionPreset conversionPreset)
        {
            float  scaleFactor = conversionPreset.GetSettingsValue <float>(ConversionPreset.ConversionSettingKeys.VideoScale);
            string scaleArgs   = string.Empty;

            if (conversionPreset.OutputType == OutputType.Mkv || conversionPreset.OutputType == OutputType.Mp4)
            {
                // This presets use h264 codec, the size of the video need to be divisible by 2.
                scaleArgs = string.Format("scale=trunc(iw*{0}/2)*2:trunc(ih*{0}/2)*2", scaleFactor.ToString("#.##", CultureInfo.InvariantCulture));
            }
            else if (Math.Abs(scaleFactor - 1f) >= 0.005f)
            {
                scaleArgs = string.Format("scale=iw*{0}:ih*{0}", scaleFactor.ToString("#.##", CultureInfo.InvariantCulture));
            }

            float  rotationAngleInDegrees = conversionPreset.GetSettingsValue <float>(ConversionPreset.ConversionSettingKeys.VideoRotation);
            string rotationArgs           = string.Empty;

            if (Math.Abs(rotationAngleInDegrees - 0f) >= 0.05f)
            {
                // Transpose:
                // 0: 90 CounterClockwise and vertical flip
                // 1: 90 Clockwise
                // 2: 90 CounterClockwise
                // 3: 90 Clockwise and vertical flip
                if (Math.Abs(rotationAngleInDegrees - 90f) <= 0.05f)
                {
                    rotationArgs = "transpose=2";
                }
                else if (Math.Abs(rotationAngleInDegrees - 180f) <= 0.05f)
                {
                    rotationArgs = "vflip,hflip";
                }
                else if (Math.Abs(rotationAngleInDegrees - 270f) <= 0.05f)
                {
                    rotationArgs = "transpose=1";
                }
                else
                {
                    Diagnostics.Debug.LogError("Unsupported rotation: {0}°", rotationAngleInDegrees);
                }
            }

            // Build vf args content (scale=..., transpose=...)
            string transformArgs = string.Empty;

            if (!string.IsNullOrEmpty(scaleArgs))
            {
                transformArgs += scaleArgs;
            }

            if (!string.IsNullOrEmpty(rotationArgs))
            {
                if (!string.IsNullOrEmpty(transformArgs))
                {
                    transformArgs += ",";
                }

                transformArgs += rotationArgs;
            }

            return(transformArgs);
        }