Exemple #1
0
        private static VideoInfoModel GetVideoInfo(string videoPath)
        {
            var model = new VideoInfoModel();

            using (var videoFrameReader = new VideoFrameReader(videoPath))
            {
                model.Properties.Add("Duration", videoFrameReader.Duration.ToString());
                model.Properties.Add("Width", videoFrameReader.Width.ToString());
                model.Properties.Add("Height", videoFrameReader.Height.ToString());
                model.Properties.Add("CodecName", videoFrameReader.CodecName);
                model.Properties.Add("CodecDescription", videoFrameReader.CodecDescription);
                model.Properties.Add("CodecTag", videoFrameReader.CodecTag);
                model.Properties.Add("BitRate", videoFrameReader.BitRate.ToString());
                model.Properties.Add("FrameRate", videoFrameReader.FrameRate.ToString(CultureInfo.InvariantCulture));

                foreach (var entry in videoFrameReader.Metadata)
                {
                    model.Metadata.Add(entry.Key, entry.Value);
                }

                if (model.Metadata.Count == 0)
                {
                    model.Metadata.Add("", "");
                }
            }

            return(model);
        }
Exemple #2
0
 public static TimeSpan GetDuration(string videoPath)
 {
     using (var videoFrameReader = new VideoFrameReader(videoPath))
     {
         return(videoFrameReader.Duration);
     }
 }
Exemple #3
0
 private void PrepareVideoExtraction(string filePath)
 {
     videoFrameReader = new VideoFrameReader(filePath);
     if (videoFrameReader != null)
     {
         frameDurationSec = 1.0 / videoFrameReader.FrameRate;
     }
     else
     {
         throw new InvalidDataException("Unable to open video file");
     }
 }
Exemple #4
0
 /// <summary>
 /// Close the input file. Once closed, NextFrame cannot be used
 /// </summary>
 public void Close()
 {
     if (wavFile != null)
     {
         wavFile.Close();
         wavFile = null;
         //DeleteExistingFile(wavFilePath);
     }
     if (videoFrameReader != null)
     {
         videoFrameReader.Dispose();
         videoFrameReader = null;
     }
 }
Exemple #5
0
        public VideoModel GetVideoFrames(byte[] videoBytes)
        {
            Bitmap fr;

            videoModel.Images = new List <string>();
            //create the frame reader
            using (MemoryStream ms = new MemoryStream(videoBytes))
                using (videoFrameReader = new VideoFrameReader(ms))
                {
                    //if video too big
                    if (videoFrameReader.Duration.TotalSeconds > 30)
                    {
                        return(null);
                    }
                    else
                    {
                        //calculating number of frames as fps/framerate*duration
                        videoModel.FrameRate = (int)Math.Ceiling(videoFrameReader.FrameRate);
                        //        var totalFrames = videoFrameReader.Duration.TotalSeconds * fps;
                        //looping through all the frames
                        foreach (var frame in videoFrameReader)
                        {
                            //for this frame
                            using (frame)
                            {
                                //detect if there is motion
                                fr = DetectMotion(motionDetector, frame);
                                //if there is motion get image bytes, convert it to base64 string and add it to list
                                if (fr != null)
                                {
                                    var bytes       = GetImageBytes(fr);
                                    var base64Frame = Convert.ToBase64String(bytes);
                                    videoModel.Images.Add(base64Frame);
                                }
                                //if there isn't motion get the byte array of frame, convert it to base 64 and add it to list
                                else
                                {
                                    var bytes       = GetImageBytes(frame);
                                    var base64Frame = Convert.ToBase64String(bytes);
                                    videoModel.Images.Add(base64Frame);
                                }
                            }
                        }
                    }

                    videoFrameReader.Dispose();
                    return(videoModel);
                }
        }
        private void importedVideoLabel_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //Add the video at 0 zero seconds on the timeline.
            //TODO: Make the user drag it to the spot in the timeline they want.

            Label clickedLabel = (Label)sender;

            //Open the video file so we can get its length
            VideoFrameReader reader = importedVideos[(string)clickedLabel.Content];
            double           length = reader.Duration.TotalSeconds;

            //Create a timeline entry for it
            TimelineEntry newEntry = new TimelineEntry((string)clickedLabel.Content, 0, length, null);

            //Add it to the timeline
            timelineView.GetLayer(0).AddEntry(newEntry);
        }
