Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / Visual Basic
Article

VB.NET LameMP3 Shell

Rate me:
Please Sign up or sign in to vote.
3.73/5 (17 votes)
22 Sep 20042 min read 137.1K   2.3K   35   32
How to use lame.exe, a thread, and get a progress update while encoding a file.

Sample Image - LameShell.gif

Introduction

The idea for LameShell came from my wish to be able to resample MP3 files in VB.NET. The goal was: "Whatever valid file goes in, out comes a 32kbit encoded MP3". I started looking around and I really didn’t like the solutions I found. You either had to deal with Interop or unmanaged code, it would cost, the interfaces where out of date or they where just too much. So I created this wrapper that allows me to have all sorts of status updates while Lame encodes the MP3. All you have to do in the application is supply the source file, the destination file and options you need.

Background

While there are some .NET wrappers around the LAME encoder lame_enc.dll, I decided to go a different route – a simple wrapper around lame.exe. There are so many options in there you could expose easily if you wanted to, or just directly type into your source if you have a simple goal – like I had. You can find out more about LAME here and you will need lame.exe to use this class. (lame.exe is not included with the source code)

Using the code

Using the code is very simple; you supply the filenames and options. Additionally, you can supply the path to lame.exe if it isn't in your application directory.

VB
Dim WithEvents _lameShell As New LameShell
[...]
_lameShell.InFile = Application.StartupPath & "\test.mp3"
_lameShell.OutFile = Application.StartupPath & "\testOut.mp3"
_lameShell.Options = "-b 32"
_lameShell.Start()

Add some event handlers:

VB
Private Sub _lameShell_Done() Handles _lameShell.Done
    lblFeedback.Text = "Done"
End Sub

Private Sub _lameShell_Canceled() Handles _lameShell.Canceled
    lblFeedback.Text = "Canceled/Error"
    pBar.Value = 0
End Sub

Private Sub _lameShell_Progress(ByRef Progress As LameProgress) _
                                      Handles _lameShell.Progress

    If pBar.Maximum <> Progress.FrameMax Then
        pBar.Value = 0
        pBar.Maximum = Progress.FrameMax
    Else
        pBar.Value = Progress.FrameCurrent
    End If
    lblFeedback.Text = Progress.PercentDone & "%" & " ETA:" & Progress.ETA

End Sub

That's all there is to it.

How it Works

Essentially, you create a new System.Diagnostics.Process with the needed ProcessStartInfo.

VB
Private _startInfo As New ProcessStartInfo
[...]
'need this stuff to hide the window and redirect the output
_startInfo.FileName = "lame.exe"
_startInfo.UseShellExecute = False
_startInfo.RedirectStandardOutput = True
_startInfo.RedirectStandardError = True
_startInfo.CreateNoWindow = True

And you create a Reader in a different thread that keeps reading the output of lame.exe.

VB
Private _lameThread As System.Threading.Thread
[...]
_lameThread = New System.Threading.Thread(AddressOf LameReader)
_lameThread.IsBackground = True
_lameThread.Name = "LameReader"
_lameThread.Start()
[...]

Private Sub LameReader()
    Dim oneLine As String
    _lameProcess.Start()
    
    oneLine = _lameProcess.StandardError.ReadLine()
    While Not oneLine Is Nothing
        '[...] Analyze Line
        
        'this call is blocking... thats why we are using a thread
        oneLine = _lameProcess.StandardError.ReadLine()
    End While
End Sub

Points of Interest

One thing I haven’t figured out is why the thread that is launched to read the info from lame.exe does not throw a Threading.ThreadAbortException when the application exits. According to MSDN, it should when IsBackround = true. For now, you are safe if you make sure to call LameShell.Cancel() if your application is closing.

Make sure not to depend on the Closing or Closed event of a form (like in the example) if you use Application.Exit anywhere. Those won’t fire if you use Application.Exit. That's why I would have liked the Threading.ThreadAbortException to fire to clean up on all circumstances.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUsing lame.exe from other than root directory fails Pin
tweber201228-Aug-18 0:08
tweber201228-Aug-18 0:08 
AnswerRe: Using lame.exe from other than root directory fails Pin
tweber201228-Aug-18 4:53
tweber201228-Aug-18 4:53 
QuestionCan someone write another LAME Shell for VB.NET 2017? Pin
Member 120944039-Apr-18 9:42
Member 120944039-Apr-18 9:42 
AnswerRe: Can someone write another LAME Shell for VB.NET 2017? Pin
tweber201228-Aug-18 4:53
tweber201228-Aug-18 4:53 
GeneralDidn't work in Windows 8.1 Pin
Gerry Lindsay6-Dec-14 6:54
Gerry Lindsay6-Dec-14 6:54 
QuestionMP3 to Wav? Pin
rspercy654-Sep-12 4:06
rspercy654-Sep-12 4:06 
QuestionThis works great. Can anyone help with looping through a folder? Pin
iismoove11-Aug-12 6:12
iismoove11-Aug-12 6:12 
GeneralWorks with VB 2008 Express Pin
pawnslinger10-May-11 14:04
pawnslinger10-May-11 14:04 
GeneralOnly works OK without debugger Pin
Onur Guzel29-Jun-10 8:15
Onur Guzel29-Jun-10 8:15 
GeneralRe: Only works OK without debugger Pin
Titan15-Aug-14 20:34
Titan15-Aug-14 20:34 
QuestionWant to Convert from more file formats Pin
kjhaveri2-Dec-09 5:52
kjhaveri2-Dec-09 5:52 
GeneralFor Visual Basic 2008 Pin
technobase18-Oct-09 9:10
technobase18-Oct-09 9:10 
GeneralYou legend! No hassle mp3 convertor! Pin
seanclancy14-Jun-09 11:39
seanclancy14-Jun-09 11:39 
QuestionMP3 to wav conversion? Pin
rspercy658-Jun-09 6:48
rspercy658-Jun-09 6:48 
GeneralSinple and useful. ¡Thank you very much! Pin
JesusAbizanda8-Jan-09 22:13
JesusAbizanda8-Jan-09 22:13 
QuestionGetting Started Pin
Paul Rothenheber24-Oct-08 10:48
Paul Rothenheber24-Oct-08 10:48 
QuestionC# version? Pin
itjavelin16-Nov-07 8:59
itjavelin16-Nov-07 8:59 
QuestionAnd Tag informations? Pin
GTClove3-Aug-07 8:11
GTClove3-Aug-07 8:11 
QuestionVB6... is it possible? Pin
The Rockas1-Dec-06 14:53
The Rockas1-Dec-06 14:53 
AnswerRe: VB6... is it possible? Pin
VorTechS7-Jan-07 21:16
VorTechS7-Jan-07 21:16 
GeneralHelp launching various instances at a time Pin
mikeperaltag13-Nov-06 19:02
mikeperaltag13-Nov-06 19:02 
GeneralAdvice... Pin
snort4-Jul-06 2:06
snort4-Jul-06 2:06 
Funny idea.... works very well.. and well programmed Smile | :)

continue !

Olivier
GeneralRe: Advice... Pin
vnesteem9-Jul-06 18:30
vnesteem9-Jul-06 18:30 
QuestionAny suggestion on how to record wav / mp3 files ? Pin
Parik Advani12-Jun-06 3:00
Parik Advani12-Jun-06 3:00 
AnswerRe: Any suggestion on how to record wav / mp3 files ? Pin
Andre Figueiredo16-Aug-06 3:59
Andre Figueiredo16-Aug-06 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.