2013-03-16 4 views
0

저는 오디오를 한 형식에서 다른 형식으로 변환하고 압축하기 위해 DirectShow 필터를 사용해 왔습니다. 저는 GraphStudio와 GraphStudioNext를 사용하여 VB.Net의 필터로 변환하는 필터 그래프를 만듭니다. 그들은 지금까지 항상 위대한 일을 해왔습니다. .wav 파일을 .mp3 파일로 변환하려고합니다. GraphStudio와 다음 필터를 기본 연결과 함께 사용하면 압축 파일을 얻을 수 있습니다. FileSource-Async -> WaveParser -> MPEG Layer3 -> WAV Dest -> Filewriter를 선택합니다.DirectShow MP3 압축

그러나 VB.Net 프로그램에서 동일한 필터를 연결하면 압축이 전혀 없습니다. 필자는 IAMStreamConfig 인터페이스를 사용하여 압축을 설정하려고 시도했지만 프로그램에서 핀을 열거 할 때 GraphStudio에 표시되는 압축을 사용할 수 없습니다. 함수 (일부 설명 포함)는 다음과 같습니다. 어떤 생각, 제안 많이 감사합니다.

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 
+0

왜 붙여 넣기 코드가 주석으로 처리 되었습니까? 어떻게 유용할까요? –

+0

필자는 설명에서 언급 한 것처럼 압축 매개 변수를 설정하려고 시도했기 때문에 누군가가 테스트를 원한다면 어떻게 할 수 있는지 보여줄 수 있습니다. 요점은 IAMStreamConfig를 설정하려고 시도하거나하지 않는 것입니다. GraphStudio에서와 같은 결과를 얻지 못합니다. – user2176065

+1

'1' DirectShowNET.MPEG3Layer 란 무엇입니까? '2' 개별 필터에 SetSyncSource를해서는 안됩니다. 좋지는 않지만 좋지 않을 수 있습니다. –

답변

0

많은 연구를 거친 후에 나는 압축 작업을 graphedit에서와 같이 할 수 없었습니다. 그래서 프로젝트를 완전히 변경하고 절름발이 인코더를 사용하여 변환 작업을 수행하기로 결정했습니다. 그것은 아주 작은 코드로 완벽하게 작동합니다. 새 코드는 다음과 같습니다.

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