Exemple #7
0
        public static Bitmap GetFrame(string videoPath, double frameTime)
        {
            using (var videoFrameReader = new VideoFrameReader(videoPath))
            {
                if (frameTime > 0)
                {
                    videoFrameReader.Seek(frameTime);
                }

                //videoFrameReader.SetFrameWidth(300);

                if (videoFrameReader.Read())
                {
                    return(videoFrameReader.GetFrame());
                }

                return(GetErrorFrame(videoFrameReader.Width, videoFrameReader.Height, "Reading frame failed"));
            }
        }
Exemple #8
0
        // GET: Video
        public ActionResult Index()
        {
            string root = @"E:\VidCheck";

            string[] VidEntries = Directory.GetFiles(root);
            foreach (string VidFiles in VidEntries)
            {
                using (var VideoFrameReader = new VideoFrameReader(VidFiles))
                {
                    var Duration           = VideoFrameReader.Duration;
                    var Width              = VideoFrameReader.Width;
                    var Height             = VideoFrameReader.Height;
                    var CodecName          = VideoFrameReader.CodecName;
                    var CodecTag           = VideoFrameReader.CodecTag;
                    var BitRate            = VideoFrameReader.BitRate;
                    var FrameRate          = VideoFrameReader.FrameRate;
                    var CodecDescription   = VideoFrameReader.CodecDescription;
                    var CurrentFrameNumber = VideoFrameReader.CurrentFrameNumber;

                    /*VideoDetail1 viewModel = new VideoDetail1();
                     * viewModel.DictionaryTest = new Dictionary<string, string>();
                     * viewModel.DictionaryTest.Add("key1", "value1");
                     * viewModel.DictionaryTest.Add("key2", "value2");*/

                    VideoDetail video = new VideoDetail();
                    video.Length             = Duration.ToString();
                    video.FrameWidth         = Width.ToString();
                    video.FrameHeight        = Height.ToString();
                    video.CodecName          = CodecName.ToString();
                    video.CodecTag           = CodecTag.ToString();
                    video.BitRate            = BitRate.ToString();
                    video.FrameRate          = FrameRate.ToString();
                    video.CodecDescription   = CodecDescription.ToString();
                    video.CurrentFrameNumber = CurrentFrameNumber.ToString();
                    db.VideoDetails.Add(video);
                }

                db.SaveChanges();
            }
            return(View());
        }
