首页 新闻 会员 周边 捐助

DirectShowLib里的例子turner,只有实时显示图像,和录视频。请问怎样在录像文件里加上音频,实时里也播放

0
悬赏园豆:100 [已解决问题] 解决于 2011-09-30 19:06

DirectShowLib里的例子turner,只有实时显示图像,和录视频。请问怎样在录像文件里加上音频,实时里也播放声音。?谢谢!

我加了音频压缩连到输出文件 部分,但程序死机了。

            //Create the file writer part of the graph. SetOutputFileName does this for us, and returns the mux and sink
            IBaseFilter mux;
            IFileSinkFilter sink;
            hr = captureGraphBuilder.SetOutputFileName(MediaSubType.Avi, get_SaveFileName(), out mux, out sink);//textBox1.Text
            DsError.ThrowExceptionForHR(hr);

            //Connect the device and compressor to the mux to render the capture part of the graph
            hr = captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Video, theDevice, theCompressor, mux);//            DsError.ThrowExceptionForHR(hr);

            hr = captureGraphBuilder.RenderStream(PinCategory.Capture, MediaType.Audio, theDevice, theCompressor_A, mux);//我加的            DsError.ThrowExceptionForHR(hr);//我加的 

999ny999的主页 999ny999 | 初学一级 | 园豆:0
提问于:2011-09-27 18:09
< >
分享
最佳答案
1
    AMMediaType media = new AMMediaType();
// Get the video device and add it to the filter graph
if ( VideoDevice != null )
{
videoDeviceFilter = (IBaseFilter) Marshal.BindToMoniker( VideoDevice.MonikerString );
hr = graphBuilder.AddFilter( videoDeviceFilter, "Video Capture Device" );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );


media.majorType = MediaType.Video;
media.subType = MediaSubType.RGB24;
media.formatType = FormatType.VideoInfo; // ???
hr = sampGrabber.SetMediaType( media );
if( hr < 0 )
Marshal.ThrowExceptionForHR( hr );

hr = graphBuilder.AddFilter( baseGrabFlt, "Ds.NET Grabber" );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
}

// Get the audio device and add it to the filter graph
if ( AudioDevice != null )
{
audioDeviceFilter = (IBaseFilter) Marshal.BindToMoniker( AudioDevice.MonikerString );
hr = graphBuilder.AddFilter( audioDeviceFilter, "Audio Capture Device" );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
}

// Get the video compressor and add it to the filter graph
if ( VideoCompressor != null )
{
videoCompressorFilter = (IBaseFilter) Marshal.BindToMoniker( VideoCompressor.MonikerString );
hr = graphBuilder.AddFilter( videoCompressorFilter, "Video Compressor" );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
}

// Get the audio compressor and add it to the filter graph
if ( AudioCompressor != null )
{
audioCompressorFilter = (IBaseFilter) Marshal.BindToMoniker( AudioCompressor.MonikerString );
hr = graphBuilder.AddFilter( audioCompressorFilter, "Audio Compressor" );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
}

// Retrieve the stream control interface for the video device
// FindInterface will also add any required filters
// (WDM devices in particular may need additional
// upstream filters to function).

// Try looking for an interleaved media type
object o;
cat = PinCategory.Capture;
med = MediaType.Interleaved;
Guid iid = typeof(IAMStreamConfig).GUID;
hr = captureGraphBuilder.FindInterface(
ref cat, ref med, videoDeviceFilter, ref iid, out o );

if ( hr != 0 )
{
// If not found, try looking for a video media type
med = MediaType.Video;
hr = captureGraphBuilder.FindInterface(
ref cat, ref med, videoDeviceFilter, ref iid, out o );

if ( hr != 0 )
o = null;
}
videoStreamConfig = o as IAMStreamConfig;

// Retrieve the stream control interface for the audio device
o = null;
cat = PinCategory.Capture;
med = MediaType.Audio ;
iid = typeof(IAMStreamConfig).GUID;
hr = captureGraphBuilder.FindInterface(
ref cat, ref med, audioDeviceFilter, ref iid, out o );
if ( hr != 0 )
o = null;
audioStreamConfig = o as IAMStreamConfig;

// Retreive the media control interface (for starting/stopping graph)
mediaControl = (IMediaControl) graphBuilder;

// Reload any video crossbars
if ( videoSources != null ) videoSources.Dispose(); videoSources = null;

// Reload any audio crossbars
if ( audioSources != null ) audioSources.Dispose(); audioSources = null;

// Reload any property pages exposed by filters
if ( propertyPages != null ) propertyPages.Dispose(); propertyPages = null;

// Reload capabilities of video device
videoCaps = null;

