private static void UpdateAudioPart(IMediaDet mediaDet, MediaDescription mediaDesc) { int hr = 0; AMMediaType mediaType = new AMMediaType(); hr = mediaDet.get_StreamMediaType(mediaType); DsError.ThrowExceptionForHR(hr); mediaDesc.audioSubType = mediaType.subType; double streamLength; hr = mediaDet.get_StreamLength(out streamLength); DsError.ThrowExceptionForHR(hr); mediaDesc.audioLength = TimeSpan.FromSeconds(streamLength); if (mediaType.formatType == FormatType.WaveEx) { WaveFormatEx waveFormatEx = (WaveFormatEx)Marshal.PtrToStructure(mediaType.formatPtr, typeof(WaveFormatEx)); mediaDesc.channels = waveFormatEx.nChannels; mediaDesc.samplesPerSec = ((float)waveFormatEx.nSamplesPerSec) / 1000; mediaDesc.bitsPerSample = waveFormatEx.wBitsPerSample; } }
private static void UpdateVideoPart(IMediaDet mediaDet, MediaDescription mediaDesc) { int hr = 0; AMMediaType mediaType = new AMMediaType(); hr = mediaDet.get_StreamMediaType(mediaType); DsError.ThrowExceptionForHR(hr); mediaDesc.videoSubType = mediaType.subType; double streamLength; hr = mediaDet.get_StreamLength(out streamLength); DsError.ThrowExceptionForHR(hr); mediaDesc.videoLength = TimeSpan.FromSeconds(streamLength); if (mediaType.formatType == FormatType.VideoInfo) { VideoInfoHeader videoHeader = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader)); mediaDesc.resolution = new Size(videoHeader.BmiHeader.Width, videoHeader.BmiHeader.Height); mediaDesc.bitsPerPixel = videoHeader.BmiHeader.BitCount; mediaDesc.fourCC = FourCCToString(videoHeader.BmiHeader.Compression); } }
private void MainForm_DragDrop(object sender, DragEventArgs e) { // Retrieve the files droped into the form string[] fileNames = (string[])e.Data.GetData("FileNameW"); // Get the description of this file MediaDescription mediaDesc = MediaDetector.GetDescription(fileNames[0]); // Display the returned description propertyGrid1.SelectedObject = mediaDesc; pictureBox1.Image = mediaDesc.Snapshot; }
public static MediaDescription GetDescription(string fileName) { int hr = 0; MediaDescription mediaDesc = new MediaDescription(); IMediaDet mediaDet = null; try { // Create the DirectShow's MediaDet mediaDet = (IMediaDet)new MediaDet(); hr = mediaDet.put_Filename(fileName); if (hr < 0) { MessageBox.Show("This file is not supprted by MediaDet", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); DsError.ThrowExceptionForHR(hr); } mediaDesc.fileName = fileName; int streamCount; hr = mediaDet.get_OutputStreams(out streamCount); DsError.ThrowExceptionForHR(hr); for (int i = 0; i < streamCount; i++) { hr = mediaDet.put_CurrentStream(i); DsError.ThrowExceptionForHR(hr); Guid streamType; hr = mediaDet.get_StreamType(out streamType); DsError.ThrowExceptionForHR(hr); if (streamType == MediaType.Audio) UpdateAudioPart(mediaDet, mediaDesc); else if (streamType == MediaType.Video) UpdateVideoPart(mediaDet, mediaDesc); else continue; } if (mediaDesc.videoSubType != Guid.Empty) mediaDesc.snapshot = GetSnapshot(mediaDet, mediaDesc.resolution.Width, mediaDesc.resolution.Height, mediaDesc.videoLength.TotalSeconds / 2); } finally { if (mediaDet != null) Marshal.ReleaseComObject(mediaDet); } return mediaDesc; }
private static void UpdateVideoPart(IMediaDet mediaDet, MediaDescription mediaDesc) { int hr = 0; AMMediaType mediaType = new AMMediaType(); hr = mediaDet.get_StreamMediaType(mediaType); DsError.ThrowExceptionForHR(hr); mediaDesc.videoSubType = mediaType.subType; double streamLength; hr = mediaDet.get_StreamLength(out streamLength); DsError.ThrowExceptionForHR(hr); mediaDesc.videoLength = TimeSpan.FromSeconds(streamLength); if (mediaType.formatType == FormatType.VideoInfo) { VideoInfoHeader videoHeader = (VideoInfoHeader)Marshal.PtrToStructure(mediaType.formatPtr, typeof(VideoInfoHeader)); mediaDesc.resolution = new Size(videoHeader.BmiHeader.Width, videoHeader.BmiHeader.Height); mediaDesc.bitsPerPixel = videoHeader.BmiHeader.BitCount; mediaDesc.fourCC = FourCCToString(videoHeader.BmiHeader.Compression); } }
private static void UpdateAudioPart(IMediaDet mediaDet, MediaDescription mediaDesc) { int hr = 0; AMMediaType mediaType = new AMMediaType(); hr = mediaDet.get_StreamMediaType(mediaType); DsError.ThrowExceptionForHR(hr); mediaDesc.audioSubType = mediaType.subType; double streamLength; hr = mediaDet.get_StreamLength(out streamLength); DsError.ThrowExceptionForHR(hr); mediaDesc.audioLength = TimeSpan.FromSeconds(streamLength); if (mediaType.formatType == FormatType.WaveEx) { WaveFormatEx waveFormatEx = (WaveFormatEx)Marshal.PtrToStructure(mediaType.formatPtr, typeof(WaveFormatEx)); mediaDesc.channels = waveFormatEx.nChannels; mediaDesc.samplesPerSec = ((float)waveFormatEx.nSamplesPerSec) / 1000; mediaDesc.bitsPerSample = waveFormatEx.wBitsPerSample; } }
public static MediaDescription GetDescription(string fileName) { int hr = 0; MediaDescription mediaDesc = new MediaDescription(); IMediaDet mediaDet = null; try { // Create the DirectShow's MediaDet mediaDet = (IMediaDet) new MediaDet(); hr = mediaDet.put_Filename(fileName); if (hr < 0) { MessageBox.Show("This file is not supprted by MediaDet", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); DsError.ThrowExceptionForHR(hr); } mediaDesc.fileName = fileName; int streamCount; hr = mediaDet.get_OutputStreams(out streamCount); DsError.ThrowExceptionForHR(hr); for (int i = 0; i < streamCount; i++) { hr = mediaDet.put_CurrentStream(i); DsError.ThrowExceptionForHR(hr); Guid streamType; hr = mediaDet.get_StreamType(out streamType); DsError.ThrowExceptionForHR(hr); if (streamType == MediaType.Audio) { UpdateAudioPart(mediaDet, mediaDesc); } else if (streamType == MediaType.Video) { UpdateVideoPart(mediaDet, mediaDesc); } else { continue; } } if (mediaDesc.videoSubType != Guid.Empty) { mediaDesc.snapshot = GetSnapshot(mediaDet, mediaDesc.resolution.Width, mediaDesc.resolution.Height, mediaDesc.videoLength.TotalSeconds / 2); } } finally { if (mediaDet != null) { Marshal.ReleaseComObject(mediaDet); } } return(mediaDesc); }