private static void CheckIfOptionsAreNotNull(TrailerClipperOptions options)
 {
     if (options == null)
     {
         throw new ArgumentNullException(nameof(options));
     }
 }
        /// <summary>
        ///     Execute the removal of trailers and/or intros as specificed in the options
        /// </summary>
        /// <param name="options">Contains the various clipping options and target paths.  Cannot be null</param>
        public void RemoveTrailers(TrailerClipperOptions options)
        {
            CheckIfOptionsAreNotNull(options);

            var optionList = new[] { options };

            RemoveTrailers(optionList, false);
        }
        public void ExecuteSingleFileMode(TrailerClipperOptions options)
        {
            var singleFile = options.InputPath;

            if (string.IsNullOrWhiteSpace(singleFile))
                throw new ArgumentOutOfRangeException(nameof(options), " file name: " + singleFile + " is invalid.");

            RemoveTrailerFromSingleFile(singleFile, options);
        }
        public void ExecuteSingleFileMode(TrailerClipperOptions options)
        {
            var singleFile = options.InputPath;

            if (string.IsNullOrWhiteSpace(singleFile))
                throw new ArgumentOutOfRangeException(nameof(options), " file name: " + singleFile + " is invalid.");

            RemoveTrailerFromSingleFile(singleFile, options);
        }
        private static void InitializeIntroRemovalOptions(string[] args, int iii, TrailerClipperOptions options)
        {
            decimal introLengthInMilliseconds;

            if (!decimal.TryParse(args[iii], out introLengthInMilliseconds))
            {
                throw new ArgumentOutOfRangeException(nameof(args));
            }

            options.IntroLengthInMilliseconds = introLengthInMilliseconds;
        }
        public void ExecuteDirectoryMode(TrailerClipperOptions options)
        {
            var directoryPath = options.InputPath;

            var files = Directory.GetFiles(directoryPath);

            if (options.MultiTaskFiles)
                Parallel.ForEach(files, filePath => RemoveTrailerFromSingleFile(filePath, options));
            else
                foreach (var filePath in files)
                    RemoveTrailerFromSingleFile(filePath, options);
        }
        public void ExecuteDirectoryMode(TrailerClipperOptions options)
        {
            var directoryPath = options.InputPath;

            var files = Directory.GetFiles(directoryPath);

            if (options.MultiTaskFiles)
                Parallel.ForEach(files, filePath => RemoveTrailerFromSingleFile(filePath, options));
            else
                foreach (var filePath in files)
                    RemoveTrailerFromSingleFile(filePath, options);
        }
        public void RemoveIntrosAndTrailers(string path, decimal introLenghtInMilliseconds, decimal trailerLenghtInMilliseconds)
        {
            CheckPathForValidity(path);

            var options = new TrailerClipperOptions(path)
            {
                RemoveIntro = true,
                IntroLengthInMilliseconds   = introLenghtInMilliseconds,
                TrailerLengthInMilliSeconds = trailerLenghtInMilliseconds
            };

            RemoveTrailers(options);
        }
        private static TrailerClipperOptions ProcessInputOptions(string[] args)
        {
            var options = new TrailerClipperOptions();

            for (var iii = 0; iii < args.Length; iii++)
            {
                var currentArg = args[iii].Trim().ToLower();

                if (!currentArg.StartsWith("-"))
                {
                    continue;
                }

                switch (currentArg)
                {
                case "-d":
                    options.DeleteOriginalFiles = true;
                    break;

                case "-i":
                case "-intro":
                    options.RemoveIntro = true;
                    iii++;
                    if (args.Length <= iii)
                    {
                        break;
                    }

                    InitializeIntroRemovalOptions(args, iii, options);
                    break;

                case "-m":
                case "-multi":
                    options.MultiTaskFiles = true;
                    break;

                case "-cf":
                case "-consoleoff":
                    options.OutputToConsole = false;
                    break;

                case "-a":
                case "-allfiles":
                    options.ProcessEveryFile = true;
                    break;
                }
            }

            return(options);
        }
        public void RemoveIntros(string path, decimal introLenghtInMilliseconds)
        {
            CheckPathForValidity(path);

            if (introLenghtInMilliseconds < 0)
                throw new ArgumentOutOfRangeException(nameof(introLenghtInMilliseconds), "Intro length must be greater than zero, otherwise there is nothing to clip");

            var options = new TrailerClipperOptions(path)
            {
                RemoveIntro = true,
                IntroLengthInMilliseconds = introLenghtInMilliseconds
            };

            RemoveTrailers(options);
        }
        private void RemoveTrailerFromSingleFile(string filePath, TrailerClipperOptions options)
        {
            if (!options.ProcessEveryFile && IsNotAValidMediaFile(filePath))
                return;

            var durationInMilliseconds = GetDurationOfMediaFile(filePath);

            var newDuration = durationInMilliseconds - options.TrailerLengthInMilliSeconds;

            if (options.OutputToConsole)
                Console.WriteLine("Starting on file: " + filePath);

            TrimFileToNewDuration(filePath, newDuration, options);
            
            if(options.DeleteOriginalFiles)
                File.Delete(filePath);
        }
        public void RemoveIntros(string path, decimal introLenghtInMilliseconds)
        {
            CheckPathForValidity(path);


            if (introLenghtInMilliseconds < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(introLenghtInMilliseconds), "Intro length must be greater than zero, otherwise there is nothing to clip");
            }

            var options = new TrailerClipperOptions(path)
            {
                RemoveIntro = true,
                IntroLengthInMilliseconds = introLenghtInMilliseconds
            };


            RemoveTrailers(options);
        }
        private static ConversionOptions InitializeClippingData(decimal newDurationInMilliseconds, TrailerClipperOptions clipperOptions)
        {
            var options = new ConversionOptions();

            var newDuration = Convert.ToDouble(newDurationInMilliseconds);

            var newDurationTimeSpan = TimeSpan.FromMilliseconds(newDuration);

            var seekToPosition = TimeSpan.Zero;

            if (clipperOptions.RemoveIntro)
            {
                var introLength = Convert.ToDouble(clipperOptions.IntroLengthInMilliseconds);

                seekToPosition = TimeSpan.FromMilliseconds(introLength);
            }

            var lengthSpan = newDurationTimeSpan.Subtract(seekToPosition);

            options.CutMedia(seekToPosition, lengthSpan);

            return options;
        }
        public void RemoveTrailers(string directoryPath, decimal milliseconds)
        {
            var options = new TrailerClipperOptions(directoryPath, milliseconds);

            RemoveTrailers(options);
        }
        /// <summary>
        ///     Execute the removal of trailers and/or intros as specificed in the options
        /// </summary>
        /// <param name="options">Contains the various clipping options and target paths.  Cannot be null</param>
        public void RemoveTrailers(TrailerClipperOptions options)
        {
            CheckIfOptionsAreNotNull(options);

            var optionList = new[] {options};

            RemoveTrailers(optionList, false);
        }
        private void RemoveTrailerFromSingleFile(string filePath, TrailerClipperOptions options)
        {
            if (!options.ProcessEveryFile && IsNotAValidMediaFile(filePath))
                return;

            var durationInMilliseconds = GetDurationOfMediaFile(filePath);

            var newDuration = durationInMilliseconds - options.TrailerLengthInMilliSeconds;

            if (options.OutputToConsole)
                Console.WriteLine("Starting on file: " + filePath);

            TrimFileToNewDuration(filePath, newDuration, options);

            if(options.DeleteOriginalFiles)
                File.Delete(filePath);
        }
        private static ConversionOptions InitializeClippingData(decimal newDurationInMilliseconds, TrailerClipperOptions clipperOptions)
        {
            var options = new ConversionOptions();

            var newDuration = Convert.ToDouble(newDurationInMilliseconds);

            var newDurationTimeSpan = TimeSpan.FromMilliseconds(newDuration);

            var seekToPosition = TimeSpan.Zero;

            if (clipperOptions.RemoveIntro)
            {
                var introLength = Convert.ToDouble(clipperOptions.IntroLengthInMilliseconds);

                seekToPosition = TimeSpan.FromMilliseconds(introLength);
            }

            var lengthSpan = newDurationTimeSpan.Subtract(seekToPosition);

            options.CutMedia(seekToPosition, lengthSpan);

            return options;
        }
        private static void TrimFileToNewDuration(string inputFilePath, decimal newDurationInMilliseconds, TrailerClipperOptions clipperOptions)
        {
            var outputFilePath = ComputeOutputFilePath(inputFilePath, clipperOptions.OutputDirectoryPath);

            var inputFile = new MediaFile {Filename = inputFilePath};

            var outputFile = new MediaFile {Filename = outputFilePath};

            var options = InitializeClippingData(newDurationInMilliseconds, clipperOptions);

            using (var engine = new Engine())
            {
                engine.GetMetadata(inputFile);

                engine.Convert(inputFile, outputFile, options);
            }

            if (clipperOptions.OutputToConsole)
                Console.WriteLine("Finished on trimming file, output: " + outputFilePath);
        }
        public void RemoveTrailers(string directoryPath, decimal milliseconds)
        {
            var options = new TrailerClipperOptions(directoryPath, milliseconds);

            RemoveTrailers(options);
        }
        private static TrailerClipperOptions ProcessInputOptions(string[] args)
        {
            var options = new TrailerClipperOptions();

            for (var iii = 0; iii < args.Length; iii++)
            {
                var currentArg = args[iii].Trim().ToLower();

                if (!currentArg.StartsWith("-"))
                    continue;

                switch (currentArg)
                {
                    case "-d":
                        options.DeleteOriginalFiles = true;
                        break;
                    case "-i":
                    case "-intro":
                        options.RemoveIntro = true;
                        iii++;
                        if (args.Length <= iii)
                            break;

                        InitializeIntroRemovalOptions(args, iii, options);
                        break;
                    case "-m":
                    case "-multi":
                        options.MultiTaskFiles = true;
                        break;
                    case "-cf":
                    case "-consoleoff":
                        options.OutputToConsole = false;
                        break;
                    case "-a":
                    case "-allfiles":
                        options.ProcessEveryFile = true;
                        break;
                }
            }

            return options;
        }
 private static void CheckIfOptionsAreNotNull(TrailerClipperOptions options)
 {
     if (options == null)
         throw new ArgumentNullException(nameof(options));
 }
        private static void TrimFileToNewDuration(string inputFilePath, decimal newDurationInMilliseconds, TrailerClipperOptions clipperOptions)
        {
            var outputFilePath = ComputeOutputFilePath(inputFilePath, clipperOptions.OutputDirectoryPath);

            var inputFile = new MediaFile {Filename = inputFilePath};

            var outputFile = new MediaFile {Filename = outputFilePath};

            var options = InitializeClippingData(newDurationInMilliseconds, clipperOptions);

            using (var engine = new Engine())
            {
                engine.GetMetadata(inputFile);

                engine.Convert(inputFile, outputFile, options);
            }

            if (clipperOptions.OutputToConsole)
                Console.WriteLine("Finished on trimming file, output: " + outputFilePath);
        }
        public void RemoveIntrosAndTrailers(string path, decimal introLenghtInMilliseconds, decimal trailerLenghtInMilliseconds)
        {
            CheckPathForValidity(path);

            var options = new TrailerClipperOptions(path)
            {
                RemoveIntro = true,
                IntroLengthInMilliseconds = introLenghtInMilliseconds,
                TrailerLengthInMilliSeconds = trailerLenghtInMilliseconds
            };

            RemoveTrailers(options);
        }
        private static void InitializeIntroRemovalOptions(string[] args, int iii, TrailerClipperOptions options)
        {
            decimal introLengthInMilliseconds;

            if (!decimal.TryParse(args[iii], out introLengthInMilliseconds))
            {
                throw new ArgumentOutOfRangeException(nameof(args));
            }

            options.IntroLengthInMilliseconds = introLengthInMilliseconds;
        }