Animatroller.PostProcessor.TrimBlack.Execute C# (CSharp) Method

Execute() public method

public Execute ( ) : void
return void
        public void Execute()
        {
            long? timestampOffset = null;

            var positions = new List<long>();
            var blackPositions = new HashSet<long>();

            int inputFrameCount = 0;
            int outputFrameCount = 0;

            // Read until we have all starting points
            while (this.fileReader.DataAvailable)
            {
                long pos = this.fileReader.Position;

                var data = this.fileReader.ReadFrame();
                inputFrameCount++;

                if (data.DataType == Common.DmxData.DataTypes.FullFrame)
                {
                    positions.Add(pos);
                    if (data.Data.All(x => x == 0))
                        blackPositions.Add(pos);
                    else
                    {
                        if (!timestampOffset.HasValue)
                            timestampOffset = data.Timestamp;
                    }
                }
            }

            Console.WriteLine("{0} frames in input file", inputFrameCount);

            long firstPos = positions
                .SkipWhile(x => blackPositions.Contains(x))
                .FirstOrDefault();

            long? lastPos = positions
                .Reverse<long>()
                .SkipWhile(x => blackPositions.Contains(x))
                .FirstOrDefault();
            if (lastPos == 0)
                lastPos = null;

            this.fileReader.Position = firstPos;
            while (this.fileReader.DataAvailable)
            {
                long pos = this.fileReader.Position;
                if (lastPos.HasValue && pos > lastPos.Value)
                    break;

                var data = this.fileReader.ReadFrame();

                data.Timestamp -= timestampOffset.Value;

                this.fileWriter.Output(data);
                outputFrameCount++;
            }

            Console.WriteLine("{0} frames written to output file", outputFrameCount);
        }

Usage Example

示例#1
0
        public static void Main(string[] args)
        {
            try
            {
                var arguments = Args.Parse<Arguments>(args);

                using (var fileReader = new Common.BinaryFileReader(arguments.Inputfile))
                {
                    Common.BinaryFileWriter fileWriter = null;

                    switch (arguments.Command)
                    {
                        case Arguments.Commands.TrimBlack:
                            fileWriter = new Common.BinaryFileWriter(arguments.OutputFile);
                            var trimBlackCommand = new TrimBlack(fileReader, fileWriter);
                            trimBlackCommand.Execute();
                            break;

                        case Arguments.Commands.FindLoop:
                            var findLoopCommand = new FindLoop(fileReader);
                            findLoopCommand.Execute();
                            break;

                        case Arguments.Commands.TrimEnd:
                            fileWriter = new Common.BinaryFileWriter(arguments.OutputFile);
                            var trimEndCommand = new TrimEnd(fileReader, fileWriter, arguments.TrimPos);
                            trimEndCommand.Execute();
                            break;
                    }

                    if (fileWriter != null)
                        fileWriter.Dispose();
                }
            }
            catch (ArgException ex)
            {
                Console.WriteLine("Argument error {0}", ex.Message);

                Console.WriteLine(ArgUsage.GenerateUsageFromTemplate<Arguments>());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unhandled exception: {0}", ex);
            }
        }