Example #1
0
        public static CropValues autocrop(IVideoReader reader)
        {
            int pos        = reader.FrameCount / 4;
            int tenPercent = reader.FrameCount / 20;

            CropValues[] cropValues = new CropValues[10];
            for (int i = 0; i < 10; i++)
            {
                Bitmap b = reader.ReadFrameBitmap(pos);
                cropValues[i] = getAutoCropValues(b);
                pos          += tenPercent;
            }
            bool       error = false;
            CropValues final = getFinalAutocropValues(cropValues);

            if (!error)
            {
                return(final);
            }
            else
            {
                final.left   = -1;
                final.right  = -1;
                final.top    = -1;
                final.bottom = -1;
                return(final);
            }
        }
Example #2
0
        public static CropValues autocrop(IVideoReader reader)
        {
            // start at 10% of the video, then advance by 6,66% and analyze 11 frames in total
            int pos  = reader.FrameCount / 10;
            int step = reader.FrameCount / 15;

            CropValues[] cropValues = new CropValues[11];
            for (int i = 0; i < 11; i++)
            {
                using (Bitmap b = reader.ReadFrameBitmap(pos))
                    cropValues[i] = getAutoCropValues(b);
                pos += step;
            }
            bool       error = false;
            CropValues final = getFinalAutocropValues(cropValues);

            if (!error)
            {
                return(final);
            }
            else
            {
                final.left   = -1;
                final.right  = -1;
                final.top    = -1;
                final.bottom = -1;
                return(final);
            }
        }
Example #3
0
 private void drawFrame(Graphics g)
 {
     using (Bitmap bmp = reader.ReadFrameBitmap(trackBar1.Value))
     {
         g.FillRectangle(Brushes.Black, 0, 0, bmp.Width + 2, bmp.Height + 2);
         g.DrawImage(bmp, new Point(1, 1));
         using (Pen p = new Pen(lineColor))
         {
             g.DrawLine(p, 0, cropTop, bmp.Width + 1, cropTop);
             g.DrawLine(p, 0, bmp.Height - cropBottom, bmp.Width + 1, bmp.Height - cropBottom);
             g.DrawLine(p, cropLeft, 0, cropLeft, bmp.Height + 1);
             g.DrawLine(p, bmp.Width - cropRight, 0, bmp.Width - cropRight, bmp.Height + 1);
         }
     }
 }
Example #4
0
 private Bitmap getFrame(int pos)
 {
     readerWriterLock.AcquireReaderLock(Timeout.Infinite);
     try
     {
         IVideoReader reader = VideoReader;
         if (reader == null)
         {
             return(null);
         }
         return(reader.ReadFrameBitmap(pos));
     }
     finally
     {
         readerWriterLock.ReleaseReaderLock();
     }
 }
Example #5
0
        public StringMask GetMask(string s, int x, int y, string style)
        {
            if (!IsAvsMask)
            {
                return(base.GetMask(s, x, y));
            }
            if (s.Trim() == "")
            {
                return new StringMask {
                           Height = FontHeight, Width = (WhitespaceWidth >= 0) ? WhitespaceWidth : FontWidth, X0 = x, Y0 = y, Points = new List <ASSPoint>()
                }
            }
            ;

            using (MaskDataContext db = new MaskDataContext())
            {
                var ma = db.Masks.Where(m => m.X == x && m.Y == y && m.PlayResX == this.PlayResX && m.PlayResY == this.PlayResY && m.Style == this.MaskStyle && m.Str == s.GetHashCode().ToString());

                if (ma.Count() > 0)
                {
                    return(new BinaryFormatter().Deserialize(new MemoryStream(ma.First().Data.ToArray())) as StringMask);
                }
            }

            // generate ass file
            string       assFN  = "BaseAnime2_Temp.ass";
            StreamWriter assOut = new StreamWriter(new FileStream(assFN, FileMode.Create), Encoding.Unicode);

            assOut.WriteLine("[Script Info]");
            assOut.WriteLine("Synch Point:0");
            assOut.WriteLine("ScriptType: v4.00+");
            assOut.WriteLine("Collisions:Normal");
            assOut.WriteLine("PlayResX:{0}", this.PlayResX);
            assOut.WriteLine("PlayResY:{0}", this.PlayResY);
            assOut.WriteLine("Timer:100.0000");
            assOut.WriteLine("");
            assOut.WriteLine("[V4+ Styles]");
            assOut.WriteLine("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding");
            assOut.WriteLine(style);
            assOut.WriteLine("");
            assOut.WriteLine("[Events]");
            assOut.WriteLine("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text");
            assOut.WriteLine("Dialogue: 0,0:00:00.00,0:01:00.00,Default,NTP,0000,0000,0000,,{0}{1}", ASSEffect.pos(x, y), s);
            assOut.Close();

            // generate avs file
            string       avsFN  = "BaseAnime2_Temp.avs";
            StreamWriter avsOut = new StreamWriter(new FileStream(avsFN, FileMode.Create), Encoding.Default);

            avsOut.WriteLine("BlankClip(height={0}, width={1}, length=1000, fps=23.976)", this.PlayResY, this.PlayResX);
            avsOut.WriteLine("ConvertToRGB24()");
            avsOut.WriteLine("TextSub(\"{0}\")", assFN);
            avsOut.Close();

            AvsFile avs = AvsFile.OpenScriptFile(avsFN);

            if (!avs.CanReadVideo)
            {
                return(null);
            }
            IVideoReader    ivr    = avs.GetVideoReader();
            Bitmap          bmp    = ivr.ReadFrameBitmap(0);
            BitmapData      bd     = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            List <ASSPoint> result = new List <ASSPoint>();

            unsafe
            {
                byte *p = (byte *)(void *)bd.Scan0;
                for (int x1 = 0; x1 < bmp.Width; x1++)
                {
                    for (int y1 = 0; y1 < bmp.Height; y1++)
                    {
                        byte *q = p + bd.Stride * y1 + x1 * 3;
                        if (q[0] > 0)
                        {
                            result.Add(new ASSPoint {
                                Brightness = q[0], X = x1, Y = y1
                            });
                        }
                    }
                }
            }
            bmp.UnlockBits(bd);
            bmp.Dispose();
            avs.Dispose();

            if (result.Count == 0)
            {
                return new StringMask {
                           Height = 0, Width = 0, X0 = x, Y0 = y, Points = result
                }
            }
            ;
            int xmin = 10000;
            int ymin = 10000;
            int xmax = -1;
            int ymax = -1;

            foreach (ASSPoint pt in result)
            {
                if (xmin > pt.X)
                {
                    xmin = pt.X;
                }
                if (xmax < pt.X)
                {
                    xmax = pt.X;
                }

                if (ymin > pt.Y)
                {
                    ymin = pt.Y;
                }
                if (ymax < pt.Y)
                {
                    ymax = pt.Y;
                }
            }

            StringMask sm = new StringMask {
                Height = ymax - ymin + 1, Width = xmax - xmin + 1, X0 = x, Y0 = y, Points = result
            };

            //sm.CalculateEdgeDistance();

            using (MaskDataContext db = new MaskDataContext())
            {
                try
                {
                    MemoryStream ms = new MemoryStream();
                    new BinaryFormatter().Serialize(ms, sm);
                    ms.Position = 0;
                    db.Masks.InsertOnSubmit(new Mask {
                        X = x, Y = y, PlayResX = this.PlayResX, PlayResY = this.PlayResY, Style = this.MaskStyle, Str = s.GetHashCode().ToString(), Data = new Binary(ms.ToArray())
                    });
                    db.SubmitChanges();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            return(sm);
        }