Exemple #9
0
        private void btnGenerateFrame_Click(object sender, EventArgs e)
        {
            //string path = @"F:\Grapics\video.mp4";

            string path = txtSource.Text; //Get video path from source path

            using (var videoFrameReader = new VideoFrameReader(path))
            {
                if (videoFrameReader.Read()) //Only if frame was read successfully
                {
                    var resultCount = videoFrameReader.FrameRate * videoFrameReader.Duration.TotalSeconds;

                    progressBar.Maximum = (int)resultCount; // set maximum value of progress bar

                    int i = 1;
                    foreach (var frame in videoFrameReader)
                    {
                        progressBar.Value += 1; // increement progess bar value
                        using (frame)           //save generated frame
                        {
                            frame.Save(@$ "F:\Grapics\Frame\Frame{i}.jpg", ImageFormat.Jpeg);
 static async Task MainAsync()
 {
     int movienumber = 12;
     using (var videoFrameReader = new VideoFrameReader($@"{movienumber}.mp4"))
     {
         var startTime = 10;
         videoFrameReader.Seek(startTime);
         var frameIndex = 0;
         foreach (var frame in videoFrameReader)
         {
             using (frame)
             {
                 if (frameIndex % 60 == 0) //Save every 60th frame
                 {
                     Console.WriteLine($"Getting image at {TimeSpan.FromSeconds(startTime).TotalMinutes}");
                     frame.Save($@"{movienumber}-full/Frame{frameIndex} - {startTime} - {TimeSpan.FromSeconds(startTime).TotalMinutes}.jpg", ImageFormat.Jpeg);
                     startTime++;
                 }
                 frameIndex++;
             }
         }
     }
 }
Exemple #11
0
        static async Task MainAsync()
        {
            int movienumber = 12;

            using (var videoFrameReader = new VideoFrameReader($@"{movienumber}.mp4"))
            {
                var startTime = 10;
                videoFrameReader.Seek(startTime);
                var frameIndex = 0;
                foreach (var frame in videoFrameReader)
                {
                    using (frame)
                    {
                        if (frameIndex % 60 == 0) //Save every 60th frame
                        {
                            Console.WriteLine($"Getting image at {TimeSpan.FromSeconds(startTime).TotalMinutes}");
                            frame.Save($@"{movienumber}-full/Frame{frameIndex} - {startTime} - {TimeSpan.FromSeconds(startTime).TotalMinutes}.jpg", ImageFormat.Jpeg);
                            startTime++;
                        }
                        frameIndex++;
                    }
                }
            }
        }
        static void VideoHandler()
        {
moveCopyStart:
            Console.WriteLine("Do you wish to Move or Copy these files?");
            string moveOrCopy = Console.ReadLine().ToLower();

            if (moveOrCopy == "m" || moveOrCopy == "move" || moveOrCopy == "c" || moveOrCopy == "copy")
            {
                Console.WriteLine("Searching for and sorting files now...");

                if (moveOrCopy == "move" || moveOrCopy == "m")
                {
                    moveCopy = true;
                }
                else
                {
                    moveCopy = false;
                }
                string searchLocationStart = driveLetter + ":\\";
                searchPaths.Enqueue(searchLocationStart);

                while (searchPaths.Count > 0)
                {
                    var directory = searchPaths.Dequeue();

                    try
                    {
                        fileList = Directory.GetFiles(directory).ToList();

                        foreach (string file in fileList)
                        {
                            foreach (string type in fileType)
                            {
                                moveCopyComplete = false;
                                if (file.Contains(type.ToString()))
                                {
                                    var videoReader = new VideoFrameReader(file);
                                    videoDuration = videoReader.Duration;

                                    /*if (moveCopyComplete == true)
                                     * {
                                     *  break;
                                     * }
                                     * else*/
                                    if (videoDuration > movieDuration && presetSelect == "m")
                                    {
                                        if (moveCopy == true)
                                        {
                                            if (!File.Exists(placementDirectoryM + Path.GetFileName(file)))
                                            {
                                                Console.WriteLine("Moving {0} to {1}", file, placementDirectoryM);
                                                File.Move(file, placementDirectoryM + Path.GetFileName(file));
                                                moveCopyComplete = true;
                                            }
                                            else if (File.Exists(placementDirectoryM + Path.GetFileName(file)))
                                            {
                                                var existingVideo = new VideoFrameReader(placementDirectoryM + Path.GetFileName(file));
                                                if (existingVideo.Height < videoReader.Height)
                                                {
                                                    Console.WriteLine("Moving {0} to {1}", file, placementDirectoryM);
                                                    File.Delete(placementDirectoryM + Path.GetFileName(file));
                                                    File.Move(file, placementDirectoryM + Path.GetFileName(file));
                                                    moveCopyComplete = true;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (!File.Exists(placementDirectoryM + Path.GetFileName(file)))
                                            {
                                                Console.WriteLine("Moving {0} to {1}", file, placementDirectoryM);
                                                File.Copy(file, placementDirectoryM + Path.GetFileName(file));
                                                moveCopyComplete = true;
                                            }
                                            else if (File.Exists(placementDirectoryM + Path.GetFileName(file)))
                                            {
                                                var existingVideo = new VideoFrameReader(placementDirectoryM + Path.GetFileName(file));
                                                if (existingVideo.Height < videoReader.Height)
                                                {
                                                    Console.WriteLine("Moving {0} to {1}", file, placementDirectoryM);
                                                    File.Delete(placementDirectoryM + Path.GetFileName(file));
                                                    File.Copy(file, placementDirectoryM + Path.GetFileName(file));
                                                    moveCopyComplete = true;
                                                }
                                            }
                                        }
                                    }
                                    else if (videoDuration < movieDuration && videoDuration > showDuration && presetSelect == "s")
                                    {
                                        if (moveCopy == true)
                                        {
                                            if (!File.Exists(placementDirectoryS + Path.GetFileName(file)))
                                            {
                                                Console.WriteLine("Moving {0} to {1}", file, placementDirectoryS);
                                                File.Move(file, placementDirectoryS + Path.GetFileName(file));
                                                moveCopyComplete = true;
                                            }
                                            else if (File.Exists(placementDirectoryM + Path.GetFileName(file)))
                                            {
                                                var existingVideo = new VideoFrameReader(placementDirectoryM + Path.GetFileName(file));
                                                if (existingVideo.Height < videoReader.Height)
                                                {
                                                    Console.WriteLine("Moving {0} to {1}", file, placementDirectoryS);
                                                    File.Delete(placementDirectoryS + Path.GetFileName(file));
                                                    File.Move(file, placementDirectoryS + Path.GetFileName(file));
                                                    moveCopyComplete = true;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (!File.Exists(placementDirectoryS + Path.GetFileName(file)))
                                            {
                                                Console.WriteLine("Copying {0} to {1}", file, placementDirectoryS);
                                                File.Copy(file, placementDirectoryS + Path.GetFileName(file));
                                                moveCopyComplete = true;
                                            }
                                            else if (File.Exists(placementDirectoryM + Path.GetFileName(file)))
                                            {
                                                var existingVideo = new VideoFrameReader(placementDirectoryM + Path.GetFileName(file));
                                                if (existingVideo.Height < videoReader.Height)
                                                {
                                                    Console.WriteLine("Copying {0} to {1}", file, placementDirectoryS);
                                                    File.Delete(placementDirectoryS + Path.GetFileName(file));
                                                    File.Copy(file, placementDirectoryS + Path.GetFileName(file));
                                                    moveCopyComplete = true;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        foreach (var subDirectory in Directory.GetDirectories(directory))
                        {
                            searchPaths.Enqueue(subDirectory);
                        }
                    }
                    catch (System.IO.IOException)
                    {
                        /*Console.WriteLine("IO Exeption encountered");
                         * Console.WriteLine("Press any key to continue...");
                         * Console.ReadKey();*/
                    }
                    catch (System.UnauthorizedAccessException)
                    {
                        /*Console.WriteLine("Unauthorized Access");
                         * Console.WriteLine("Administrator Priveliges required");
                         * Console.ReadKey();*/
                    }
                    catch (Exception e)
                    {
                        /*Console.WriteLine("There was a {0}", e);
                         * Console.WriteLine("Press any key to continue...");
                         * Console.ReadKey();
                         * Console.WriteLine("Continuing...");*/
                    }
                }
            }
            else if (moveOrCopy != "m" || moveOrCopy != "move" || moveOrCopy != "c" || moveOrCopy != "copy")
            {
                Console.WriteLine("Please select a valid option");
                goto moveCopyStart;
            }
        }
Exemple #13
0
        private void ExtractThumbnails()
        {
            try
            {
                foreach (Content content in _contents)
                {
                    #region if

                    if (content.Extension == ".avi" || content.Extension == ".mov" || content.Extension == ".mp4" || content.Extension == ".wmv" || content.Extension == ".mpg" ||
                        content.Extension == ".mpeg")
                    {
                        string previewFrameDirectory = _previewFramesPath + content.SubDir + "\\";

                        if (!Directory.Exists(previewFrameDirectory))
                        {
                            Directory.CreateDirectory(previewFrameDirectory);
                        }

                        using (VideoFrameReader vfr = new VideoFrameReader(_filePath + content.FileName))
                        {
                            int noFrames = vfr.NumberOfFrames;

                            // save a thumbnail for the first frame for the "My Content" panel
                            int firstFrameIndex;

                            if (noFrames < 50)
                            {
                                firstFrameIndex = 0;
                            }
                            if (noFrames < 200)
                            {
                                firstFrameIndex = 50;
                            }
                            else
                            {
                                firstFrameIndex = 200;
                            }

                            using (Bitmap firstFrame = vfr.GetFrame(firstFrameIndex))
                            {
                                int height = (int)((float)_thumbnailWidth * ((float)firstFrame.Height / (float)firstFrame.Width));

                                using (Bitmap thumbnail = new Bitmap(_thumbnailWidth, height))
                                {
                                    using (Graphics g = Graphics.FromImage(thumbnail))
                                    {
                                        g.DrawImage(firstFrame, 0, 0, _thumbnailWidth, height);
                                    }

                                    thumbnail.Save(_thumbnailPath + content.SubDir + "\\" + content.FileNameWithoutExtension + ".jpg");
                                }
                            }

                            int frameCount = 1;

                            int space = noFrames / _targetNoFrames;

                            for (int frameNumber = 0; frameNumber < vfr.NumberOfFrames; frameNumber = frameNumber + space)
                            {
                                using (Bitmap frame = vfr.GetFrame(frameNumber))
                                {
                                    ImageUtilities.SaveImage(frame, previewFrameDirectory + content.FileNameWithoutExtension + frameCount + ".jpg", _quality);
                                }

                                frameCount++;
                            }
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }