-2
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using DirectShowLib;
using System.Windows.Forms;
using System.Collections.Generic;


namespace Polkan.DataSource
{
    internal class WmvAdapter : ISampleGrabberCB, IDisposable
    {
        #region Fields
        //public Image img;
        private IFilterGraph2 _filterGraph;
        private IMediaControl _mediaCtrl;
        private IMediaEvent _mediaEvent;
        private int _width;
        private int _height;
        private readonly string _outFolder;
        private int _frameId;

        #endregion

        #region Constructors and Destructors

        public WmvAdapter(string file, string outFolder)
        {
            _outFolder = outFolder;
            try
            {
                SetupGraph(file);
            }
            catch
            {
                Dispose();
                MessageBox.Show("A codec is required to load this video file. Please use http://www.headbands.com/gspot/ or search the web for the correct codec");
                throw;
            }
        }

        ~WmvAdapter()
        {
            CloseInterfaces();
        }

        #endregion

        public void Dispose()
        {
            CloseInterfaces();
        }

        public void Start()
        {
            int hr = _mediaCtrl.Run();
            WaitUntilDone();
            DsError.ThrowExceptionForHR(hr);
        }

        public void WaitUntilDone()
        {
            int hr;
            const int eAbort = unchecked((int)0x80004004);

            do
            {

                System.Windows.Forms.Application.DoEvents();
                EventCode evCode;
                hr = _mediaEvent.WaitForCompletion(100, out evCode);
            } while 
                (hr == eAbort);

            DsError.ThrowExceptionForHR(hr);


        }

        /// <summary> build the capture graph for grabber. </summary>
        private void SetupGraph(string file)
        {
            ISampleGrabber sampGrabber = null;
            IBaseFilter capFilter = null;
            IBaseFilter nullrenderer = null;

            _filterGraph = (IFilterGraph2)new FilterGraph();
            _mediaCtrl = (IMediaControl)_filterGraph;
            _mediaEvent = (IMediaEvent)_filterGraph;

            var mediaFilt = (IMediaFilter)_filterGraph;

            try
            {
                // Add the video source
                int hr = _filterGraph.AddSourceFilter(file, "Ds.NET FileFilter", out capFilter);
                DsError.ThrowExceptionForHR(hr);

                // Get the SampleGrabber interface
                sampGrabber = new SampleGrabber() as ISampleGrabber;
                var baseGrabFlt = sampGrabber as IBaseFilter;

                ConfigureSampleGrabber(sampGrabber);

                // Add the frame grabber to the graph
                hr = _filterGraph.AddFilter(baseGrabFlt, "Ds.NET Grabber");
                DsError.ThrowExceptionForHR(hr);

                // ---------------------------------
                // Connect the file filter to the sample grabber

                // Hopefully this will be the video pin, we could check by reading it's mediatype
                IPin iPinOut = DsFindPin.ByDirection(capFilter, PinDirection.Output, 0);

                // Get the input pin from the sample grabber
                IPin iPinIn = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Input, 0);

                hr = _filterGraph.Connect(iPinOut, iPinIn);
                DsError.ThrowExceptionForHR(hr);

                // Add the null renderer to the graph
                nullrenderer = new NullRenderer() as IBaseFilter;
                hr = _filterGraph.AddFilter(nullrenderer, "Null renderer");
                DsError.ThrowExceptionForHR(hr);

                // ---------------------------------
                // Connect the sample grabber to the null renderer

                iPinOut = DsFindPin.ByDirection(baseGrabFlt, PinDirection.Output, 0);
                iPinIn = DsFindPin.ByDirection(nullrenderer, PinDirection.Input, 0);

                hr = _filterGraph.Connect(iPinOut, iPinIn);
                DsError.ThrowExceptionForHR(hr);

                // Turn off the clock. This causes the frames to be sent
                // thru the graph as fast as possible
                hr = mediaFilt.SetSyncSource(null);
                DsError.ThrowExceptionForHR(hr);

                // Read and cache the image sizes
                SaveSizeInfo(sampGrabber);
            }
            finally
            {
                if (capFilter != null)
                {
                    Marshal.ReleaseComObject(capFilter);
                }
                if (sampGrabber != null)
                {
                    Marshal.ReleaseComObject(sampGrabber);
                }
                if (nullrenderer != null)
                {
                    Marshal.ReleaseComObject(nullrenderer);
                }
                GC.Collect();
            }
        }

        private void SaveSizeInfo(ISampleGrabber sampGrabber)
        {
            // Get the media type from the SampleGrabber
            var media = new AMMediaType();
            int hr = sampGrabber.GetConnectedMediaType(media);
            DsError.ThrowExceptionForHR(hr);

            if ((media.formatType != FormatType.VideoInfo) || (media.formatPtr == IntPtr.Zero))
            {
                throw new NotSupportedException("Unknown Grabber Media Format");
            }

            // Grab the size info
            var videoInfoHeader = (VideoInfoHeader)Marshal.PtrToStructure(media.formatPtr, typeof(VideoInfoHeader));
            _width = videoInfoHeader.BmiHeader.Width;
            _height = videoInfoHeader.BmiHeader.Height;

            DsUtils.FreeAMMediaType(media);
            GC.Collect();
        }

        private void ConfigureSampleGrabber(ISampleGrabber sampGrabber)
        {
            var media = new AMMediaType
            {
                majorType = MediaType.Video,
                subType = MediaSubType.RGB24,
                formatType = FormatType.VideoInfo
            };
            int hr = sampGrabber.SetMediaType(media);
            DsError.ThrowExceptionForHR(hr);

            DsUtils.FreeAMMediaType(media);
            GC.Collect();
            hr = sampGrabber.SetCallback(this, 1);
            DsError.ThrowExceptionForHR(hr);
        }

        private void CloseInterfaces()
        {
            try
            {
                if (_mediaCtrl != null)
                {
                    _mediaCtrl.Stop();
                    _mediaCtrl = null;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            if (_filterGraph != null)
            {
                Marshal.ReleaseComObject(_filterGraph);
                _filterGraph = null;
            }
            GC.Collect();
        }

        int ISampleGrabberCB.SampleCB(double sampleTime, IMediaSample pSample)
        {
            Marshal.ReleaseComObject(pSample);
            return 0;
        }

        //add a boolean property to indicate the save-mode
        public bool SaveToDisc { get; set; }
        //the list for the bitmaps
        public List<Bitmap> Images { get; set; }

        int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen)
        {
            using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer))
            {
                bitmap.RotateFlip(RotateFlipType.Rotate180FlipX);
                if (SaveToDisc)
                {
                    String tempFile = _outFolder + _frameId + ".bmp";
                    if (File.Exists(tempFile))
                    {

                    }
                    else
                    {
                        bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"));

                    }
                    _frameId++;
                }
                else
                {
                    if (Images == null)
                        Images = new List<Bitmap>();
                    Images.Add((Bitmap)bitmap.Clone());
                }
            }
            return 0;
        }


    }
}

クラスは、ビデオ ファイルからハード ディスクにフレームを抽出しています。

私の場合、たとえばハードディスク上の47個のファイル。

クラスの下部で、ハードディスクへの保存を行っています。

bitmap.Save(Path.Combine(_outFolder, _frameId.ToString("D6") + ".bmp"));

そして、クラスの上には、WaitUntilDone()という関数があり、役に立ちません。

私が欲しいのは、すべてのファイルをハードディスクに抽出するのが終わったときに、messagebox.showまたはラベルのメッセージだけか、「プロセスが完了しました」というメッセージが表示されることです。

Form1ボタンで、次のようなクラスを使用してイベントimをクリックします。

wmv = new Polkan.DataSource.WmvAdapter(@"d:\VIDEO0040.3gp", sf);
            wmv.SaveToDisc = true;
            wmv.Start();

ビデオ名 sf は、ハードディスク上で抽出するディレクトリです。

次に true にすると、フレームのメモリではなくハードディスクに保存されます。

そしてスタート。

Form1 のボタン クリック イベント wmv.WaitUntilDone(); で行うこともできます。ただし、完了時にメッセージや何かをスローすることはありません。

4

3 に答える 3

1

プロセスが終了したことを UI コンポーネントに伝えるには、イベントを使用する必要があります。

public event EventHandler ProcessFinished; 

メソッド内で、終了したらイベントを発生させます。

if(ProcessFinished != null)
    ProcessFinished(this, EventArgs.Empty);

最後に、プロセスを呼び出しているクラスで:

wmv = new Polkan.DataSource.WmvAdapter(@"d:\VIDEO0040.3gp", sf);
wmv.SaveToDisc = true;
wmv.ProcessFinished += OnProcessFinished;
wmv.Start();

OnProcessFinished は次のようになります。

public void OnProcessFinished(object sender, EventArgs e){
    MessageBox.Show("done!");
}

それが役に立てば幸い。

于 2012-06-30T16:36:26.870 に答える
1

DoEvents() を使用する独自の WaitUntil メソッドを実装しました。これはトリッキーで、正しく行うのは難しいです。

関連するコードをBackgoundworkerに移動する方がはるかに簡単です。その後、Completed イベントを使用して終了を通知します。

于 2012-06-30T16:40:00.677 に答える
0

WaitUntilDoneファイル全体が最後まで再生されるまでループし、ビデオ フレームごとにすべての呼び出しが中断されます。MessageBox.Showの一番下に追加できWaitUntilDoneます。

外側のメッセージ ループに制御を渡さずにこのループを回避するために、アプリケーションは通常、別の方法でそれを行います。これらはIMediaEventEx.SetNotifyWindow、グラフ イベントをフィルター処理するためにサブスクライブし、再生が完了したときにウィンドウ メッセージを受け取ります。つまり、ファイルの処理が完了すると、メッセージが送信され、ハンドラーが必要な処理を実行する機会が与えられます。

ここでおそらくより簡単なオプションは、何もせずWaitUntilDone、代わりにタイマーを使用して、たとえば 1 秒に 1 回、IMediaEvent.WaitForCompletion(0, ...処理が完了したかどうかを確認することです。

于 2012-06-30T17:23:56.453 に答える