Vb.net DirectShow MP3压缩
我一直在使用DirectShow过滤器将音频从一种格式转换和压缩到另一种格式。我使用GraphStudio和GraphStudio文本创建一个过滤器图形,然后在VB.Net中将其转换为过滤器。到目前为止,他们一直工作得很好。我正在尝试将.wav文件转换为.mp3。使用GraphStudio和以下过滤器以及默认连接链接在一起,我得到了压缩文件。文件源异步-->WaveParser-->MPEG层3-->WAV Dest-->文件编写器 然而,当我在VB.Net程序中将相同的过滤器连接在一起时,我根本没有得到压缩。我甚至尝试使用IAMStreamConfig接口设置压缩,但是当我在程序中枚举管脚时,GraphStudio中显示的压缩不可用。功能(包括一些注释)如下所示。任何想法、建议都将不胜感激Vb.net DirectShow MP3压缩,vb.net,mp3,directshow,wav,file-conversion,Vb.net,Mp3,Directshow,Wav,File Conversion,我一直在使用DirectShow过滤器将音频从一种格式转换和压缩到另一种格式。我使用GraphStudio和GraphStudio文本创建一个过滤器图形,然后在VB.Net中将其转换为过滤器。到目前为止,他们一直工作得很好。我正在尝试将.wav文件转换为.mp3。使用GraphStudio和以下过滤器以及默认连接链接在一起,我得到了压缩文件。文件源异步-->WaveParser-->MPEG层3-->WAV Dest-->文件编写器 然而,当我在VB.Net程序中将相同的过滤器连接在一起时,我根
Private Function PlayInitMP3(ByVal path As String, ByVal dstPath As String) As Integer
Dim asyncReader As DirectShowNET.IBaseFilter = Nothing
Dim WaveParser As DirectShowNET.IBaseFilter = Nothing
'Dim ACMWrapper As DirectShowNET.IBaseFilter = Nothing
Dim MPEG3Wrapper As DirectShowNET.IBaseFilter = Nothing
Dim WavDestWrapper As DirectShowNET.IBaseFilter = Nothing
Dim fileSource As DirectShowNET.IFileSourceFilter = Nothing
Dim fileDestination As DirectShowNET.IBaseFilter = Nothing
Dim result As Integer = -1
Dim srcFileInfo As System.IO.FileInfo = New FileInfo(path)
Service1.nlog.LogError(0, "Starting Init", "", "")
'Set to Null Renderer
Try
result = WaveParser.SetSyncSource(DBNull.Value)
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "SetSyncSource-WaveParser", result.ToString, "")
'result = ACMWrapper.SetSyncSource(DBNull.Value)
'Marshal.ThrowExceptionForHR(result)
'Service1.nlog.LogError(0, "SetSyncSource-ACMWrapper", result.ToString, "")
result = MPEG3Wrapper.SetSyncSource(DBNull.Value)
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "SetSyncSource-MPEG3Wrapper", result.ToString, "")
result = WavDestWrapper.SetSyncSource(DBNull.Value)
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "SetSyncSource-WavDestWrapper", result.ToString, "")
Catch ex As Exception
End Try
Try
If IsNothing(Me.graphBuilder) = False Then
Marshal.ReleaseComObject(Me.mediaSeeking)
Marshal.ReleaseComObject(Me.mediaControl)
Marshal.ReleaseComObject(Me.graphBuilder)
Me.mediaSeeking = Nothing
Me.mediaControl = Nothing
Me.graphBuilder = Nothing
End If
' Create IGraphBuilder instance
Me.graphBuilder = New DirectShowNET.FilterGraph()
' Create MediaControl
' File source filter addition
asyncReader = New DirectShowNET.AsyncReader
result = graphBuilder.AddFilter(asyncReader, System.IO.Path.GetFileName(path))
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "SourceFilter", result.ToString, "")
' Add Wave Parser
WaveParser = New DirectShowNET.WaveParser
result = Me.graphBuilder.AddFilter(WaveParser, "Wave Parser")
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "WaveParser", result.ToString, "")
'' Add ACMWrapper
'ACMWrapper = New DirectShowNET.ACMWrapper
'result = Me.graphBuilder.AddFilter(ACMWrapper, "ACMWrapper Parser")
'Marshal.ThrowExceptionForHR(result)
'Service1.nlog.LogError(0, "ACMWrapper", result.ToString, "")
' Add MPEG3Wrapper
MPEG3Wrapper = New DirectShowNET.MPEG3Layer
result = Me.graphBuilder.AddFilter(MPEG3Wrapper, "MPEG3Layer Parser")
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "MPEG3Wrapper", result.ToString, "")
' Add WAVDest
WavDestWrapper = New DirectShowNET.WaveDestination
result = Me.graphBuilder.AddFilter(WavDestWrapper, "WaveDestination")
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "WaveDestination", result.ToString, "")
' FileWriter filter addition
fileDestination = New DirectShowLib.FileWriter
Dim fs As DirectShowLib.IFileSinkFilter = DirectCast(fileDestination, DirectShowLib.IFileSinkFilter)
result = fs.SetFileName(dstPath, Nothing)
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "FileWriter", result.ToString, "")
result = Me.graphBuilder.AddFilter(DirectCast(fileDestination, DirectShowNET.IBaseFilter), "Filewriter")
Marshal.ThrowExceptionForHR(result)
' Set the Filename using the IFileSinkFilter interface
' The file is loaded.
fileSource = asyncReader
result = fileSource.Load(path, Nothing)
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "SourceFileLoad", result.ToString, "")
' Wave Parser is connected with FileSourceFilter.
Try
result = ConnectFilters(Me.graphBuilder, asyncReader, WaveParser)
Marshal.ThrowExceptionForHR(result)
Catch ex As Exception
Service1.nlog.LogError(0, "ConnectWaveParser", ex.Message, "")
End Try
Service1.nlog.LogError(0, "WaveParser Result", result.ToString, "")
' Wave Parser is connected with MPEG3.
Try
result = ConnectFilters(Me.graphBuilder, WaveParser, MPEG3Wrapper)
Marshal.ThrowExceptionForHR(result)
Catch ex As Exception
Service1.nlog.LogError(0, "ConnectWaveParser", ex.Message, "")
End Try
Service1.nlog.LogError(0, "WaveParser Result", result.ToString, "")
'' ACMParser is connected with WaveParser.
'Try
' result = ConnectFilters(Me.graphBuilder, WaveParser, ACMWrapper)
' Marshal.ThrowExceptionForHR(result)
'Catch ex As Exception
' Service1.nlog.LogError(0, "ConnectACMWrapper", ex.Message, "")
'End Try
'Service1.nlog.LogError(0, "ACMWrapper Result", result.ToString, "")
''COMPRESSION
'Try
' 'Configure the Compression Stream Variables
' Dim NewConfig As IAMStreamConfig
' Dim mp3Pin As IPin
' Dim ppEnum As IEnumPins
' Dim aPin(1) As IPin
' Dim dPin As PinDirection = PinDirection.Output
' Dim nPin As Integer
' Service1.nlog.LogError(0, "Beginning ACMWrapper Compression", "", "")
' ACMWrapper.EnumPins(ppEnum)
' While ppEnum.Next(1, aPin, nPin) = 0
' mp3Pin = aPin(0)
' mp3Pin.QueryDirection(dPin)
' If dPin = PinDirection.Output Then
' Exit While
' End If
' End While
' NewConfig = TryCast(mp3Pin, IAMStreamConfig)
' If NewConfig Is Nothing Then
' Service1.nlog.LogError(0, "NewConfig Is nothing", "", "")
' End If
' Service1.nlog.LogError(0, "After TryCast", "", "")
' Dim nCap As Integer
' Dim sCap As Integer
' Dim pmt As New DirectShowLib.AMMediaType
' Service1.nlog.LogError(0, "After pmt", "", "")
' NewConfig.GetNumberOfCapabilities(nCap, sCap)
' Dim i As Integer
' Dim sArray(sCap) As System.IntPtr
' Dim intPtr As IntPtr = Marshal.AllocCoTaskMem(sCap)
' Dim audioFormatACM As WaveFormatEx
' For i = 0 To nCap - 1
' NewConfig.GetStreamCaps(i, pmt, intPtr)
' audioFormatACM = New WaveFormatEx()
' Marshal.PtrToStructure(pmt.formatPtr, audioFormatACM)
' 'Print Audio Format
' Service1.nlog.LogError(0, "Index:", i.ToString, "")
' Service1.nlog.LogError(0, "AudioFormat:BitsPerSample", audioFormatACM.wBitsPerSample.ToString, "")
' Service1.nlog.LogError(0, "AudioFormat:nSamplesPerSec", audioFormatACM.nSamplesPerSec.ToString, "")
' Service1.nlog.LogError(0, "AudioFormat:AvgBytesPerSec", audioFormatACM.nAvgBytesPerSec.ToString, "")
' ''Once find the proper format then save it and exit
' If audioFormatACM.nAvgBytesPerSec = 8000 AndAlso audioFormatACM.nSamplesPerSec = 8000 Then
' 'audioFormatACM.nAvgBytesPerSec = 8000
' 'audioFormatACM.wBitsPerSample = 8
' 'audioFormatACM.nSamplesPerSec = 8000
' Marshal.StructureToPtr(audioFormatACM, pmt.formatPtr, True)
' Exit For
' End If
' Next
' 'Use Selected format from enum to set
' If NewConfig.SetFormat(pmt) <> 0 Then
' Service1.nlog.LogError(0, "ACM Setformat Error", "", "")
' End If
' 'See if Successfully Set
' audioFormatACM = New WaveFormatEx()
' Marshal.PtrToStructure(pmt.formatPtr, audioFormatACM)
' If NewConfig.GetFormat(pmt) <> 0 Then
' Service1.nlog.LogError(0, "ACM GetFormat Error", "", "")
' End If
' Service1.nlog.LogError(0, "ACM Updated AvgBytesPerSec", audioFormatACM.nAvgBytesPerSec.ToString, "")
' Service1.nlog.LogError(0, "ACM Updated SamplesPerSec", audioFormatACM.nSamplesPerSec.ToString, "")
' 'Release Memory
'Catch ex As Exception
' Service1.nlog.LogError(0, "Compression Error:", ex.Message, ex.Source)
'End Try
'' WaveParser is connected with MPEG3.
'Try
' result = ConnectFilters(Me.graphBuilder, ACMWrapper, MPEG3Wrapper)
' Marshal.ThrowExceptionForHR(result)
'Catch ex As Exception
' Service1.nlog.LogError(0, "Connect MPEG3Wrapper", ex.Message, "")
'End Try
'Service1.nlog.LogError(0, "MPEG3Wrapper Result", result.ToString, "")
'COMPRESSION
Try
'Configure the Compression Stream Variables
Dim NewConfig1 As IAMStreamConfig
Dim mp3Pin1 As IPin
Dim ppEnum1 As IEnumPins
Dim aPin1(1) As IPin
Dim dPin1 As PinDirection = PinDirection.Output
Dim nPin1 As Integer
MPEG3Wrapper.EnumPins(ppEnum1)
While ppEnum1.Next(1, aPin1, nPin1) = 0
mp3Pin1 = aPin1(0)
mp3Pin1.QueryDirection(dPin1)
If dPin1 = PinDirection.Output Then
Exit While
End If
End While
NewConfig1 = TryCast(mp3Pin1, IAMStreamConfig)
If NewConfig1 Is Nothing Then
Service1.nlog.LogError(0, "NewConfig Is nothing", "", "")
End If
Service1.nlog.LogError(0, "After TryCast", "", "")
Dim nCap1 As Integer
Dim sCap1 As Integer
Dim pmt1 As New DirectShowLib.AMMediaType
Service1.nlog.LogError(0, "After pmt", "", "")
NewConfig1.GetNumberOfCapabilities(nCap1, sCap1)
Dim i As Integer
Dim sArray1(sCap1) As System.IntPtr
Dim intPtr1 As IntPtr = Marshal.AllocCoTaskMem(sCap1)
Dim audioFormat As MPEGLAYER3WAVEFORMAT
For i = 0 To nCap1 - 1
NewConfig1.GetStreamCaps(i, pmt1, intPtr1)
audioFormat = New MPEGLAYER3WAVEFORMAT()
Marshal.PtrToStructure(pmt1.formatPtr, audioFormat)
'Print Audio Format
Service1.nlog.LogError(0, "Index:", i.ToString, "")
Service1.nlog.LogError(0, "AudioFormat:BitsPerSample", audioFormat.wfx.wBitsPerSample.ToString, "")
Service1.nlog.LogError(0, "AudioFormat:nSamplesPerSec", audioFormat.wfx.nSamplesPerSec.ToString, "")
Service1.nlog.LogError(0, "AudioFormat:AvgBytesPerSec", audioFormat.wfx.nAvgBytesPerSec.ToString, "")
'Once find the proper format then save it and exit
'If audioFormat.wfx.nAvgBytesPerSec = 8000 AndAlso audioFormat.wfx.wBitsPerSample = 8 Then
' audioFormat.wfx.nAvgBytesPerSec = 8000
' 'audioFormat.wfx.wBitsPerSample = 8
' 'audioFormat.wfx.wFormatTag = 85
' 'audioFormat.wfx.nSamplesPerSec = 1000
' Service1.nlog.LogError(0, "MP3 Selected Format AvgBytesPerSec", audioFormat.wfx.nSamplesPerSec.ToString, "")
' Marshal.StructureToPtr(audioFormat, pmt1.formatPtr, True)
' Exit For
'End If
Next
'Use Selected format from enum to set
'If NewConfig1.SetFormat(pmt1) <> 0 Then
' Service1.nlog.LogError(0, "Setformat Error:", NewConfig1.SetFormat(pmt1).ToString, "")
'End If
'See if Successfully Set
audioFormat = New MPEGLAYER3WAVEFORMAT()
pmt1 = New AMMediaType
If NewConfig1.GetFormat(pmt1) <> 0 Then
Service1.nlog.LogError(0, "GetFormat Error", "", "")
End If
Marshal.PtrToStructure(pmt1.formatPtr, audioFormat)
Service1.nlog.LogError(0, "Updated AvgBytesPerSec", audioFormat.wfx.nAvgBytesPerSec.ToString, "")
Service1.nlog.LogError(0, "Updated SamplesPerSec", audioFormat.wfx.nSamplesPerSec.ToString, "")
Service1.nlog.LogError(0, "Updated FormatTag", audioFormat.wfx.wFormatTag.ToString, "")
Catch ex As Exception
Service1.nlog.LogError(0, "Compression Error:", ex.Message, ex.Source)
End Try
' MPEG3 is connected with WavDest.
Try
result = ConnectFilters(Me.graphBuilder, MPEG3Wrapper, WavDestWrapper)
Marshal.ThrowExceptionForHR(result)
Catch ex As Exception
Service1.nlog.LogError(0, "Connect WavDestWrapper", ex.Message, "")
End Try
Service1.nlog.LogError(0, "WaveDestWrapper Result", result.ToString, "")
' Connect to the File Writer
result = ConnectFilters(Me.graphBuilder, WavDestWrapper, fileDestination)
Marshal.ThrowExceptionForHR(result)
Service1.nlog.LogError(0, "ConnectFileWriter", result.ToString, "")
'Actually Write The File
' The IMediaSeeking interface is acquired.
Me.mediaSeeking = Me.graphBuilder
Me.mediaSeeking.GetDuration(Me.duration)
Me.duration /= DSS_MEDIA_TIME ' MEDIA_TIME to Millisecond
Service1.nlog.LogError(0, "Duration=", Me.duration.ToString, "")
Catch ex As Exception
Marshal.ReleaseComObject(Me.graphBuilder)
Me.graphBuilder = Nothing
Service1.nlog.LogError(0, "Exception MP3Convert", ex.Message, "")
Finally
asyncReader = Nothing
WaveParser = Nothing
'ACMWrapper = Nothing
MPEG3Wrapper = Nothing
WavDestWrapper = Nothing
fileSource = Nothing
fileDestination = Nothing
End Try
Service1.nlog.LogError(0, "Ending Init MP3", " Result:", result.ToString)
Return result
End Function
Private Function PlayInitMP3(ByVal path作为字符串,ByVal dstPath作为字符串)作为整数
Dim asyncReader作为DirectShowNET.IBaseFilter=无
Dim WaveParser作为DirectShowNET.IBaseFilter=无
'Dim ACMWrapper As DirectShowNET.IBaseFilter=无
Dim MPEG3Wrapper作为DirectShowNET.IBaseFilter=无
Dim WaveDestWrapper作为DirectShowNET.IBaseFilter=无
Dim fileSource作为DirectShowNET.IFileSourceFilter=Nothing
Dim fileDestination作为DirectShowNET.IBaseFilter=无
Dim结果为整数=-1
Dim srcFileInfo As System.IO.FileInfo=新文件信息(路径)
Service1.nlog.LogError(0,“开始初始化”,“开始初始化”)
'设置为空
尝试
结果=WaveParser.SetSyncSource(DBNull.Value)
Marshal.ThroweExceptionForHR(结果)
Service1.nlog.LogError(0,“SetSyncSource WaveParser”,result.ToString,“”)
'result=ACMWrapper.SetSyncSource(DBNull.Value)
'Marshal.ThroweExceptionForHR(结果)
'Service1.nlog.LogError(0,“SetSyncSource ACMWrapper”,result.ToString,“”)
结果=MPEG3Wrapper.SetSyncSource(DBNull.Value)
Marshal.ThroweExceptionForHR(结果)
Service1.nlog.LogError(0,“SetSyncSource-MPEG3Wrapper”,result.ToString,“”)
结果=WavDestWrapper.SetSyncSource(DBNull.Value)
Marshal.ThroweExceptionForHR(结果)
Service1.nlog.LogError(0,“SetSyncSource WavDestWrapper”,result.ToString,”)
特例
结束尝试
尝试
如果没有(Me.graphBuilder)=False,那么
Marshal.ReleaseComObject(Me.mediaSeeking)
Marshal.ReleaseComObject(Me.mediaControl)
Marshal.ReleaseComObject(Me.graphBuilder)
Me.media=无
Me.mediaControl=无
Me.graphBuilder=无
如果结束
'创建IGraphBuilder实例
Me.graphBuilder=新的DirectShowNET.FilterGraph()
'创建媒体控件
'文件源筛选器添加
asyncReader=新建DirectShowNET.asyncReader
结果=graphBuilder.AddFilter(asyncReader,System.IO.Path.GetFileName(Path))
Marshal.ThroweExceptionForHR(结果)
Service1.nlog.LogError(0,“SourceFilter”,result.ToString,“”)
'添加波形分析器
WaveParser=新建DirectShowNET.WaveParser
结果=Me.graphBuilder.AddFilter(WaveParser,“Wave Parser”)
Marshal.ThroweExceptionForHR(结果)
Service1.nlog.LogError(0,“WaveParser”,result.ToString,“”)
“”添加ACMWrapper
'ACMWrapper=New DirectShowNET.ACMWrapper
'result=Me.graphBuilder.AddFilter(ACMWrapper,“ACMWrapper解析器”)
'Marshal.ThroweExceptionForHR(结果)
'Service1.nlog.LogError(0,“ACMWrapper”,result.ToString,“”)
'添加MPEG3包装器
MPEG3Wrapper=新的DirectShowNET.MPEG3层
结果=Me.graphBuilder.AddFilter(MPEG3Wrapper,“MPEG3Layer解析器”)
Marshal.ThroweExceptionForHR(结果)
Service1.nlog.LogError(0,“MPEG3Wrapper”,result.ToString,“”)
“添加WAVDest
WavDestWrapper=New DirectShowNET.WaveDestination
结果=Me.graphBuilder.AddFilter(WaveDestWrapper,“WaveDestination”)
Marshal.ThroweExceptionForHR(结果)
Service1.nlog.LogError(0,“WaveDestination”,result.ToString,“”)
'FileWriter筛选器添加
fileDestination=New DirectShowLib.FileWriter
Dim fs作为DirectShowLib.IFileSinkFilter=DirectCast(文件目的地,DirectShowLib.IFileSinkFilter)
结果=fs.SetFileName(dstPath,无)
Marshal.ThroweExceptionForHR(结果)
Service1.nlog.LogError(0,“FileWriter”,result.ToString,“”)
结果=Me.graphBuilder.AddFilter(DirectCast(文件目的地,DirectShowNET.IBaseFilter),“文件编写器”)
Marshal.ThroweExceptionForHR(结果)
'使用IFileSinkFilter接口设置文件名
'文件已加载。
fileSource=asyncReader
结果=fileSource.Load(路径,无)
Marshal.ThroweExceptionForHR(结果)
Service1.nlog.LogError(0,“SourceFileLoad”,result.ToString,“”)
'Wave解析器已与FileSourceFilter连接。
尝试
结果=连接过滤器(Me.graphBuilder、asyncReader、WaveParser)
Marshal.ThroweExceptionForHR(结果)
特例
Service1.nlog.LogError(0,“ConnectWaveParser”,例如消息“”)
结束尝试
Service1.nlog.LogError(0,“WaveParser结果”,Result.ToString,”)
'波形分析器与MPEG3连接。
尝试
结果=连接过滤器(Me.graphBuilder、WaveParser、MPEG3Wrapper)
Marshal.ThroweExceptionForHR(结果)
特例
Service1.nlog.LogError(0,“ConnectWaveParser”,例如消息“”)
结束尝试
Service1.nlog.LogError(0,“WaveParser结果”,Result.ToString,”)
“”ACMParser已与WaveParser连接。
“试试看
'result=ConnectFilters(Me.graphBuilder、WaveParser、ACMWrapper)
“元帅,ThroweExcept
Private Function PlayInitMP3(ByVal WaveFile As String, ByVal MP3File As String) As Integer
Try
'Convert WAV to MP3 using Lame
Dim WaveFileInfo As New FileInfo(WaveFile)
Dim MP3FileInfo As New FileInfo(MP3File)
Dim strLameLine As String
Dim LameExe As String = ConfigurationManager.AppSettings("LameExe") '"C:\scribe\lame\lame.exe -h"
Dim sBlank As String = " "
Dim waitTime As Integer = System.Convert.ToInt16(ConfigurationManager.AppSettings("LameWait")) '-1
strLameLine = LameExe & sBlank & WaveFileInfo.FullName & sBlank & MP3FileInfo.FullName
Shell(strLameLine, AppWinStyle.Hide, True, waitTime)
'Debug
Service1.nlog.LogError(0, WaveFileInfo.Name, strLameLine, "LAME MP3 Conversion Complete")
Catch ex As Exception
End Try
End Function