public void SaveVideo(int TimeLength) { List <Mat> frames = new List <Mat>(); var size = new Size(640, 480); var start = DateTime.Now; while ((DateTime.Now - start).TotalMilliseconds < TimeLength / 2) { var mat = new Mat(); if (capture.Read(mat)) { mat = mat.Resize(size); frames.Add(mat); System.Threading.Thread.Sleep(1); } } var frame_rate = frames.Count; var fmt = VideoWriter.FourCC('m', 'p', '4', 'v'); var writer = new VideoWriter(VIDEOFILE_NAME, fmt, frame_rate, size); for (int i = 0; i < frames.Count; i++) { writer.Write(frames[i].Clone()); } for (int i = frames.Count - 1; i >= 0; i--) { writer.Write(frames[i].Clone()); } writer.Release(); }
private static async Task RecordVideo(string videoname, int seconds) { VideoWriter writer = null; try { await Task.Delay(seconds * 1000); writer = new VideoWriter(videoname, VideoWriter.FourCC('M', 'J', 'P', 'G'), _fps, new Size(CAPTUREWIDTH, CAPTUREHEIGHT), true); lock (frameLoker) { LogUtil.Log($"start write buffer", LogLevel.Info); foreach (var buffer in matBuffer) { writer.Write(buffer); } } LogUtil.Log($"{videoname} Record {seconds} seconds finished", LogLevel.Info); } catch (Exception e) { LogUtil.LogException(e); } finally { if (writer != null) { writer.Dispose(); } } }
static void Main(string[] args) { string name = DateTime.Now.ToString("yyyyMMddhhmmss"); Console.WriteLine("开始生成..."); using (VideoWriter videoWriter = new VideoWriter("c:\\test_" + name + ".avi" , VideoWriter.FourCC('M', 'J', 'P', 'G') , 1 //每秒播放帧数 , new Size(800, 600), true)) //视频大小 { string filePath = System.AppDomain.CurrentDomain.BaseDirectory; DirectoryInfo directoryInfo = new DirectoryInfo(filePath + "\\image"); var filePathList = directoryInfo.GetFiles(); foreach (var item in filePathList) { Mat tempCCv = Cv2.ImRead(item.FullName); Mat result = new Mat(); Cv2.Resize(tempCCv, result, new Size(800, 600));//必须将图片的大小调整为视频的大小 //插入4帧相同的图片 videoWriter.Write(result); videoWriter.Write(result); videoWriter.Write(result); videoWriter.Write(result); } videoWriter.Release(); Cv2.DestroyAllWindows(); } Console.Write("生成完成!路径c:\\test_" + name + ".avi,建议使用Windows Media Player播放"); Console.Read(); }
private void RECstart_Click(object sender, EventArgs e) { if (REC.Text.Equals("REC start")) { //string recSaveTargetDirectory = @"./REC/" + DateTime.Now.ToString("yyyy-MM-dd"); //if (!System.IO.File.Exists(recSaveTargetDirectory)) //{ // System.IO.Directory.CreateDirectory(recSaveTargetDirectory); //} string fileName = initPath() + "Rec.avi"; int codec = VideoWriter.FourCC('M', 'J', 'P', 'G'); double fps = 20.0; writer.Open(fileName, codec, fps, frame.Size(), true); REC.Text = "REC stop"; } else { if (writer.IsOpened()) { writer.Release(); } REC.Text = "REC start"; } }
public static void Input() { VideoCapture cap = null; var dic = new Dictionary <FrameInfo, PrintEffectBase>(); Camera cam = null; string msg = "Enter the command..."; while (true) { string[] cmd = Inline(msg, true).Split(" "); msg = null; if (cmd[0] == "exit") { break; } else if (cmd[0] == "load") { if (!File.Exists(cmd[1])) { Log.Error("The file is not found."); continue; } cap = new VideoCapture(cmd[1]); Log.Info("Loading OK."); } else if (cmd[0] == "unload") { cap.Dispose(); Log.Info("Unloading completed."); } else if (cmd[0] == "effect") { if (cmd[1] == "flip") { var fi = new FrameInfo(uint.Parse(cmd[3], SystemLang), uint.Parse(cmd[4], SystemLang)); if (cmd[2] == "X") { dic.Add(fi, PrintEffect.FLIP(FlipMode.X)); } } } else if (cmd[0] == "output") { Movie.OutputMovie(cmd[1], ".avi", VideoWriter.FourCC('J', 'P', 'E', 'G'), cap, dic); } else if (cmd[0] == "cam") { if (cam == null) { cam = new Camera(); } if (cmd[1] == "open") { cam.Open(); } else if (cmd[1] == "show" && !cam.IsShow) { Task.Run(() => { cam.Show(); }); } else if (cmd[1] == "close") { cam.Close(); } else { continue; } Log.Info("Camera " + cmd[1]); } else if (cmd[0] == "show") { if (cap != null) { Task.Run(() => { Show(cap); }); } } else { Log.Warn("Illegual command : " + cmd[0]); } } cap.Dispose(); cam.Dispose(); }