View Full Version : How to Encode Audio with x264.exe
MysteryX
11th September 2015, 05:34
I'm making improvements to the media encoder I'm working on. Currently, the only option I'm giving is to copy the original audio after processing the video. In some cases, however, it would be good to re-encode the audio, such as when we want to trim the video, or when we have uncompressed low-quality audio from a camera.
To copy the audio, I encode with x264.exe and then muxe back the original audio with FFMPEG.
If I instead want to encode the audio as AAC, what's the best way of doing it? AFAIK, x264 official builds don't include the audio encoder and can only output a raw H264 file that then needs to be muxed into a container.
Only options I can think of are
1. Having a custom build that supports audio encoding
2. Rendering the video and audio separately and then muxing together; for this pass, I would have to discard video functions and only keep functions that affect the audio, speed or duration.
What do experts have to say on the topic?
I did custom-build FFMPEG to include audio encoder, but AFAIK x264's encoder is superior to FFMPEG.
Also, in some cases it could be useful to encode the video in a lossless format, what's *the* way of doing so?
Thanks!
raffriff42
11th September 2015, 06:24
If you can mux with ffmpeg, >ffmpeg.exe -y -i "video.264" -i "audio.wav" -fps 30 -c:v copy -c:a copy -f mp4 "output.mp4"
You can encode the audio at the same time:
(may not work, depending on ffmpeg version and included libraries; check your console output for clues)>ffmpeg.exe -y -i "video.264" -i "audio.wav" -fps 30 -c:v copy -c:a aac -q:a 0.35 -f mp4 "output.mp4"
However, NeroAAC is regarded as a better option for AAC:>neroAacEnc.exe -q 0.35 -ignorelength -if "audio.wav" -of "audio.aac"
>ffmpeg.exe -y -i "video.264" -i "audio.aac" -fps 30 -c:v copy -c:a copy -f mp4 "output.mp4"
Boulder
11th September 2015, 06:24
First compile libx264 using the latest x264 git and then compile ffmpeg with libx264, and you'll have the best of both worlds :)
https://trac.ffmpeg.org/wiki/How%20to%20quickly%20compile%20FFmpeg%20with%20libx264%20(x264,%20H.264)
raffriff42
11th September 2015, 06:31
Also, in some cases it could be useful to encode the video in a lossless format, what's the best way of doing so?Oooh rule 12! Anyway, in place of
-c:v copy
above, try
-c:v utvideo -colorspace bt709
MysteryX
11th September 2015, 06:48
First compile libx264 using the latest x264 git and then compile ffmpeg with libx264, and you'll have the best of both worlds :)
https://trac.ffmpeg.org/wiki/How%20to%20quickly%20compile%20FFmpeg%20with%20libx264%20(x264,%20H.264)
Page not found.
If I'm using FFMPEG, do I need to use x264 separately, or can I simply use it from within FFMPEG? I read somewhere that to encode x264 videos with FFMPEG with decent quality, you had to tweak settings quite a bit while x264.exe was working great right out of the box.
While searching on the topic, I also found someone mentioning that encoding both separately and then muxing could cause synchronization issues; not sure if that's an issue.
While also searching about lossless encoding, I also saw a comment that UT Video was fast to process but produced a large file, and that there were better options for lossless to produce smaller files.
Oooh rule 12!
Sorry, fixed it!
Boulder
11th September 2015, 06:51
The forum software snipped the last ')' out of the URL so you'll apparently need to add that :) You can use x264 inside ffmpeg if you follow the instructions. The quality will be the same because the code is the same.
MysteryX
11th September 2015, 07:05
I just read that it is illegal to publicly distribute a software that includes FFMPEG with libfdk_aac, so I shouldn't use that option. (correct?)
Nero AAC Codec looks interesting.
Will I run into any potential issues if I encode the audio and video separately and then muxe? Even if I trim the video and change the speed?
If I get this correctly, I would have to
1. Encode to x264 via FFMPEG and output the audio as PCM
2. Convert audio from PCM to AAC with Nero AAC Codec
3. Muxe the video with the AAC audio
That would work? Then if I want lossless audio, I stick to step 1.
fvisagie
11th September 2015, 08:42
ffmpeg also supports the libvo_aacenc codec which I use constantly. Some people on the ffmpeg mailing list regard it as inferior to libfdk_aac, so I guess a comparison in your case might be in order.
AzraelNewtype
11th September 2015, 23:37
Nero hasn't been the suggested encoder for years now. Get QAAC if you're looking to grab an out-of-ffmpeg encoder at all.
raffriff42
12th September 2015, 03:03
I think you are right. My memory circuits are faulty.
http://wiki.hydrogenaud.io/index.php?title=AAC_encoders
fvisagie
12th September 2015, 15:56
I read somewhere that to encode x264 videos with FFMPEG with decent quality, you had to tweak settings quite a bit while x264.exe was working great right out of the box.
Whether that was previously true I do not know, but it certainly has not been recently. Use the well-designed presets (ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo) in both cases. Here's a handy ffmpeg H.264 encoding guide: https://trac.ffmpeg.org/wiki/Encode/H.264.
MysteryX
16th September 2015, 16:48
Thanks for all the useful information!
About Fraunhofer AAC Encoders, could I run into legal issues if I distribute a custom-built version of FFMPEG that includes it, as part of the setup package of an open-source software?
As for Fraunhofer AAC vs Nero... it seems they're both good. If I get this properly, Nero was best when it came out, but now Fraunhofer is better?
colours
17th September 2015, 13:55
If you care about GPL compatibility (so you can have an ffmpeg build with --enable-gpl), the best option (rule 12 be damned) is to use libavcodec's AAC encoder. I hear it's much better now than it was years back, but it's not like you have much of a choice anyway because libvo_aacenc is literally the only other free AAC encoder and it allegedly sucks (https://trac.ffmpeg.org/wiki/Encode/HighQualityAudio).
According to that same page, if you absolutely must have an audio format compatible with MP4 and a GPL-compatible encoder for it, using LAME to encode to MP3 is an even better option. (Yeah, yeah, MP3 is an old format and AAC is supposed to be better, but people haven't invested the same amount of time into improving the free AAC encoders.)
Also, why is this in the AviSynth usage forum?
Brazil2
18th September 2015, 21:28
Nero was best when it came out, but now Fraunhofer is better?
Only for bitrates < 128 kbps
http://forum.doom9.org/showthread.php?p=1605602#post1605602
filler56789
19th September 2015, 08:52
Regarding NeroAACenc...
keep in mind that it has a BIG encoder delay, so
it probably is not the most suitable choice for movies's or videos's audio tracks...
tebasuna51
19th September 2015, 09:50
Regarding NeroAACenc...
keep in mind that it has a BIG encoder delay, so
it probably is not the most suitable choice for movies's or videos's audio tracks...
But this delay is stored in m4a metadata and can be compensated by the muxer like mkvmerge do. Also NeroAacDec can recover the original audio with 0 delay.
The recommended encoder for low bitrates is Qaac (Quicktime) than have also the --no-delay parameter, but for bitrates over 64 kbps/channel you can use NeroAacEnc without audible differences.
You can use NeroAacEnc with -q 0.4 (or greater) safely.
manolito
19th September 2015, 17:59
Totally agree with tebasuna...
If you prefer to use FFMpeg for AAC encoding, have a look here:
https://trac.ffmpeg.org/wiki/Encode/AAC
From their FAQ:
Which encoder should I use? What provides the best quality?
For AAC-LC the likely answer is: libfdk_aac > libfaac > Native FFmpeg AAC encoder (aac) > libvo_aacenc.
The libfdk_aac encoder is free, but it does not conform to the GPL, so the FFMpeg developers consider it illegal to distribute FFMpeg versions where libfdk_aac is included. But you can get FFMpeg binaries without libfdk_aac in it which enable its use if the separate Fraunhofer DLL resides in the same folder.
You can get such FFMpeg binaries and also the libfdk_aac DLL at Sherpya's MPlayer site:
http://oss.netfarm.it/mplayer/
Cheers
manolito
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.