Adds a stream to the profile.
public void AddStream(WMStreamConfig Config)
Public Sub AddStream( _ByVal Config As WMStreamConfig _)
public:void AddStream(WMStreamConfig^ Config)
Config
A WMStreamConfig object that contains information about the stream to add.
This method works by copying the stream configuration details into the profile. New streams are created using the CreateNewStream method. Configure the newly created stream using the WMStreamConfig object methods and properties before adding it to the profile using this method. A maximum of 63 streams can be added to a profile. For more information, refer to the Microsoft documentation for IWMProfile.AddStream. To add a script command stream to the file, use the WMScript.EnableScriptStream method.
using Leadtools;using Leadtools.Multimedia;using LeadtoolsMultimediaExamples.Fixtures;public bool _result = false;public ConvertCtrlForm _form = new ConvertCtrlForm();const int CODEC_AUDIO_MSAUDIO = 353; // Microsoft WMAudioconst int CODEC_VIDEO_WMV1 = 827739479; // FOURCC( 'W', 'M', 'V', '1' )public void AddStreamExample(){ConvertCtrl convertctrl = _form.ConvertCtrl;string inFile = Path.Combine(LEAD_VARS.MediaDir, "ConvertCtrl_Source.avi");string outFile = Path.Combine(LEAD_VARS.MediaDir, "WMProfile_RemoveStreamExample.avi");try{WMProfileManager manager = new WMProfileManager();// create a WMProfileconvertctrl.WMProfile = CreateCustomProfile(manager);// run the conversionconvertctrl.SourceFile = inFile;convertctrl.TargetFormat = TargetFormatType.WMV;convertctrl.TargetFile = outFile;convertctrl.StartConvert();// dispose of the managermanager.Dispose();}catch (Exception){_result = false;}// we'll loop on the state and pump messages for this example.// but you should not need to if running from a Windows Forms application.while (convertctrl.State == ConvertState.Running)Application.DoEvents();// check the validity of the output fileMediaInfo mediaInfo = new MediaInfo();try{mediaInfo.SourceFile = outFile;if (mediaInfo.SourceFormat == SourceFormatType.WMV)_result = true;}catch (Exception){_result = false;}}private WMProfile CreateCustomProfile(WMProfileManager manager){WMProfile profile = manager.CreateEmptyProfile(WMT_Version.V9);profile.Name = "Custom Profile";profile.Description = "Custom Profile Description";bool added = AddAudioStream(manager, profile, 1, CODEC_AUDIO_MSAUDIO, 8000, 8000, 1, true);// add 5 video streams with bitrates starting from 32KB/s (256Kbps). Increase the bitrate if you increase the video sizefor (int i = 0; (i <= 4); i++)AddVideoStream(manager, profile, (2 + i), CODEC_VIDEO_WMV1,(1024 * 32) + i * 1024, 400, 300, 5 * (i + 1), 0, 8);// mark all the video streams for mutual exclusionAddMutexObject(profile, 2, 5);return profile;}private bool AddVideoStream(WMProfileManager manager,WMProfile profile,int streamnum,long fourcc,int bitrate,int width,int height,int fps,int quality,int secperkey){WMStreamConfig codecformat;MediaType mediatype;VideoInfoHeader vih;bool candidate = false;VideoInfoHeader candidatevih = new VideoInfoHeader();MediaType candidatemt = null;WMStreamConfig candidatestream = null;bool added = false;byte[] viharr = new byte[88];int codecs = manager.GetCodecInfoCount(Constants.MEDIATYPE_Video);string sinfo = string.Empty;// width and height must be evenif ((width & 1) != 0)width++;if ((height & 1) != 0)height++;// search for matching codec until I find a candidatefor (int codecindex = 0; codecindex < codecs && !candidate; codecindex++){sinfo += manager.GetCodecName(Constants.MEDIATYPE_Video, codecindex);int formats = manager.GetCodecFormatCount(Constants.MEDIATYPE_Video, codecindex);for (int formatindex = 0; formatindex < formats; formatindex++){sinfo += manager.GetCodecFormatDesc(Constants.MEDIATYPE_Video, codecindex, formatindex);codecformat = manager.GetCodecFormat(Constants.MEDIATYPE_Video, codecindex, formatindex);mediatype = codecformat.GetMediaType();if (mediatype.FormatType == Constants.FORMAT_VideoInfo){vih = mediatype.GetVideoFormatData();if (vih.bmiHeader.biCompression == fourcc){candidate = true;candidatevih = vih;candidatemt = mediatype;candidatestream = codecformat;// stop searching if I found a candidatebreak;}}}}if (candidate){// modify the selected codec to support this bitrate and sizecandidatevih.dwBitRate = bitrate;candidatevih.rcSource.Right = width;candidatevih.rcSource.Bottom = height;candidatevih.rcTarget.Right = width;candidatevih.rcTarget.Bottom = height;candidatevih.bmiHeader.biWidth = width;candidatevih.bmiHeader.biHeight = height;// since fps is integer, 10000000/fps always fits in a 32-bit value, so lowpart=10000000/fps and high part is always 0candidatevih.AvgTimePerFrame.lowpart = 10000000 / fps;candidatevih.AvgTimePerFrame.highpart = 0;StructToByteArray(candidatevih, ref viharr);// candidatevih is a copy of the media type, so updating candidatevih is not enough to update the media type.// Call SetVideoFormatData to copy the updated information from candidatevih to the video format data inside candidatemt// If you just change candidatevih without calling candidatemt.SetVideoFormatData,// candidatemt is an empty media type with no width and heightcandidatemt.SetVideoFormatData(candidatevih, null, 0);candidatestream.Quality = quality;candidatestream.MaxKeyFrameSpacing = secperkey;candidatestream.StreamNumber = streamnum;candidatestream.StreamName = "Video Stream";candidatestream.ConnectionName = "Video";candidatestream.Bitrate = bitrate;candidatestream.SetMediaType(candidatemt);profile.AddStream(candidatestream);added = true;}return added;}private bool AddAudioStream(WMProfileManager manager,WMProfile profile,int streamnum,int formattag,int prefbitrate,int samplespersec,int channels,bool withvideo){WMStreamConfig codecformat;MediaType mediatype;WaveFormatEx wfex = new WaveFormatEx();bool added = false;bool candidate = false;WaveFormatEx candidatewfex = new WaveFormatEx();MediaType candidatemt = null;MediaType mt;WMStreamConfig stream;int codecs = manager.GetCodecInfoCount(Constants.MEDIATYPE_Audio);// search for matching codecfor (int codecindex = 0; codecindex < codecs; codecindex++){int formats = manager.GetCodecFormatCount(Constants.MEDIATYPE_Audio, codecindex);for (int formatindex = 0; formatindex < formats; formatindex++){codecformat = manager.GetCodecFormat(Constants.MEDIATYPE_Audio, codecindex, formatindex);mediatype = codecformat.GetMediaType();if (mediatype.FormatType == Constants.FORMAT_WaveFormatEx){ByteArrayToStruct(mediatype.Format, ref wfex);int diff = ((wfex.nAvgBytesPerSec * 8) - prefbitrate);if (diff < 250 && diff > -250&& wfex.nSamplesPerSec == samplespersec&& wfex.nChannels == channels&& wfex.wFormatTag == formattag){if (candidate){if (withvideo){//// For audio/video configurations, we want to// find the smaller blockalign. In this case,// the blockalign is larger, so we want to// use the old format.if (wfex.nBlockAlign <= candidatewfex.nBlockAlign){candidatewfex = wfex;candidatemt = mediatype;}}else if (wfex.nBlockAlign >= candidatewfex.nBlockAlign){candidatewfex = wfex;candidatemt = mediatype;}}else{candidate = true;candidatewfex = wfex;candidatemt = mediatype;}}}}}if (candidate){// modify the selected codec to support this bitrate and formatmt = new MediaType();mt.Type = Constants.MEDIATYPE_Audio;mt.SubType = "{" + String.Format("{0:X8}", formattag) + "-0000-0010-8000-00AA00389B71}";mt.FixedSizeSamples = true;mt.TemporalCompression = false;mt.SampleSize = candidatewfex.nBlockAlign;mt.FormatType = Constants.FORMAT_WaveFormatEx;mt.SetFormatData(-1, candidatemt.Format);stream = profile.CreateNewStream(Constants.MEDIATYPE_Audio);stream.StreamNumber = streamnum;stream.StreamName = "Audio Stream";stream.ConnectionName = "Audio";stream.Bitrate = (candidatewfex.nAvgBytesPerSec * 8);stream.SetMediaType(mt);profile.AddStream(stream);profile.ReconfigStream(stream);added = true;}return added;}private void StructToByteArray(object o, ref byte[] dest){try{//convert the structure to a byte arrayint rawSize = Marshal.SizeOf(o);GCHandle handle = new GCHandle();IntPtr buffer;handle = GCHandle.Alloc(dest, GCHandleType.Pinned);buffer = handle.AddrOfPinnedObject();Marshal.StructureToPtr(o, buffer, false);handle.Free();}catch (Exception ex){throw ex;}}private void ByteArrayToStruct(object buffer, ref WaveFormatEx vih){try{GCHandle handle = new GCHandle();//convert the structure to a byte arrayhandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);IntPtr ptr = handle.AddrOfPinnedObject();vih = (WaveFormatEx)Marshal.PtrToStructure(ptr, typeof(WaveFormatEx));handle.Free();}catch (Exception ex){throw ex;}}private bool AddMutexObject(WMProfile profile, int basestream, int streamcount){WMMutualExclusion excl = profile.CreateNewMutualExclusion();// indicate that the streams differ by bit rateexcl.Type = "{D6E22A01-35DA-11D1-9034-00A0C90349BE}";for (int i = 0; i < streamcount; i++)excl.AddStream(basestream + i);// assign the exclusion object to the profileprofile.AddMutualExclusion(excl);return true;}private bool RemoveMutexObject(WMProfile profile, WMMutualExclusion excl, int basestream, int streamcount){// indicate that the streams differ by bit rateexcl.Type = "{D6E22A01-35DA-11D1-9034-00A0C90349BE}";for (int i = 0; i < streamcount; i++)excl.RemoveStream(basestream + i);// remove the exclusion object from the profileprofile.RemoveMutualExclusion(excl);return true;}static class LEAD_VARS{public const string MediaDir = @"C:\Program Files (x86)\LEAD Technologies\LEADTOOLS 20\Media";}
Imports LeadtoolsImports Leadtools.MultimediaImports LeadtoolsMultimediaExamples.FixturesPublic _result As Boolean = FalsePublic _form As ConvertCtrlForm = New ConvertCtrlForm()Private Const CODEC_AUDIO_MSAUDIO As Integer = 353 ' Microsoft WMAudioPrivate Const CODEC_VIDEO_WMV1 As Integer = 827739479 ' FOURCC( "W"c, "M"c, "V"c, "1"c )Public Sub AddStreamExample()Dim convertctrl As ConvertCtrl = _form.ConvertCtrlDim inFile As String = Path.Combine(LEAD_VARS.MediaDir, "ConvertCtrl_Source.avi")Dim outFile As String = Path.Combine(LEAD_VARS.MediaDir, "WMProfile_RemoveStreamExample.avi")TryDim manager As WMProfileManager = New WMProfileManager()' create a WMProfileconvertctrl.WMProfile = CreateCustomProfile(manager)' run the conversionconvertctrl.SourceFile = inFileconvertctrl.TargetFormat = TargetFormatType.WMVconvertctrl.TargetFile = outFileconvertctrl.StartConvert()' dispose of the managermanager.Dispose()Catch e1 As Exception_result = FalseEnd Try' we'll loop on the state and pump messages for this example.' but you should not need to if running from a Windows Forms application.Do While convertctrl.State = ConvertState.RunningApplication.DoEvents()Loop' check the validity of the output fileDim mediaInfo As MediaInfo = New MediaInfo()TrymediaInfo.SourceFile = outFileIf (mediaInfo.SourceFormat = SourceFormatType.WMV) Then _result = TrueCatch e2 As Exception_result = FalseEnd TryEnd SubPrivate Function CreateCustomProfile(ByVal manager As WMProfileManager) As WMProfileDim profile As WMProfile = manager.CreateEmptyProfile(WMT_Version.V9)profile.Name = "Custom Profile"profile.Description = "Custom Profile Description"Dim added As Boolean = AddAudioStream(manager, profile, 1, CODEC_AUDIO_MSAUDIO, 8000, 8000, 1, True)' add 5 video streams with bitrates starting from 32KB/s (256Kbps). Increase the bitrate if you increase the video sizeDim i As Integer = 0Do While (i <= 4)AddVideoStream(manager, profile, (2 + i), CODEC_VIDEO_WMV1, (1024 * 32) + i * 1024, 400, 300, 5 * (i + 1), 0, 8)i += 1Loop' mark all the video streams for mutual exclusionAddMutexObject(profile, 2, 5)Return profileEnd FunctionPrivate Function AddVideoStream(ByVal manager As WMProfileManager,ByVal profile As WMProfile,ByVal streamnum As Integer,ByVal fourcc As Long,ByVal bitrate As Integer,ByVal width As Integer,ByVal height As Integer,ByVal fps As Integer,ByVal quality As Integer,ByVal secperkey As Integer) As BooleanDim codecformat As WMStreamConfigDim mediatype As MediaTypeDim vih As VideoInfoHeaderDim candidate As Boolean = FalseDim candidatevih As VideoInfoHeader = New VideoInfoHeader()Dim candidatemt As MediaType = NothingDim candidatestream As WMStreamConfig = NothingDim added As Boolean = FalseDim viharr As Byte() = New Byte(87) {}Dim codecs As Integer = manager.GetCodecInfoCount(Leadtools.Multimedia.Constants.MEDIATYPE_Video)Dim sinfo As String = String.Empty' width and height must be evenIf ((width And 1) <> 0) Then width += 1If ((height And 1) <> 0) Then height += 1' search for matching codec until I find a candidateDim codecindex As Integer = 0Do While (codecindex < codecs) And Not candidatesinfo &= manager.GetCodecName(Leadtools.Multimedia.Constants.MEDIATYPE_Video, codecindex)Dim formats As Integer = manager.GetCodecFormatCount(Leadtools.Multimedia.Constants.MEDIATYPE_Video, codecindex)Dim formatindex As Integer = 0Do While formatindex < formatssinfo &= manager.GetCodecFormatDesc(Leadtools.Multimedia.Constants.MEDIATYPE_Video, codecindex, formatindex)codecformat = manager.GetCodecFormat(Leadtools.Multimedia.Constants.MEDIATYPE_Video, codecindex, formatindex)mediatype = codecformat.GetMediaType()If mediatype.FormatType = Leadtools.Multimedia.Constants.FORMAT_VideoInfo Thenvih = mediatype.GetVideoFormatData()If vih.bmiHeader.biCompression = fourcc Thencandidate = Truecandidatevih = vihcandidatemt = mediatypecandidatestream = codecformat' stop searching if I found a candidateExit DoEnd IfEnd Ifformatindex += 1Loopcodecindex += 1LoopIf candidate Then' modify the selected codec to support this bitrate and sizecandidatevih.dwBitRate = bitratecandidatevih.rcSource.Right = widthcandidatevih.rcSource.Bottom = heightcandidatevih.rcTarget.Right = widthcandidatevih.rcTarget.Bottom = heightcandidatevih.bmiHeader.biWidth = widthcandidatevih.bmiHeader.biHeight = height' since fps is integer, 10000000/fps always fits in a 32-bit value, so lowpart=10000000/fps and high part is always 0candidatevih.AvgTimePerFrame.lowpart = CInt(10000000 / fps)candidatevih.AvgTimePerFrame.highpart = 0StructToByteArray(candidatevih, viharr)' candidatevih is a copy of the media type, so updating candidatevih is not enough to update the media type.' Call SetVideoFormatData to copy the updated information from candidatevih to the video format data inside candidatemt' If you just change candidatevih without calling candidatemt.SetVideoFormatData,' candidatemt is an empty media type with no width and heightcandidatemt.SetVideoFormatData(candidatevih, Nothing, 0)candidatestream.Quality = qualitycandidatestream.MaxKeyFrameSpacing = secperkeycandidatestream.StreamNumber = streamnumcandidatestream.StreamName = "Video Stream"candidatestream.ConnectionName = "Video"candidatestream.Bitrate = bitratecandidatestream.SetMediaType(candidatemt)profile.AddStream(candidatestream)added = TrueEnd IfReturn addedEnd FunctionPrivate Function AddAudioStream(ByVal manager As WMProfileManager,ByVal profile As WMProfile,ByVal streamnum As Integer,ByVal formattag As Integer,ByVal prefbitrate As Integer,ByVal samplespersec As Integer,ByVal channels As Integer,ByVal withvideo As Boolean) As BooleanDim codecformat As WMStreamConfigDim mediatype As MediaTypeDim wfex As WaveFormatEx = New WaveFormatEx()Dim added As Boolean = FalseDim candidate As Boolean = FalseDim candidatewfex As WaveFormatEx = New WaveFormatEx()Dim candidatemt As MediaType = NothingDim mt As MediaTypeDim stream As WMStreamConfigDim codecs As Integer = manager.GetCodecInfoCount(Leadtools.Multimedia.Constants.MEDIATYPE_Audio)' search for matching codecDim codecindex As Integer = 0Do While codecindex < codecsDim formats As Integer = manager.GetCodecFormatCount(Leadtools.Multimedia.Constants.MEDIATYPE_Audio, codecindex)Dim formatindex As Integer = 0Do While formatindex < formatscodecformat = manager.GetCodecFormat(Leadtools.Multimedia.Constants.MEDIATYPE_Audio, codecindex, formatindex)mediatype = codecformat.GetMediaType()If mediatype.FormatType = Leadtools.Multimedia.Constants.FORMAT_WaveFormatEx ThenByteArrayToStruct(mediatype.Format, wfex)Dim diff As Integer = ((wfex.nAvgBytesPerSec * 8) - prefbitrate)If diff < 250 AndAlso diff > -250 AndAlso wfex.nSamplesPerSec = samplespersec _AndAlso wfex.nChannels = channels AndAlso wfex.wFormatTag = formattag ThenIf candidate ThenIf withvideo Then'' For audio/video configurations, we want to' find the smaller blockalign. In this case,' the blockalign is larger, so we want to' use the old format.If wfex.nBlockAlign <= candidatewfex.nBlockAlign Thencandidatewfex = wfexcandidatemt = mediatypeEnd IfElseIf wfex.nBlockAlign >= candidatewfex.nBlockAlign Thencandidatewfex = wfexcandidatemt = mediatypeEnd IfElsecandidate = Truecandidatewfex = wfexcandidatemt = mediatypeEnd IfEnd IfEnd Ifformatindex += 1Loopcodecindex += 1LoopIf candidate Then' modify the selected codec to support this bitrate and formatmt = New MediaType()mt.Type = Leadtools.Multimedia.Constants.MEDIATYPE_Audiomt.SubType = "{" & String.Format("{0:X8}", formattag) & "-0000-0010-8000-00AA00389B71}"mt.FixedSizeSamples = Truemt.TemporalCompression = Falsemt.SampleSize = candidatewfex.nBlockAlignmt.FormatType = Leadtools.Multimedia.Constants.FORMAT_WaveFormatExmt.SetFormatData(-1, candidatemt.Format)stream = profile.CreateNewStream(Leadtools.Multimedia.Constants.MEDIATYPE_Audio)stream.StreamNumber = streamnumstream.StreamName = "Audio Stream"stream.ConnectionName = "Audio"stream.Bitrate = (candidatewfex.nAvgBytesPerSec * 8)stream.SetMediaType(mt)profile.AddStream(stream)profile.ReconfigStream(stream)added = TrueEnd IfReturn addedEnd FunctionPrivate Sub StructToByteArray(ByVal o As Object, ByRef dest As Byte())Try'convert the structure to a byte arrayDim rawSize As Integer = Marshal.SizeOf(o)Dim handle As GCHandle = New GCHandle()Dim buffer As IntPtrhandle = GCHandle.Alloc(dest, GCHandleType.Pinned)buffer = handle.AddrOfPinnedObject()Marshal.StructureToPtr(o, buffer, False)handle.Free()Catch ex As ExceptionThrow exEnd TryEnd SubPrivate Sub ByteArrayToStruct(ByVal buffer As Object, ByRef vih As WaveFormatEx)TryDim handle As GCHandle = New GCHandle()'convert the structure to a byte arrayhandle = GCHandle.Alloc(buffer, GCHandleType.Pinned)Dim ptr As IntPtr = handle.AddrOfPinnedObject()vih = CType(Marshal.PtrToStructure(ptr, GetType(WaveFormatEx)), WaveFormatEx)handle.Free()Catch ex As ExceptionThrow exEnd TryEnd SubPrivate Function AddMutexObject(ByVal profile As WMProfile,ByVal basestream As Integer,ByVal streamcount As Integer) As BooleanDim excl As WMMutualExclusion = profile.CreateNewMutualExclusion()' indicate that the streams differ by bit rateexcl.Type = "{D6E22A01-35DA-11D1-9034-00A0C90349BE}"Dim i As Integer = 0Do While i < streamcountexcl.AddStream(basestream + i)i += 1Loop' assign the exclusion object to the profileprofile.AddMutualExclusion(excl)Return TrueEnd FunctionPrivate Function RemoveMutexObject(ByVal profile As WMProfile,ByVal excl As WMMutualExclusion,ByVal basestream As Integer,ByVal streamcount As Integer) As Boolean' indicate that the streams differ by bit rateexcl.Type = "{D6E22A01-35DA-11D1-9034-00A0C90349BE}"Dim i As Integer = 0Do While i < streamcountexcl.RemoveStream(basestream + i)i += 1Loop' remove the exclusion object from the profileprofile.RemoveMutualExclusion(excl)Return TrueEnd FunctionPublic NotInheritable Class LEAD_VARSPublic Const MediaDir As String = "C:\Program Files (x86)\LEAD Technologies\LEADTOOLS 20\Media"End Class
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
