我想保存视频来自摄像机,作为MP4在WPF上

发布于 2025-01-17 12:19:15 字数 62 浏览 2 评论 0原文

我正在尝试将相机中的图像保存为 WPF 应用程序上的 mp4(等)。但到目前为止我还没有成功。 感谢您的帮助。

I am trying to save the image from the camera as mp4(etc) on WPF application. But so far I have not been successful.
Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

药祭#氼 2025-01-24 12:19:15

抱歉,我一开始的英语不好。我正在从网络摄像头接收图像。下面是代码。

公共部分类 MainWindow : 窗口
{
私有 FilterInfoCollection cihazlar;
私有VideoCaptureDevice yakala_resim
;
公共主窗口()
{
初始化组件();
cihazlar = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach(cihazlar 中的 FilterInfo 设备)
{
kameralar.Items.Add(设备.名称);
kameralar.SelectedIndex = 2;
}
目前没有问题

    private void Başla_Click(object sender, RoutedEventArgs e)
    {
        yakala_resim = new VideoCaptureDevice(cihazlar[kameralar.SelectedIndex].MonikerString);
        yakala_resim.NewFrame += Yakala_NewFrame1;
        yakala_resim.Start(); 
    }

    private void Yakala_NewFrame1(object sender, NewFrameEventArgs eventArgs)
    {
        try
        {
            BitmapImage bi;
            using (var bitmap = (Bitmap)eventArgs.Frame.Clone())
            {
                bi = bitmap.ToBitmapImage();
            }
            bi.Freeze();
            Dispatcher.BeginInvoke(new ThreadStart(delegate { video.Source = bi; })); 
        }
        catch (Exception)
        {

            
        }

    }

。但当我想将来自网络摄像头的图像保存为 mp4 时,我不知道该怎么做。

Sorry for my bad english at first.I am receiving the image from the webcam. Below are the codes.

public partial class MainWindow : Window
{
private FilterInfoCollection cihazlar;
private VideoCaptureDevice yakala_resim
;
public MainWindow()
{
InitializeComponent();
cihazlar = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo device in cihazlar)
{
kameralar.Items.Add(device.Name);
kameralar.SelectedIndex = 2;
}
}

    private void Başla_Click(object sender, RoutedEventArgs e)
    {
        yakala_resim = new VideoCaptureDevice(cihazlar[kameralar.SelectedIndex].MonikerString);
        yakala_resim.NewFrame += Yakala_NewFrame1;
        yakala_resim.Start(); 
    }

    private void Yakala_NewFrame1(object sender, NewFrameEventArgs eventArgs)
    {
        try
        {
            BitmapImage bi;
            using (var bitmap = (Bitmap)eventArgs.Frame.Clone())
            {
                bi = bitmap.ToBitmapImage();
            }
            bi.Freeze();
            Dispatcher.BeginInvoke(new ThreadStart(delegate { video.Source = bi; })); 
        }
        catch (Exception)
        {

            
        }

    }

no problem currently. But I don't know what to do when I want to save the image come from webcam as mp4.

池予 2025-01-24 12:19:15

You can use from OpenCvSharp

namespace BlackBears.Recording
{
    using System;
    using System.Drawing;
    using System.Threading;

    using OpenCvSharp;
    using OpenCvSharp.Extensions;

    using Size = OpenCvSharp.Size;

    public class Recorder : IDisposable
    {
        private readonly VideoCaptureAPIs _videoCaptureApi = VideoCaptureAPIs.DSHOW;
        private readonly ManualResetEventSlim _writerReset = new(false);
        private readonly VideoCapture _videoCapture;
        private VideoWriter _videoWriter;
        private Thread _writerThread;

        private bool IsVideoCaptureValid => _videoCapture is not null && _videoCapture.IsOpened();

        public Recorder(int deviceIndex, int frameWidth, int frameHeight, double fps)
        {
            _videoCapture = VideoCapture.FromCamera(deviceIndex, _videoCaptureApi);
            _videoCapture.Open(deviceIndex, _videoCaptureApi);

            _videoCapture.FrameWidth = frameWidth;
            _videoCapture.FrameHeight = frameHeight;
            _videoCapture.Fps = fps;
        }

        /// <inheritdoc />
        public void Dispose()
        {
            GC.SuppressFinalize(this);
            Dispose(true);
        }

        ~Recorder()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                StopRecording();

                _videoCapture?.Release();
                _videoCapture?.Dispose();
            }
        }

        public void StartRecording(string path)
        {
            if (_writerThread is not null)
                return;

            if (!IsVideoCaptureValid)
                ThrowHelper.ThrowVideoCaptureNotReadyException();

            _videoWriter = new VideoWriter(path, FourCC.XVID, _videoCapture.Fps, new Size(_videoCapture.FrameWidth, _videoCapture.FrameHeight));

            _writerReset.Reset();
            _writerThread = new Thread(AddCameraFrameToRecordingThread);
            _writerThread.Start();
        }

        public void StopRecording()
        {
            if (_writerThread is not null)
            {
                _writerReset.Set();
                _writerThread.Join();
                _writerThread = null;
                _writerReset.Reset();
            }

            _videoWriter?.Release();
            _videoWriter?.Dispose();
            _videoWriter = null;
        }

        private void AddCameraFrameToRecordingThread()
        {
            var waitTimeBetweenFrames = (int)(1_000 / _videoCapture.Fps);
            using var frame = new Mat();
            while (!_writerReset.Wait(waitTimeBetweenFrames))
            {
                if (!_videoCapture.Read(frame))
                    return;
                _videoWriter.Write(frame);
            }
        }
    }
}

