最后,这是我为解决问题所做的工作,以便能够以“简单的方式”检索两个输出:
#Region " Process Info "
' CoreConverter Process Information.
Private Shared CoreConverter As New Process() With { _
.StartInfo = New ProcessStartInfo With { _
.CreateNoWindow = True, _
.UseShellExecute = False, _
.RedirectStandardError = True, _
.RedirectStandardOutput = True, _
.StandardErrorEncoding = System.Text.Encoding.Unicode, _
.StandardOutputEncoding = System.Text.Encoding.Unicode}}
#End Region
' Some code
' Blah blah blah...
#Region " Run Converter Procedure "
Private Shared Sub Run_CoreConverter()
CoreConverter.StartInfo.FileName = CoreConverter_Location
CoreConverter.Start()
While Not CoreConverter.HasExited
OutputCharacter = ChrW(CoreConverter.StandardOutput.Read)
If OutputCharacter = "*" Then
CurrentProgress += 1 ' Maximum value is 59, so a ProgressBar Maximum property value would be 59.
RaiseEvent PercentDone(CurrentProgress, Nothing)
End If
If CurrentProgress = 59 Then
' I store only the last line 'cause it has interesting information:
' Example message: Conversion completed in 30 seconds x44 realtime encoding
StandardOutput = CoreConverter.StandardOutput.ReadToEnd.Trim
End If
End While
ErrorOutput = CoreConverter.StandardError.ReadToEnd
Select Case CoreConverter.ExitCode
Case 0 : RaiseEvent Exited(StandardOutput, Nothing) ' Return StandardOutput
Case Else : RaiseEvent Exited(ErrorOutput, Nothing) ' Return ErrordOutput
End Select
CurrentProgress = 0
OutputCharacter = Nothing
StandardOutput = Nothing
ErrorOutput = Nothing
' CoreConverter.Close()
End Sub
#End Region
更新:
如果有人需要,这是完整的课程:
#Region " CoreConverter Helper "
' [ CoreConverter Helper ]
'
' // By Elektro H@cker
'
'
' Instructions:
'
' 1. Add the "CoreConverter.exe" into the project,
' together with the dbPoweramp Effects and Codec folders.
'
' Examples :
'
' -------------------
' CONVERT FILE TO MP3
' -------------------
' CoreConverter.Convert_To_MP3("C:\Input.wav", "C:\Output.mp3", _
' CoreConverter.Lame_Bitrate.kbps_320, _
' CoreConverter.Lame_Bitrate_Mode.cbr, _
' CoreConverter.Lame_Profile.SLOW, _
' CoreConverter.Lame_Quality.Q0_Maximum, _
' CoreConverter.Lame_Khz.Same_As_Source, _
' CoreConverter.Lame_Channels.auto, _
' { _
' CoreConverter.DSP_Effects.Delete_Output_File_on_Error, _
' CoreConverter.DSP_Effects.Recycle_Source_File_After_Conversion _
' }, _
' False, _
' CoreConverter.Priority.normal)
'
' -------------------
' CONVERT FILE TO WAV
' -------------------
' CoreConverter.Convert_To_WAV_Uncompressed("C:\Input.mp3", "C:\Output.wav", _
' CoreConverter.WAV_Uncompressed_Bitrate.Same_As_Source, _
' CoreConverter.WAV_Uncompressed_Khz.Same_As_Source, _
' CoreConverter.WAV_Uncompressed_Channels.Same_As_Source, , False)
'
' -------------------
' CONVERT FILE TO WMA
' -------------------
' CoreConverter.Convert_To_WMA("C:\Input.mp3", "C:\Output.wma", _
' CoreConverter.WMA_9_2_BitRates.Kbps_128, _
' CoreConverter.WMA_9_2_Khz.Khz_44, _
' CoreConverter.WMA_9_2_Channels.stereo, , False)
'
' ------
' EVENTS
' ------
' Public WithEvents Converter As New CoreConverter()
'
' Sub Converter_Progress(Progress As Integer, e As EventArgs) Handles Converter.PercentDone
' ProgressBar1.Maximum = 59
' ProgressBar1.Step = 1
' ProgressBar1.PerformStep()
' End Sub
'
' Sub Converter_Message(Message As String, e As EventArgs) Handles Converter.Exited
' ProgressBar1.Value = 0
' MessageBox.Show(Message)
' End Sub
Public Class CoreConverter : Implements IDisposable
#Region " Variables "
' <summary>
' Gets or sets CoreConverter.exe executable path.
' </summary>
Public Shared CoreConverter_Location As String = ".\CoreConverter.exe"
' Stores the CoreConverter process progress
Private Shared CurrentProgress As Integer = 0
' Stores the CoreConverter process StandarOutput
Private Shared StandardOutput As String = String.Empty
' Stores the CoreConverter process ErrorOutput
Private Shared ErrorOutput As String = String.Empty
' Stores the next output character
Private Shared OutputCharacter As Char = Nothing
' Stores the DSP Effects formatted string
Private Shared Effects As String = String.Empty
#End Region
#Region " Events "
' <summary>
' Event raised when conversion progress changes.
' </summary>
Public Shared Event PercentDone As EventHandler(Of PercentDoneEventArgs)
Public Class PercentDoneEventArgs : Inherits EventArgs
Public Property Progress As Integer
End Class
' <summary>
' Event raised when CoreConverter process has exited.
' </summary>
Public Shared Event Exited As EventHandler(Of ExitedEventArgs)
Public Class ExitedEventArgs : Inherits EventArgs
Public Property Message As String
End Class
#End Region
#Region " Process Info "
' CoreConverter Process Information.
Private Shared CoreConverter As New Process() With { _
.StartInfo = New ProcessStartInfo With { _
.CreateNoWindow = True, _
.UseShellExecute = False, _
.RedirectStandardError = True, _
.RedirectStandardOutput = True, _
.StandardErrorEncoding = System.Text.Encoding.Unicode, _
.StandardOutputEncoding = System.Text.Encoding.Unicode}}
#End Region
#Region " CoreConverter Enumerations "
' Priority level of CoreConverter.exe
Enum Priority
idle
low
normal
high
End Enum
' DSP Effects
Public Enum DSP_Effects
Delete_Output_File_on_Error ' Delete failed conversion (not deletes source file).
Delete_Source_File_After_Conversion ' Delete source file after conversion.
Recycle_Source_File_After_Conversion ' Send source file to recycle bin after conversion.
Karaoke_Remove_Voice ' Remove voice from file.
Karaoke_Remove_Instrument ' Remove instruments from file.
Reverse ' Reverse complete audio file.
Write_Silence ' Write silence at start of file.
End Enum
#End Region
#Region " Codec Enumerations "
Enum Lame_Bitrate
kbps_8 = 8
kbps_16 = 16
kbps_24 = 24
kbps_32 = 32
kbps_40 = 40
kbps_48 = 48
kbps_56 = 56
kbps_64 = 64
kbps_80 = 80
kbps_96 = 96
kbps_112 = 112
kbps_128 = 128
kbps_144 = 144
kbps_160 = 160
kbps_192 = 192
kbps_224 = 224
kbps_256 = 256
kbps_320 = 320
End Enum
Enum Lame_Bitrate_Mode
cbr
abr
End Enum
Enum Lame_Profile
NORMAL
FAST
SLOW
End Enum
Enum Lame_Quality
Q0_Maximum = 0
Q1 = 1
Q2 = 2
Q3 = 3
Q4 = 4
Q5 = 5
Q6 = 6
Q7 = 7
Q8 = 8
Q9_Minimum = 9
End Enum
Enum Lame_Khz
Same_As_Source
khz_8 = 8000
khz_11 = 11000
khz_12 = 12000
khz_16 = 16000
khz_22 = 22000
khz_24 = 24000
khz_32 = 32000
khz_44 = 44100
khz_48 = 48000
End Enum
Enum Lame_Channels
auto
mono
stereo
joint_stereo
forced_joint_stereo
forced_stereo
dual_channels
End Enum
Enum WAV_Uncompressed_Bitrate
Same_As_Source
bits_8 = 8
bits_16 = 16
bits_24 = 24
bits_32 = 32
End Enum
Enum WAV_Uncompressed_Khz
Same_As_Source
khz_8 = 8000
khz_11 = 11000
khz_12 = 12000
khz_16 = 16000
khz_22 = 22000
khz_24 = 24000
khz_32 = 32000
khz_44 = 44100
khz_48 = 48000
khz_96 = 96000
khz_192 = 192000
End Enum
Enum WAV_Uncompressed_Channels
Same_As_Source
Channels_1_Mono = 1
Channels_2_Stereo = 2
Channels_3 = 3
Channels_4_Quadraphonic = 4
Channels_5_Surround = 5
Channels_6_Surround_DVD = 6
Channels_7 = 7
Channels_8_Theater = 8
End Enum
Enum WMA_9_2_BitRates
Kbps_12 = 12
Kbps_16 = 16
Kbps_20 = 20
Kbps_22 = 22
Kbps_24 = 24
Kbps_32 = 32
Kbps_40 = 40
Kbps_48 = 48
Kbps_64 = 64
Kbps_80 = 80
Kbps_96 = 96
Kbps_128 = 128
Kbps_160 = 160
Kbps_192 = 192
Kbps_256 = 256
Kbps_320 = 320
End Enum
Enum WMA_9_2_Khz
Khz_8 = 8
Khz_16 = 16
Khz_22 = 22
Khz_32 = 32
Khz_44 = 44
Khz_48 = 48
End Enum
Enum WMA_9_2_Channels
mono
stereo
End Enum
#End Region
#Region " Codec Procedures "
#Region " MP3 Lame "
' <summary>
' Converts a file to MP3 using Lame codec.
' </summary>
Public Shared Sub Convert_To_MP3(ByVal In_File As String, _
ByVal Out_File As String, _
ByVal Bitrate As Lame_Bitrate, _
ByVal Bitrate_Mode As Lame_Bitrate_Mode, _
ByVal Encoding_Profile As Lame_Profile, _
ByVal Quality As Lame_Quality, _
ByVal Khz As Lame_Khz, _
ByVal Channels As Lame_Channels, _
Optional ByVal DSP_Effects() As DSP_Effects = Nothing, _
Optional ByVal Update_Tag As Boolean = True, _
Optional ByVal Priority As Priority = Priority.normal, _
Optional ByVal Processor As Short = 1)
If DSP_Effects IsNot Nothing Then
Effects = String.Empty
For X As Integer = 0 To DSP_Effects.Length - 1
Effects &= String.Format(" -dspeffect{0}={1}", _
X + 1, _
Format_DSP_Effect(DSP_Effects(X).ToString))
Next
End If
CoreConverter.StartInfo.Arguments = String.Format("-infile=""{0}"" -outfile=""{1}"" -convert_to=""mp3 (Lame)"" {2} {3} -priority=""{4}"" -processor=""{5}"" -b {6} {7} -encoding=""{8}"" -freq=""{9}"" -channels=""{10}"" --noreplaygain --extracli=""-q {11}""", _
In_File, _
Out_File, _
If(Not Update_Tag, "-noidtag", ""), _
Effects, _
Priority.ToString, _
Processor, _
CInt(Bitrate), _
"--" & Bitrate_Mode.ToString, _
Encoding_Profile.ToString, _
If(Khz = Lame_Khz.Same_As_Source, "", CInt(Khz)), _
If(Channels = Lame_Channels.auto, "", Channels), _
CInt(Quality))
Run_CoreConverter()
End Sub
#End Region
#Region " WAV Uncompressed "
' <summary>
' Converts a file to WAV
' </summary>
Public Shared Sub Convert_To_WAV_Uncompressed(ByVal In_File As String, _
ByVal Out_File As String, _
ByVal Bitrate As WAV_Uncompressed_Bitrate, _
ByVal Khz As WAV_Uncompressed_Khz, _
ByVal Channels As WAV_Uncompressed_Channels, _
Optional ByVal DSP_Effects() As DSP_Effects = Nothing, _
Optional ByVal Update_Tag As Boolean = True, _
Optional ByVal Priority As Priority = Priority.normal, _
Optional ByVal Processor As Short = 1)
If DSP_Effects IsNot Nothing Then
Effects = String.Empty
For X As Integer = 0 To DSP_Effects.Length - 1
Effects &= String.Format(" -dspeffect{0}={1}", _
X + 1, _
Format_DSP_Effect(DSP_Effects(X).ToString))
Next
End If
CoreConverter.StartInfo.Arguments = String.Format("-infile=""{0}"" -outfile=""{1}"" -convert_to=""Wave"" {2} {3} -priority=""{4}"" -processor=""{5}"" -compression=""PCM"" -bits=""{6}"" -freq=""{7}"" -channels=""{8}""", _
In_File, _
Out_File, _
If(Not Update_Tag, "-noidtag", ""), _
Effects, _
Priority.ToString, _
Processor, _
If(Bitrate = WAV_Uncompressed_Bitrate.Same_As_Source, "", CInt(Bitrate)), _
If(Khz = WAV_Uncompressed_Khz.Same_As_Source, "", CInt(Khz)), _
If(Channels = WAV_Uncompressed_Channels.Same_As_Source, "", CInt(Channels)))
Run_CoreConverter()
End Sub
#End Region
#Region " WMA 9.2 "
' <summary>
' Converts a file to WMA 9.2
' </summary>
Public Shared Sub Convert_To_WMA(ByVal In_File As String, _
ByVal Out_File As String, _
ByVal Bitrate As WMA_9_2_BitRates, _
ByVal Khz As WMA_9_2_Khz, _
ByVal Channels As WMA_9_2_Channels, _
Optional ByVal DSP_Effects() As DSP_Effects = Nothing, _
Optional ByVal Update_Tag As Boolean = True, _
Optional ByVal Priority As Priority = Priority.normal, _
Optional ByVal Processor As Short = 1, _
Optional ByVal LogFile As String = ".\CoreConverter.log")
If DSP_Effects IsNot Nothing Then
Effects = String.Empty
For X As Integer = 0 To DSP_Effects.Length - 1
Effects &= String.Format(" -dspeffect{0}={1}", _
X + 1, _
Format_DSP_Effect(DSP_Effects(X).ToString))
Next
End If
CoreConverter.StartInfo.Arguments = String.Format("-infile=""{0}"" -outfile=""{1}"" -convert_to=""Windows Media Audio 10"" {2} {3} -priority=""{4}"" -processor=""{5}"" -codec=""Windows Media Audio 9.2"" -settings=""{6} kbps, {7} kHz, {8} CBR""",
In_File, _
Out_File, _
If(Not Update_Tag, "-noidtag", ""), _
Effects, _
Priority.ToString, _
Processor, _
CInt(Bitrate), _
CInt(Khz), _
Channels.ToString)
Run_CoreConverter()
End Sub
#End Region
#End Region
#Region " Run Converter Procedure "
Private Shared Sub Run_CoreConverter()
CoreConverter.StartInfo.FileName = CoreConverter_Location
CoreConverter.Start()
While Not CoreConverter.HasExited
OutputCharacter = ChrW(CoreConverter.StandardOutput.Read)
If OutputCharacter = "*" Then
CurrentProgress += 1 ' Maximum value is 59, so a ProgressBar Maximum property value would be 59.
RaiseEvent PercentDone(CurrentProgress, Nothing)
End If
If CurrentProgress = 59 Then
' I store only the last line 'cause it has interesting information:
' Example message: Conversion completed in 30 seconds x44 realtime encoding
StandardOutput = CoreConverter.StandardOutput.ReadToEnd.Trim
End If
End While
' Stores the Error Message (If any)
ErrorOutput = CoreConverter.StandardError.ReadToEnd
Select Case CoreConverter.ExitCode
Case 0 : RaiseEvent Exited(StandardOutput, Nothing) ' Return StandardOutput
Case Else : RaiseEvent Exited(ErrorOutput, Nothing) ' Return ErrordOutput
End Select
CurrentProgress = 0
OutputCharacter = Nothing
StandardOutput = Nothing
ErrorOutput = Nothing
CoreConverter.Close()
End Sub
#End Region
#Region " Miscellaneous functions "
' <summary>
' Checks if CoreConverter process is avaliable.
' </summary>
Public Shared Function Is_Avaliable() As Boolean
Return IO.File.Exists(CoreConverter_Location)
End Function
' Returns the DSP Effects formatted string
Private Shared Function Format_DSP_Effect(ByVal Effect As String)
Select Case Effect
Case "Reverse" : Return """Reverse"""
Case "Delete_Output_File_on_Error" : Return """Delete Destination File on Error="""
Case "Recycle_Source_File_After_Conversion" : Return """Delete Source File=-recycle"""
Case "Delete_Source_File_After_Conversion" : Return """Delete Source File="""
Case "Karaoke_Remove_Voice" : Return """Karaoke (Voice_ Instrument Removal)="""
Case "Karaoke_Remove_Instrument" : Return """Karaoke (Voice_ Instrument Removal)=-i"""
Case "Write_Silence" : Return """Write Silence=-lengthms={qt}2000{qt}""" ' 2 seconds
Case Else : Return String.Empty
End Select
End Function
#End Region
#Region " Dispose Objects "
Public Sub Dispose() Implements IDisposable.Dispose
' CoreConverter_Location = Nothing ' Don't touch
OutputCharacter = Nothing
StandardOutput = Nothing
ErrorOutput = Nothing
CurrentProgress = Nothing
Effects = Nothing
CoreConverter.Close()
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
#End Region