Convert wav files to mp3 files

fmellaerts

Member
Joined
Jan 10, 2011
Messages
9
Programming Experience
Beginner
Hi,

For a long time, I was looking for a way to convert wav files to mp3 using VB.net 2008/2010.
Here's a solution, made by myself, using lame.exe 3.98.4.
The CMD window is hidden, and a progressbar will show you the conversion state.
This is an easy solution, fast and, what's most important, it's working.
That's why I share it here, because I couldn't find a working solution on the internet.
If somebody can tell me how to use the lame_enc.dll to do this, please do.
All comments on this project are more than welcome.
I hope I can make somebody happy with this code.

Freddy Mellaerts
 

Attachments

  • wav2mp3.zip
    107.8 KB · Views: 180
Last edited by a moderator:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,438
Location
Norway
Programming Experience
10+
Using Process/ProcessStartInfo to start the lame.exe process is probably the easiest way to do this.

lame_enc.dll is not a .Net assembly, so you would need to P/Invoke. I don't think that is worth the effort since the command line tool is readily available.
 

xty2004

New member
Joined
Jul 14, 2011
Messages
1
Programming Experience
Beginner
You defenetely made me happy, thank you for sharing, i was looking on the internet like you too :) Have a nice day
 

littlebigman

Well-known member
Joined
Jan 5, 2010
Messages
75
Programming Experience
Beginner
Using Process/ProcessStartInfo to start the lame.exe process is probably the easiest way to do this. lame_enc.dll is not a .Net assembly, so you would need to P/Invoke. I don't think that is worth the effort since the command line tool is readily available.

I'm a VB.Net newbie and have no idea what that means: Doe it mean using the code in wav2mp3.zip‎ is more complicated than needed and I should just work with Lame/mpg123 through the command line instead?

Thank you.
 

littlebigman

Well-known member
Joined
Jan 5, 2010
Messages
75
Programming Experience
Beginner
For those interested, here's some working code:

VB.NET:
Const LAME = "C:\Apps\Lame 3.99\lame.exe"

Private Function RunLame(ByVal arg As String, ByRef OutResult As String, ByRef ErrResult As String) As Integer
    Dim myProcess As Process = New Process()
    MessageBox.Show(LAME)

    With myProcess.StartInfo
        .FileName = LAME
        .Arguments = arg

        ' start the process in a hidden window
        .WindowStyle = ProcessWindowStyle.Hidden
        .CreateNoWindow = True
        .RedirectStandardOutput = True
        .RedirectStandardError = True
        .UseShellExecute = False
    End With

    myProcess.Start()

    ' Attach to stdout and stderr.
    Dim std_out As StreamReader = myProcess.StandardOutput()
    Dim std_err As StreamReader = myProcess.StandardError()

    ' Return results
    OutResult = std_out.ReadToEnd()
    ErrResult = std_err.ReadToEnd()

    ' Clean up.
    std_out.Close()
    std_err.Close()

    'In case process is stuck
    myProcess.WaitForExit(1000)
    Dim Result As Integer
    Result = myProcess.ExitCode
    If Not myProcess.HasExited Then
        myProcess.Kill()
    Else
        myProcess.Close()
    End If

    'BAD: "No process is associated with this object"
    'Return myProcess.ExitCode
    Return Result
End Function

Private Sub Form1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
    Dim Result As Integer
    Dim OutResult As String = ""
    Dim ErrResult As String = ""

    Try
        Result = RunLame("--preset medium " & ControlChars.Quote & "C:\Temp\test.wav" & ControlChars.Quote, OutResult, ErrResult)
        RichTextBox1.AppendText(OutResult)
        RichTextBox1.AppendText(ErrResult)
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try

End Sub
 
Top Bottom