You can use from OpenCvSharp

namespace BlackBears.Recording
{
    using System;
    using System.Drawing;
    using System.Threading;

    using OpenCvSharp;
    using OpenCvSharp.Extensions;

    using Size = OpenCvSharp.Size;

    public class Recorder : IDisposable
    {
        private readonly VideoCaptureAPIs _videoCaptureApi = VideoCaptureAPIs.DSHOW;
        private readonly ManualResetEventSlim _writerReset = new(false);
        private readonly VideoCapture _videoCapture;
        private VideoWriter _videoWriter;
        private Thread _writerThread;

        private bool IsVideoCaptureValid => _videoCapture is not null && _videoCapture.IsOpened();

        public Recorder(int deviceIndex, int frameWidth, int frameHeight, double fps)
        {
            _videoCapture = VideoCapture.FromCamera(deviceIndex, _videoCaptureApi);
            _videoCapture.Open(deviceIndex, _videoCaptureApi);

            _videoCapture.FrameWidth = frameWidth;
            _videoCapture.FrameHeight = frameHeight;
            _videoCapture.Fps = fps;
        }

        /// <inheritdoc />
        public void Dispose()
        {
            GC.SuppressFinalize(this);
            Dispose(true);
        }

        ~Recorder()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                StopRecording();

                _videoCapture?.Release();
                _videoCapture?.Dispose();
            }
        }

        public void StartRecording(string path)
        {
            if (_writerThread is not null)
                return;

            if (!IsVideoCaptureValid)
                ThrowHelper.ThrowVideoCaptureNotReadyException();

            _videoWriter = new VideoWriter(path, FourCC.XVID, _videoCapture.Fps, new Size(_videoCapture.FrameWidth, _videoCapture.FrameHeight));

            _writerReset.Reset();
            _writerThread = new Thread(AddCameraFrameToRecordingThread);
            _writerThread.Start();
        }

        public void StopRecording()
        {
            if (_writerThread is not null)
            {
                _writerReset.Set();
                _writerThread.Join();
                _writerThread = null;
                _writerReset.Reset();
            }

            _videoWriter?.Release();
            _videoWriter?.Dispose();
            _videoWriter = null;
        }

        private void AddCameraFrameToRecordingThread()
        {
            var waitTimeBetweenFrames = (int)(1_000 / _videoCapture.Fps);
            using var frame = new Mat();
            while (!_writerReset.Wait(waitTimeBetweenFrames))
            {
                if (!_videoCapture.Read(frame))
                    return;
                _videoWriter.Write(frame);
            }
        }
    }
}

Recording Video from Webcam with OpenCvSharp - Resulting File playback to fast

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文