Пример #1
0
        public void Play(string filePath, int maxColorDifference, int maximumMaxColorDifference)
        {
            using (var videoReader = new VideoFileReader())
            {
                videoReader.Open(filePath);

                //Variables used to display frames.
                var consoleImage  = new ColoredConsoleImage();
                int consoleWidth  = Console.WindowWidth,
                    consoleHeight = Console.WindowHeight;

                int  colorDifference = (int)(maximumMaxColorDifference * 0.25d);//75%
                Size videoSize       = new Size(Console.WindowWidth - 1, Console.WindowHeight * 2);

                //Variables used to calculate fps.
                double framesCounter = 0;
                var    st            = new Stopwatch();
                st.Start();

                for (int frameIndex = 0; frameIndex < videoReader.FrameCount; frameIndex++, framesCounter++)
                {
                    /*                          Handle user input.                          */
                    if (Console.KeyAvailable)//When any key is pressed.
                    {
                        switch (Console.ReadKey(true).Key)
                        {
                        case ConsoleKey.RightArrow:
                            frameIndex += (int)videoReader.FrameRate.Value * 5;    //Skip 5 sec.
                            break;

                        case ConsoleKey.LeftArrow:
                            frameIndex -= (int)videoReader.FrameRate.Value * 5;    //Go back 5 sec.
                            break;

                        case ConsoleKey.UpArrow:    //Increase quality.
                            if (colorDifference > maxColorDifference)
                            {
                                colorDifference -= 2;
                            }
                            break;

                        case ConsoleKey.DownArrow:    //Decrease quality.
                            if (colorDifference < maximumMaxColorDifference)
                            {
                                colorDifference += 2;
                            }
                            break;

                        case ConsoleKey.Spacebar:
                            st.Stop();
                            while (Console.ReadKey(true).Key != ConsoleKey.Spacebar)
                            {
                            }                                                               //Wait until space is pressed again.
                            st.Start();
                            break;

                        case ConsoleKey.C:    //Exit.
                            return;
                        }
                    }

                    /*                          Display fps.                        */
                    if (st.ElapsedMilliseconds >= 500)
                    {
                        double fps = framesCounter / (st.ElapsedMilliseconds / 1000d);

                        //Auto increase/decrease color quality.
                        if (fps < 10 && colorDifference < maximumMaxColorDifference)//Decrease quality.
                        {
                            colorDifference += 2;
                        }
                        else if (fps > 20 && colorDifference > maxColorDifference)//Increase quality.
                        {
                            colorDifference -= 2;
                        }

                        double colorDifferencePercentage = 100d / (maxColorDifference - maximumMaxColorDifference) * (colorDifference - maximumMaxColorDifference);
                        Console.Title = $"{Math.Round(fps, 2)}fps  Color quality: {Math.Round(colorDifferencePercentage, 1)}%";

                        if (consoleWidth != Console.WindowWidth || consoleHeight != Console.WindowHeight)//When window is resized.
                        {
                            Console.Clear();
                            Console.CursorVisible = false;

                            videoSize     = new Size(Console.WindowWidth - 1, Console.WindowHeight * 2);
                            consoleWidth  = Console.WindowWidth;
                            consoleHeight = Console.WindowHeight;
                        }

                        framesCounter = 0;
                        st.Restart();
                    }

                    /*                          Display frame.                          */
                    using (var plainFrame = videoReader.ReadVideoFrame(frameIndex))
                        using (Bitmap frame = new Bitmap(plainFrame, videoSize))
                            Console.Write(consoleImage.Create(frame, colorDifference));

                    Console.SetCursorPosition(0, 0);
                }
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            /*                          Process args.                           */
            if (args.Length <= 0 || !File.Exists(path: args[0]))//Check if image/video exists.
            {
                Console.WriteLine("Usage: MediaConsole.exe {path to image/video} Colors:[on|off] Background:[dark|white] [Pixels]");
                return;
            }

            //Check colors.
            bool colors =
                args.Length < 2 ||
                args[1].Equals("on", StringComparison.CurrentCultureIgnoreCase);

            //Change colors of the console, for a black/white image only.
            if (!colors && args.Length >= 3 && args[2].Equals("black", StringComparison.CurrentCultureIgnoreCase))
            {
                Console.BackgroundColor = ConsoleColor.Black;
                Console.ForegroundColor = ConsoleColor.White;
            }
            else
            {
                Console.BackgroundColor = ConsoleColor.White;
                Console.ForegroundColor = ConsoleColor.Black;
            }

            char[] pixels;//Change pixels, for a black/white image only.
            if (!colors && args.Length >= 4)
            {
                pixels = new char[args[3].Length];
                for (int i = 0; i < pixels.Length; i++)//Copy all chars to pixels.
                {
                    pixels[i] = args[3][i];
                }
            }
            else//Use default pixels.
            {
                pixels = DefaultPixels;
            }

            /*                      Display image/Play video.                           */
            if (Path.GetExtension(path: args[0]).Equals(".mp4"))
            {
                if (colors)
                {
                    new ColoredConsoleVideo().Play(filePath: args[0], maxColorDifferenceVideo, maximumMaxColorDifferenceVideo);
                }
                else
                {
                    new ConsoleVideo().Play(filePath: args[0], pixels);
                }
            }
            else
            {
                do
                {
                    Console.Clear();

                    using (Image image = Image.FromFile(filename: args[0]))
                    {
                        Size imageSize = GetMaxSize(image.Width, image.Height, Console.WindowWidth - 1, Console.WindowHeight * 2);
                        Console.WindowHeight = imageSize.Height / 2;//1 pixel is 50% of a row.

                        using (Bitmap resizedImage = new Bitmap(image, imageSize))
                        {
                            if (colors)
                            {
                                var consoleImage = new ColoredConsoleImage();
                                Console.Write(consoleImage.Create(resizedImage, maxColorDifferenceImage));
                            }
                            else
                            {
                                var consoleImage = new ConsoleImage();
                                Console.Write(consoleImage.Create(resizedImage, pixels));
                            }
                        }
                    }
                } while (Console.ReadKey().Key != ConsoleKey.C);
            }

            Console.Clear();
        }