Author Topic: Lossless conversion of AAC to M4A (no re-encoding) using ffmpeg.exe  (Read 10418 times)

sveakul

  • Sr. Member
  • ****
  • Posts: 2438
I know this can be googled but after doing that and finding various combinations of switches and options and techniques, some needed some not, I thought it might help MusicBee users to have a statement here of what absolutely worked for this user.  You need to have ffmpeg.exe on your PC first.

Pure ADTS AAC audio files can be a problem to tag, especially as there seems to be no "native" tag format for pure AAC.  Some taggers add Id3v2.3 or APE tags to them, which may be read by some players and not others, and either way are definitely not "right".

This command line will LOSSLESSY convert all *.aac files within the directory it is executed to *.m4a files.  No re-encoding of the original stream(s) is done, only the mpeg4/Apple m4a wrapper applied, which then allows any modern tagging application to correctly apply mp4 format tags to the files.  Original filenames will be kept, as well as the original *.aac files:

FOR %F IN (*.aac) DO C:\ffmpeg\bin\ffmpeg.exe -i "%F" -codec copy "%~nF.m4a"

Replace the sample ffmpeg.exe path to wherever you have it on your PC.  For questioning minds (a GOOD thing!!), the funny-looking "~n" preface in the output name means use original input filename MINUS the original extension, or else you end up with a double-extension file. For those questioning if the "-codec copy" needs more switches, the official ffmpeg documentation says, "An empty stream specifier matches all streams. For example, -codec copy or -codec: copy would copy all the streams without reencoding."  Folks, this WORKS, try it!

This is for Windows.  You can right-click the directory with the .aac files while holding down "shift" and select "Open command prompt", then copy in the string above to the Command prompt window.

You can download a Windows 32 or 64 bit copy of the latest static ffmpeg.exe from this site:

http://ffmpeg.zeranoe.com/builds/

All you need is the file "ffmpeg.exe" within the downloaded package.

Note:  you can also do a lossless ADTS AAC conversion to M4A using the Mp4box.exe tool, but you can google the procedure(s) for that one!

Final words:

If you started with an M4A file to begin with and thought you must extract/convert it to AAC first to edit it (like with Mp3DirectCut), you can directly cut out a section of/trim beginning or end of an M4A file as-is by using the app LosslessCut (https://github.com/mifi/lossless-cut/releases), also based on ffmpeg.

Hope I saved somebody some time.
Last Edit: October 28, 2017, 05:15:51 AM by sveakul

Will

  • Newbie
  • *
  • Posts: 15
Just thought I would add this which is basically the reverse of what you posted.

If you ever want to very quickly extract the audio from M4A or youtube videos without any loss in quality (it's incredibly fast too), make a batch file (text file that ends in .bat) and put this code into it:

Code
@ECHO OFF
REM ---------------------------------------------------------------------------
REM Set the path for FFmpeg
SET ffmpeg=C:\Program Files\Free Download Manager\ffmpeg.exe
REM ---------------------------------------------------------------------------
ECHO Processing: "%~1"
:LOOP
REM Check first argument whether it is empty and quit loop in case
REM `%1` is the argument as is; `%~1` removes surrounding quotes
REM `"%~1"` therefore ensures that the argument is always enclosed within quotes
IF "%~1"=="" GOTO :END
REM The argument is passed over to the command to execute (`"%~1"`):
"%ffmpeg%" -i "%~1" -c:a copy -vn "%~1.m4a"
REM `SHIFT` makes the second argument (`%2`) to be the first (`%1`), the third (`%3`) to be the second (`%2`),...
SHIFT
REM Go back to top
GOTO :LOOP
:END

Make sure to change this line to match where you have ffmpeg installed (a LOT of programs use it so just do a file search) to match your location:

Code
SET ffmpeg=C:\Program Files\Free Download Manager\ffmpeg.exe

Just drag and drop one or multiple files onto this batch file and the audio will be losslessly extracted into the same folder.

If you do this a lot, you can put a shortcut to the batch file in your "Send To" folder. To find your "Send To" folder that you see in the menu when you right click files, press WIN+R, or Start/Run, or go to the address bar in explorer and type this:

Code
shell:sendto

Hope that helps someone! Yes, I wrote that nerdy batch file code.

redwing

  • Guest
If you use MB's external tools feature, you don't need to create such batch files.

Go to Preferences> Tools, and configure it as follows:

name: demux m4a
select the installed ffmpeg.exe
parameters:
Code
-i "<URL>" -vn -acodec copy "$RSplit(<URL>,.,2).m4a"
And tick wait box if you want it to run one by one.
Then you can select multiple video files in the main panel> Send to> External Tools> demux m4a

redwing

  • Guest
There was a flaw in the formula used for parameters in the above example.

$RSplit(<URL>,.,2) usually works but if path or filename contains another dot it doesn't work properly.

This should work in any cases:

Code
$RxSplit(<URL>,"\.[^.]+?$",1).m4a
or

Code
<Path>$RxSplit(<Filename>,"\.[^.]+?$",1).m4a
and you can replace <Path> with any actual folder name

e.g. C:\Converted\$RxSplit(<Filename>,"\.[^.]+?$",1).m4a
Last Edit: December 09, 2017, 12:08:17 PM by redwing

jamal

  • Newbie
  • *
  • Posts: 1
You are great - sveakul -

You gave me a very simple and practical solution to the problem I thought was impossible to solve.

Thanks a lot

sveakul

  • Sr. Member
  • ****
  • Posts: 2438