private static WritableLockBitImage GetFrameFromVideo(VideoFingerPrintWrapper video, int frameNumber) { using (MediaInfoProcess mediaInfoProcess = new MediaInfoProcess(video.FilePath)) { MediaInfo mediaInfo = mediaInfoProcess.Execute(); if (mediaInfo.GetFramerate().Numerator == 0) { throw new Exception("Did not get valid frame rate"); } int width = mediaInfo.GetWidth(); int height = mediaInfo.GetHeight(); var ffmpegProcessSettings = new FFMPEGProcessVideoSettings( video.FilePath, mediaInfo.GetFramerate().Numerator, mediaInfo.GetFramerate().Denominator * 4, FFMPEGMode.SeekFrame ); ffmpegProcessSettings.TargetFrame = frameNumber; byte[] frameBytes = new byte[width * height * 3]; int offset = 0; using ( var ffmpegProcess = new FFMPEGProcess( ffmpegProcessSettings, (stdoutBytes, numBytes) => { Buffer.BlockCopy(stdoutBytes, 0, frameBytes, offset, numBytes); offset += numBytes; }, false ) ) { ffmpegProcess.Execute(); if (offset != 3 * width * height) { throw new Exception("Did not get all bytes to produce valid frame"); } WritableLockBitImage videoFrame = new WritableLockBitImage(width, height, frameBytes); videoFrame.Lock(); return(videoFrame); } } }
/// <summary> /// Index a video file by continuously dumping Y4M raw video and indexing it /// </summary> /// <param name="videoFile"></param> /// <returns></returns> public static VideoFingerPrintWrapper IndexVideo(string videoFile, long maxMemory) { using (var mediaInfoProcess = new MediaInfoProcess(videoFile)) { MediaInfo info = mediaInfoProcess.Execute(); if (info.GetFramerate().Numerator == 0) { throw new InvalidOperationException("Invalid framerate for: " + videoFile); } return new VideoFingerPrintWrapper { FilePath = videoFile, FingerPrints = IndexVideo(videoFile, info, maxMemory).ToArray(), }; } }