// Reload capabilities of video device
audioCaps = null;

// Retrieve TV Tuner if available
o = null;
cat = PinCategory.Capture;
med = MediaType.Interleaved;
iid = typeof(IAMTVTuner).GUID;
hr = captureGraphBuilder.FindInterface(
ref cat, ref med, videoDeviceFilter, ref iid, out o );
if ( hr != 0 )
{
med = MediaType.Video ;
hr = captureGraphBuilder.FindInterface(
ref cat, ref med, videoDeviceFilter, ref iid, out o );
if ( hr != 0 )
o = null;
}
IAMTVTuner t = o as IAMTVTuner;
if ( t != null )
tuner = new Tuner( t );


videoInfoHeader = (VideoInfoHeader) Marshal.PtrToStructure( media.formatPtr, typeof(VideoInfoHeader) );
Marshal.FreeCoTaskMem( media.formatPtr ); media.formatPtr = IntPtr.Zero;

hr = sampGrabber.SetBufferSamples( false );
if( hr == 0 )
hr = sampGrabber.SetOneShot( false );
if( hr == 0 )
hr = sampGrabber.SetCallback( null, 0 );
if( hr < 0 )
Marshal.ThrowExceptionForHR( hr );
/*
// ----------- VMR 9 -------------------
//## check out samples\inc\vmrutil.h :: RenderFileToVMR9

IBaseFilter vmr = null;
if ( ( VideoDevice != null ) && ( previewWindow != null ) )
{
vmr = (IBaseFilter) Activator.CreateInstance( Type.GetTypeFromCLSID( Clsid.VideoMixingRenderer9, true ) );
hr = graphBuilder.AddFilter( vmr, "VMR" );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

IVMRFilterConfig9 vmrFilterConfig = (IVMRFilterConfig9) vmr;
hr = vmrFilterConfig.SetRenderingMode( VMRMode9.Windowless );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

IVMRWindowlessControl9 vmrWindowsless = (IVMRWindowlessControl9) vmr;
hr = vmrWindowsless.SetVideoClippingWindow( previewWindow.Handle );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );
}
//-------------------------------------------

// ---------- SmartTee ---------------------

IBaseFilter smartTeeFilter = (IBaseFilter) Activator.CreateInstance( Type.GetTypeFromCLSID( Clsid.SmartTee, true ) );
hr = graphBuilder.AddFilter( smartTeeFilter, "Video Smart Tee" );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

// Video -> SmartTee
cat = PinCategory.Capture;
med = MediaType.Video;
hr = captureGraphBuilder.RenderStream( ref cat, ref med, videoDeviceFilter, null, smartTeeFilter );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

// smarttee -> mux
cat = PinCategory.Capture;
med = MediaType.Video;
hr = captureGraphBuilder.RenderStream( ref cat, ref med, smartTeeFilter, null, muxFilter );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

// smarttee -> vmr
cat = PinCategory.Preview;
med = MediaType.Video;
hr = captureGraphBuilder.RenderStream( ref cat, ref med, smartTeeFilter, null, vmr );
if( hr < 0 ) Marshal.ThrowExceptionForHR( hr );

// -------------------------------------
*/
// Update the state now that we are done

给分吧。哥们。

收获园豆:100
沐海 | 初学一级 |园豆:124 | 2011-09-28 10:20
其他回答(1)
0

请问一下,我下载的源码(vb.net)。为什么不能截图(黑屏)?

Public Function Click() As IntPtr      

Dim hr As Integer         ' get ready to wait for new image      

   m_PictureReady.Reset()      

   m_ipBuffer = Marshal.AllocCoTaskMem(Math.Abs(m_stride) * m_videoHeight)

        Try      

       m_WantOne = True

            ' If we are using a still pin, ask for a picture       

      If m_VidControl IsNot Nothing Then                 ' Tell the camera to send an image       

          hr = m_VidControl.SetMode(m_pinStill, VideoControlFlags.Trigger)                 DsError.ThrowExceptionForHR(hr)        

     End If

            ' Start waiting        

     If Not m_PictureReady.WaitOne(9000, False) Then       

          Throw New Exception("Timeout waiting to get picture")      

       End If      

   Catch         

    Marshal.FreeCoTaskMem(m_ipBuffer)        

     m_ipBuffer = IntPtr.Zero       

      Throw      

   End Try

        ' Got one      

   Return m_ipBuffer   

  End Function

视频显示正常,但调用截图时黑屏

 m_ip = cam.Click()
Dim b As New Bitmap(cam.Width, cam.Height, 1920, PixelFormat.Format24bppRgb, m_ip)

ayshi | 园豆:191 (初学一级) | 2016-11-29 21:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册