예제 #1
0
        public static async Task CopyLastFrame(int lastFrameNum)
        {
            if (I.canceled)
            {
                return;
            }

            try
            {
                lastFrameNum--;     // We have to do this as extracted frames start at 0, not 1
                bool   frameFolderInput = IoUtils.IsPathDirectory(I.current.inPath);
                string targetPath       = Path.Combine(I.current.framesFolder, lastFrameNum.ToString().PadLeft(Padding.inputFrames, '0') + I.current.framesExt);
                if (File.Exists(targetPath))
                {
                    return;
                }

                Size res = IoUtils.GetImage(IoUtils.GetFilesSorted(I.current.framesFolder, false).First()).Size;

                if (frameFolderInput)
                {
                    string lastFramePath = IoUtils.GetFilesSorted(I.current.inPath, false).Last();
                    await FfmpegExtract.ExtractLastFrame(lastFramePath, targetPath, res);
                }
                else
                {
                    await FfmpegExtract.ExtractLastFrame(I.current.inPath, targetPath, res);
                }
            }
            catch (Exception e)
            {
                Logger.Log("CopyLastFrame Error: " + e.Message);
            }
        }
예제 #2
0
        public static void UpdateInterpProgress(int frames, int target, string latestFramePath = "")
        {
            if (I.canceled)
            {
                return;
            }
            interpolatedInputFramesCount = ((frames / I.current.interpFactor).RoundToInt() - 1);
            //ResumeUtils.Save();
            frames = frames.Clamp(0, target);
            int percent = (int)Math.Round(((float)frames / target) * 100f);

            Program.mainForm.SetProgress(percent);

            float  generousTime = ((AiProcess.processTime.ElapsedMilliseconds - AiProcess.lastStartupTimeMs) / 1000f);
            float  fps          = ((float)frames / generousTime).Clamp(0, 9999);
            string fpsIn        = (fps / currentFactor).ToString("0.00");
            string fpsOut       = fps.ToString("0.00");

            if (fps > peakFpsOut)
            {
                peakFpsOut = fps;
            }

            float  secondsPerFrame = generousTime / (float)frames;
            int    framesLeft      = target - frames;
            float  eta             = framesLeft * secondsPerFrame;
            string etaStr          = FormatUtils.Time(new TimeSpan(0, 0, eta.RoundToInt()), false);

            bool replaceLine = Regex.Split(Logger.textbox.Text, "\r\n|\r|\n").Last().Contains("Average Speed: ");

            string logStr = $"Interpolated {frames}/{target} Frames ({percent}%) - Average Speed: {fpsIn} FPS In / {fpsOut} FPS Out - ";

            logStr += $"Time: {FormatUtils.Time(AiProcess.processTime.Elapsed)} - ETA: {etaStr}";
            if (AutoEncode.busy)
            {
                logStr += " - Encoding...";
            }
            Logger.Log(logStr, false, replaceLine);

            try
            {
                if (!string.IsNullOrWhiteSpace(latestFramePath) && frames > currentFactor)
                {
                    if (bigPreviewForm == null && !preview.Visible /* ||Program.mainForm.WindowState != FormWindowState.Minimized */ /* || !Program.mainForm.IsInFocus()*/)
                    {
                        return;                                                                                                                                                             // Skip if the preview is not visible or the form is not in focus
                    }
                    if (timeSinceLastPreviewUpdate.IsRunning && timeSinceLastPreviewUpdate.ElapsedMilliseconds < previewUpdateRateMs)
                    {
                        return;
                    }
                    Image img = IoUtils.GetImage(latestFramePath, false, false);
                    SetPreviewImg(img);
                }
            }
            catch (Exception e)
            {
                //Logger.Log("Error updating preview: " + e.Message, true);
            }
        }
예제 #3
0
        public static async Task RemoveAlpha(string inputDir, string outputDir, string fillColor = "black")
        {
            Directory.CreateDirectory(outputDir);
            foreach (FileInfo file in IoUtils.GetFileInfosSorted(inputDir))
            {
                string outPath = Path.Combine(outputDir, "_" + file.Name);
                Size   s       = IoUtils.GetImage(file.FullName).Size;
                string args    = $" -f lavfi -i color={fillColor}:s={s.Width}x{s.Height} -i {file.FullName.Wrap()} -filter_complex overlay=0:0:shortest=1 {outPath.Wrap()}";
                await RunFfmpeg(args, LogMode.Hidden);

                file.Delete();
                File.Move(outPath, file.FullName);
            }
        }
예제 #4
0
        public static async Task <Image> GetThumbnail(string path)
        {
            string imgOnDisk = Path.Combine(Paths.GetDataPath(), "thumb-temp.jpg");

            try
            {
                if (!IoUtils.IsPathDirectory(path))     // If path is video - Extract first frame
                {
                    await FfmpegExtract.ExtractSingleFrame(path, imgOnDisk, 1);

                    return(IoUtils.GetImage(imgOnDisk));
                }
                else     // Path is frame folder - Get first frame
                {
                    return(IoUtils.GetImage(IoUtils.GetFilesSorted(path)[0]));
                }
            }
            catch (Exception e)
            {
                Logger.Log("GetThumbnail Error: " + e.Message, true);
                return(null);
            }
        }
예제 #5
0
        static bool AreImagesCompatible(string inpath, int maxHeight)
        {
            NmkdStopwatch sw = new NmkdStopwatch();

            string[]   validExtensions = Filetypes.imagesInterpCompat; // = new string[] { ".jpg", ".jpeg", ".png" };
            FileInfo[] files           = IoUtils.GetFileInfosSorted(inpath);

            if (files.Length < 1)
            {
                Logger.Log("[AreImagesCompatible] Sequence not compatible: No files found.", true);
                return(false);
            }

            bool allSameExtension = files.All(x => x.Extension == files.First().Extension);

            if (!allSameExtension)
            {
                Logger.Log($"Sequence not compatible: Not all files have the same extension.", true);
                return(false);
            }

            bool allValidExtension = files.All(x => validExtensions.Contains(x.Extension));

            if (!allValidExtension)
            {
                Logger.Log($"Sequence not compatible: Not all files have a valid extension ({string.Join(", ", validExtensions)}).", true);
                return(false);
            }

            int sampleCount = Config.GetInt(Config.Key.imgSeqSampleCount, 10);

            Image[] randomSamples = files.OrderBy(arg => Guid.NewGuid()).Take(sampleCount).Select(x => IoUtils.GetImage(x.FullName)).ToArray();

            bool allSameSize = randomSamples.All(i => i.Size == randomSamples.First().Size);

            if (!allSameSize)
            {
                Logger.Log($"Sequence not compatible: Not all images have the same dimensions.", true);
                return(false);
            }

            int  div       = GetPadding();
            bool allDivBy2 = randomSamples.All(i => (i.Width % div == 0) && (i.Height % div == 0));

            if (!allDivBy2)
            {
                Logger.Log($"Sequence not compatible: Not all image dimensions are divisible by {div}.", true);
                return(false);
            }

            bool allSmallEnough = randomSamples.All(i => (i.Height <= maxHeight));

            if (!allSmallEnough)
            {
                Logger.Log($"Sequence not compatible: Image dimensions above max size.", true);
                return(false);
            }

            bool all24Bit = randomSamples.All(i => (i.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb));

            if (!all24Bit)
            {
                Logger.Log($"Sequence not compatible: Some images are not 24-bit (8bpp).", true);
                return(false);
            }

            Interpolate.current.framesExt = files.First().Extension;
            Logger.Log($"Sequence compatible!", true);
            return(true);
        }