Jump to content

Advice on creating a FLAC to MP3 program


Recommended Posts

Hello, everyone

I recently came across a FLAC compilation where I want to make mp3's of everything to save space. This gave me an idea to write a script for something I haven't seen before. What I want to make is a script where you drag & drop a folder (or a bunch of FLAC files) and specify to the program an output folder. The script will read your desires (bitrate, speed vs quality, sampling rate, channel mode) and - this is the best part - in your destination directory, create the output folders for you. By this, I mean, you specify an output folder and from there, it will use the metadata from the FLAC file to create folders for the MP3s.

In the past, when I convert FLAC to MP3, I use a program where I must create the output folder on my own. Then I must go specify those folders individually. However, if you are like me, you probably have a folder for your MP3s. I want to make a program where I drag & drop a folder with lots of FLAC files and based on their origin folder (or preferably their tag data), create the folders in the main output folder you specified. I want to detect if the folder dragged into the script is a "multi-disc" folder - a subset folder for a multi-disc set (disc 01, d01, CD01, etc) - or maybe if something like D01 comes up twice, make a D01_2. If that happens, go back to the folder above it and create it also.

Basically, this folder creation thing and ease of use  would be a huge part of the allure of such a program.

I found out a few things:

1. You cannot convert directly from FLAC to LAME MP3.2. You can include lame.exe in your project if you include a link to their website and credit them for everything.

Things I got right:

1. I was able to convert FLAC to WAV
2. I was able to get metadata from the FLAC file.
3. I have an awesome GUI where I can get all the options (bitrate, type of encode, etc). 

I am just curious if anyone has attempted to make such a FLAC to MP3 program and what I should expect as obstacles? Is this worth pursuing?

Here's the code I have so far:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Files\Trayse101-Basic-Filetypes-2-Mp3.ico
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <SliderConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$filData = @ScriptDir & "\Data.ini"
;GUISetOnEvent (@GUI_DragFile, "_FilesFoldersDropped")

$guiForm1 = GUICreate("AutoIt MP3 Encoder", 973, 626, -1, -1, -1, BitOR($WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE))
$guiLabel1 = GUICtrlCreateLabel("Source Directory", 16, 16, 83, 17)
$guiInput1 = GUICtrlCreateInput("", 104, 16, 761, 21)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$guiButton1 = GUICtrlCreateButton("Select", 880, 16, 75, 25, $WS_GROUP)
$guiList1 = GUICtrlCreateList("", 16, 56, 937, 396)
$guiGroup1 = GUICtrlCreateGroup("Settings", 16, 519, 849, 97)
$guiLabel5 = GUICtrlCreateLabel("Bitrate (kbps)", 55, 535, 74, 17)
GUICtrlSetTip(-1, "16-320 kbps - The higher number, the better sound quality")
$guiCombo1 = GUICtrlCreateCombo("", 32, 559, 115, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
$guiRadio1 = GUICtrlCreateRadio("CBR", 32, 591, 49, 17)
GUICtrlSetTip(-1, "Constant Bitrate (Better compatibility, minor quality loss - Recommended)")
GUICtrlSetState($guiRadio1, $GUI_CHECKED)
$guiRadio2 = GUICtrlCreateRadio("VBR", 110, 591, 49, 17)
GUICtrlSetTip(-1, "Variable Bitrate (Minor quality enhancement, but less compatibility")
;$guiRadio3 = GUICtrlCreateRadio("ABR", 110, 591, 49, 17)
;GUICtrlSetTip(-1, "Average Bitrate (Similar to VBR, but ")
$guiSlider1 = GUICtrlCreateSlider(288, 559, 102, 45)
GUICtrlSetLimit(-1, 9, 0)
GUICtrlSetTip(-1, "Computers are super fast these days. Go for highest quality!")
$guiLabel3 = GUICtrlCreateLabel("Faster Encode", 216, 575, 73, 17)
$guiLabel4 = GUICtrlCreateLabel("Highest Quality", 400, 575, 80, 17)
$guiLabel6 = GUICtrlCreateLabel("Speed Vs Quality", 296, 535, 85, 17)
GUICtrlSetTip(-1, "Computers are super fast these days. Go for highest quality!")
$guiLabel7 = GUICtrlCreateLabel("Sampling Rate", 544, 535, 73, 17)
GUICtrlSetTip(-1, "The default sampling rate of CDs is 44100. You should use this value if you are unsure what to choose.")
$guiCombo2 = GUICtrlCreateCombo("", 520, 567, 113, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
$guiLabel8 = GUICtrlCreateLabel("Channel Mode", 720, 535, 73, 17)
$guiCombo3 = GUICtrlCreateCombo("", 688, 567, 145, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
GUICtrlCreateGroup("", -99, -99, 1, 1)
$guiLabel2 = GUICtrlCreateLabel("Output Directory:", 16, 496, 84, 17)
$guiInput2 = GUICtrlCreateInput("", 104, 496, 761, 21)
$guiButton2 = GUICtrlCreateButton("Select", 880, 496, 75, 25, $WS_GROUP)
$guiButton3 = GUICtrlCreateButton("Encode!", 880, 544, 75, 57, $WS_GROUP)
$guiButton4 = GUICtrlCreateButton("Select All", 16, 456, 91, 25, $WS_GROUP)
$guiButton5 = GUICtrlCreateButton("De-Select All", 120, 456, 91, 25, $WS_GROUP)
$guiButton6 = GUICtrlCreateButton("Remove All From List", 248, 456, 131, 25, $WS_GROUP)
$guiButton7 = GUICtrlCreateButton("Remove Unselected Items From List", 392, 456, 195, 25, $WS_GROUP)
$guiLabel9 = GUICtrlCreateLabel("LAME Version:", 616, 464, 87, 17)
GUICtrlSetTip(-1, "LAME is a freeware MP3 encoder. Click to learn more.")
GUICtrlSetCursor(-1, 0)
$guiLabel10 = GUICtrlCreateLabel("LAME Not Found!", 704, 464, 112, 17)
GUICtrlSetTip(-1, "LAME is a freeware MP3 encoder. Click to learn more.")
GUICtrlSetCursor(-1, 0)
$guiButton8 = GUICtrlCreateButton("Search For LAME EXE", 824, 456, 131, 25, $WS_GROUP)
GUICtrlSetState($guiButton8, $GUI_HIDE)
GUISetState(@SW_SHOW)


_SetOptions()
_InstallLAME()
_GetLAMEVersion()


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            $varReadCBRVBR = GUICtrlRead($guiRadio2)
            If $varReadCBRVBR = $GUI_CHECKED Then
                IniWrite($filData, "Settings", "CBRVBR", "VBR")
            Else
                IniWrite($filData, "Settings", "CBRVBR", "CBR")
            EndIf
            Exit
        Case $guiLabel9
            _LAMEInfo()
        Case $guiLabel10
            _LAMEInfo()
        Case $guiButton3
            $varQualityResult = GUICtrlRead($guiSlider1) ; Ouput set from 0-9 where 0 is best quality and 9 fastest encode switch: -q9 or -q3
            $guiButton3 = 9 - $varQualityResult
        Case $guiRadio1
            $varBitrate = IniRead($filData, "Settings", "CBRBitrate", "320")
            _SetBitrates("CBR", $varBitrate)
        Case $guiRadio2
            $varBitrate = IniRead($filData, "Settings", "VBRBitrate", "~240")
            _SetBitrates("VBR", $varBitrate)
        Case $GUI_EVENT_DROPPED
            _FilesFoldersDropped()
    EndSwitch
WEnd

Func _SetOptions()
    $varCBRVBR = IniRead($filData, "Settings", "CBRVBR", "CBR")
    If $varCBRVBR = "CBR" Then
        $varBitrate = IniRead($filData, "Settings", "CBRBitrate", "320")
    Else
        $varBitrate = IniRead($filData, "Settings", "VBRBitrate", "~240")
    Endif
    $varQuality = IniRead($filData, "Settings", "SvQ", "100")
    $varSampleRate = IniRead($filData, "Settings", "SampleRate", "44100")
    $varChannelMode = IniRead($filData, "Settings", "ChannelMode", "Stereo") ; Stereo, Joint Stereo, Mono
    $varSourceDir = IniRead($filData, "Directories", "Source", @ScriptDir)
    $varDestinationDir = IniRead($filData, "Directories", "Output", @ScriptDir & "\MP3\")
    $varCBRVBRStatus = IniRead($filData, "Settings", "CBRVBR", "CBR")
    GUICtrlSetData($guiCombo2, "8000|11025|12000|16000|22050|24000|32000|44100|48000", $varSampleRate)
    GUICtrlSetData($guiCombo3, "Stereo|Joint Stereo|Mono", $varChannelMode)
    GUICtrlSetData($guiSlider1, $varQuality)
    If $varCBRVBRStatus = "VBR" Then
        GUICtrlSetState($guiRadio2, $GUI_CHECKED)
    EndIf
    _SetBitrates($varCBRVBRStatus, $varBitrate)
EndFunc

Func _SetBitrates($varBRType, $varBitrate)
    GUICtrlSetData($guiCombo1, "")
    If $varBRType = "CBR" Then
        GUICtrlSetData($guiCombo1, "16|24|32|48|64|96|112|128|160|192|224|256|320", $varBitrate)
    Else
        GUICtrlSetData($guiCombo1, "~240|~220|~190|~170|~160|~130|~120", $varBitrate)
    EndIf
EndFunc

Func _GetLAMEVersion()
    ; idea: maybe add functionality to scan computer for lame.exe
    $varLAMELoc = IniRead($filData, "LAME", "Location", "LAME Not Found!")
    If $varLAMELoc <> "LAME Not Found!" And FileExists($varLAMELoc) Then ; INI shows the location AND it is actually at that location.
        $varLAMEVer = FileGetVersion($varLAMELoc)
    ElseIf FileExists(@ScriptDir & "\lame.exe") Then ; Check to see if it is in the script's directory
        IniWrite($filData, "LAME", "Location", @ScriptDir & "\lame.exe")
        $varLAMEVer = FileGetVersion(@ScriptDir & "\lame.exe")
    Else ; LAME not found
        $varLAMEVer = "LAME Not Found!"
    EndIf

    GUICtrlSetData($guiLabel10, $varLAMEVer)
    If $varLAMEVer = "LAME Not Found!" Then
        GUICtrlSetFont($guiLabel9, 8, 800, 0, "MS Sans Serif") ; Red font
        GUICtrlSetColor($guiLabel9, 0xFF0000)
        GUICtrlSetFont($guiLabel10, 8, 800, 0, "MS Sans Serif")
        GUICtrlSetColor($guiLabel10, 0xFF0000)
        GUICtrlSetState($guiButton8, $GUI_SHOW) ; Enable the LAME search button
    Else
        GUICtrlSetFont($guiLabel9, 8, 800, 0, "MS Sans Serif") ; Green font
        GUICtrlSetColor($guiLabel9, 0x008000)
        GUICtrlSetFont($guiLabel10, 8, 800, 0, "MS Sans Serif")
        GUICtrlSetColor($guiLabel10, 0x008000)
    EndIf
EndFunc   ;==>_GetLAMEVersion

Func _InstallLAME()
    If @Compiled = 1 Then
        If Not FileExists($filData) And Not FileExists(@ScriptDir & "\lame.exe") Then
            $varLAMEVer = FileGetVersion(@ScriptDir & "\Files\lame.exe")
            $varQuery = MsgBox(4, "First Time Run - Install LAME", "Do you want to install the included version of LAME v." & $varLAMEVer)
            If $varQuery = 6 Then ; because of limitations of FileInstall, you need to modify the script to your working directory with a hard-coded link to the lame.exe file and destination.
                FileInstall("Y:\DL\AutoIt Projects\AutoIt MP3 Encoder\Files\lame.exe", "Y:\DL\AutoIt Projects\AutoIt MP3 Encoder\lame.exe")
            EndIf
        EndIf
    EndIf

EndFunc   ;==>_InstallLAME


Func _DownloadLAME()
    ; If user wants to download their own version of LAME, give several links. Also, check if system is 32 or 64 bit and advise to download correct version.

EndFunc   ;==>_DownloadLAME

Func _LAMEInfo()
    ShellExecute("https://lame.sourceforge.io/")
EndFunc

Func _FilesFoldersDropped()
    MsgBox(0, "", "files/folder dropped")
EndFunc

 

Edited by abberration
Link to comment
Share on other sites

If I'm not mistaken, you can convert FLAC to WAV and then convert the WAV to MP3 with Lame.

There are several versions of Lame -- can't remember how many. Seems like 4 or 5. You just have to find the right version.

Or, you can find some other program to convert FLAC to WAV and use Lame to convert WAV to MP3.

 

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

5 hours ago, abberration said:

 Is this worth pursuing?

For me, no, because im going the opposite way, getting rid of mp3's in favor of higher quality FLAC's.

So it depends on your needs and desires, how many times do you expect to make that conversion?

There are all sorts of questions that only you can answer.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

If I remember correctly, FLAC has a 2 to 1 compression ratio, while a 320kbps MP3 has a 5 to 1 ratio.

To me, I can't tell the difference in sound quality between them.

I'm not knocking FLAC, but I'd rather have the original WAV if all I'm getting is 2 to 1 ratio.

Compression is all about file size -- and I'm happy with an MP3 if I can't tell the difference between a WAV and an MP3.

My USB sticks will be happy too.

 

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

abberration,

I've given it some thought as to "Is this worth pursuing?".

Personally, I don't think so.

Reason -- the web is saturated with rippers, encoders, decoders and audio editors.

But, on the other hand, it would sharpen your coding skills to complete a project like that.

Just my opinion.

 

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

To build your own custom converter you might use FFmpeg which allows to directly convert from FLAC to MP3, to get tags etc
Easy to manage using a gui and command line instructions  :)

Edit
I globally agree with ripdad and thought so too ... until I had to convert mkv files in a way that NO free tool on the web was precisely able to go

Edited by mikell
Link to comment
Share on other sites

4 hours ago, ripdad said:

If I remember correctly, FLAC has a 2 to 1 compression ratio, while a 320kbps MP3 has a 5 to 1 ratio.

To me, I can't tell the difference in sound quality between them.

FLAC is a compressed lossless audio stream, WAV is uncompressed lossless audio stream.

This means that, while FLAC is compressible in file size, the quality is identical to original.

MP3 compresses but loses file size and quality.

Depending on the system you listen to the files, the audio quality can be little, or very noticeable.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

careca,

Yes true, but to human hearing an mp3 encoded at 320kbps, sounds great to me.

Shark007,

I don't use multi-channel, but very good point.

 

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Hi,

Thanks to everyone for their input. I have made some progress and getting things working. I think this program would help me a great deal since I do prefer FLAC for storage of files, but I use MP3 every day (and my car stereo and MP3 player do not play FLAC).

@mikell I did not know FFmpeg transcoded from FLAC to MP3 nor that it did it without an intermediate .wav file. Pursuit of that kind of information is why I started this thread. I was looking for advice just like that. I will investigate FFmpeg tomorrow. Thanks for the info.

When I finish the project, I will post it in the Example Scripts section. Maybe it will be useful to others. Like I said, I think the automatic folder creation and ease of use will make it a great tool to have.

Edit:

Ok, I just got finished with playing with FFmpeg and I am super impressed. It does convert directly from FLAC to mp3 using the LAME codec (and probably other formats, so I will experiment with that and expand it to more than just FLAC tracks). The other day, I was able to extract metadata using metaflac and import it (and artwork) through lame.exe. However, I was super excited to find out that FFmpeg just uses the command "-id3v2_version 3" to automatically transfers the FLAC metadata and artwork to the mp3. That is so awesome. That will save me a lot of headaches. Lastly, FFmpeg was so fast, I hardly noticed a difference between the minimum and maximum "speed vs. quality" levels. I had to run Timerinit and Timerdiff to see it was actually working. There is a time difference of about 13% on my computer for min/max. Wow, I am very happy @mikell suggested FFmpeg. Awesome and thanks! I think I can make this happen, so my project moves forward again.

:thumbsup:

 

Edited by abberration
Update on progress
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...