Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > General > Audio encoding

Reply
 
Thread Tools Search this Thread Display Modes
Old 11th September 2015, 05:34   #1  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,108
How to Encode Audio with x264.exe

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!

Last edited by MysteryX; 11th September 2015 at 06:51.
MysteryX is offline   Reply With Quote
Old 11th September 2015, 06:24   #2  |  Link
raffriff42
Retried Guesser
 
raffriff42's Avatar
 
Join Date: Jun 2012
Posts: 1,377
If you can mux with ffmpeg,
Code:
>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)
Code:
>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:
Code:
>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"
raffriff42 is offline   Reply With Quote
Old 11th September 2015, 06:24   #3  |  Link
Boulder
Pig on the wing
 
Boulder's Avatar
 
Join Date: Mar 2002
Location: Hollola, Finland
Posts: 4,364
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%20t...(x264,%20H.264)
__________________
And if the band you're in starts playing different tunes
I'll see you on the dark side of the Moon...
Boulder is offline   Reply With Quote
Old 11th September 2015, 06:31   #4  |  Link
raffriff42
Retried Guesser
 
raffriff42's Avatar
 
Join Date: Jun 2012
Posts: 1,377
Quote:
Originally Posted by MysteryX View Post
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
raffriff42 is offline   Reply With Quote
Old 11th September 2015, 06:48   #5  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,108
Quote:
Originally Posted by Boulder View Post
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%20t...(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.

Quote:
Originally Posted by raffriff42 View Post
Oooh rule 12!
Sorry, fixed it!

Last edited by MysteryX; 11th September 2015 at 06:51.
MysteryX is offline   Reply With Quote
Old 11th September 2015, 06:51   #6  |  Link
Boulder
Pig on the wing
 
Boulder's Avatar
 
Join Date: Mar 2002
Location: Hollola, Finland
Posts: 4,364
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.
__________________
And if the band you're in starts playing different tunes
I'll see you on the dark side of the Moon...
Boulder is offline   Reply With Quote
Old 11th September 2015, 07:05   #7  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,108
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.
MysteryX is offline   Reply With Quote
Old 11th September 2015, 08:42   #8  |  Link
fvisagie
Registered User
 
Join Date: Aug 2008
Location: Isle of Man
Posts: 586
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.
fvisagie is offline   Reply With Quote
Old 11th September 2015, 23:37   #9  |  Link
AzraelNewtype
Registered User
 
AzraelNewtype's Avatar
 
Join Date: Oct 2007
Posts: 126
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.
AzraelNewtype is offline   Reply With Quote
Old 12th September 2015, 03:03   #10  |  Link
raffriff42
Retried Guesser
 
raffriff42's Avatar
 
Join Date: Jun 2012
Posts: 1,377
I think you are right. My memory circuits are faulty.
http://wiki.hydrogenaud.io/index.php?title=AAC_encoders
raffriff42 is offline   Reply With Quote
Old 12th September 2015, 15:56   #11  |  Link
fvisagie
Registered User
 
Join Date: Aug 2008
Location: Isle of Man
Posts: 586
Quote:
Originally Posted by MysteryX View Post
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.
fvisagie is offline   Reply With Quote
Old 16th September 2015, 16:48   #12  |  Link
MysteryX
Soul Architect
 
MysteryX's Avatar
 
Join Date: Apr 2014
Posts: 2,108
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?
MysteryX is offline   Reply With Quote
Old 17th September 2015, 13:55   #13  |  Link
colours
Registered User
 
colours's Avatar
 
Join Date: Mar 2014
Posts: 312
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.

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?
__________________
Say no to AviSynth 2.5.8 and DirectShowSource!

Last edited by colours; 17th September 2015 at 13:59.
colours is offline   Reply With Quote
Old 18th September 2015, 21:28   #14  |  Link
Brazil2
Registered User
 
Join Date: Jul 2008
Posts: 479
Quote:
Originally Posted by MysteryX View Post
Nero was best when it came out, but now Fraunhofer is better?
Only for bitrates < 128 kbps

http://forum.doom9.org/showthread.ph...02#post1605602
Brazil2 is offline   Reply With Quote
Old 19th September 2015, 08:52   #15  |  Link
filler56789
SuperVirus
 
filler56789's Avatar
 
Join Date: Jun 2012
Location: Antarctic Japan
Posts: 795
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...

Last edited by filler56789; 19th September 2015 at 08:54. Reason: clarity
filler56789 is offline   Reply With Quote
Old 19th September 2015, 09:50   #16  |  Link
tebasuna51
Moderator
 
tebasuna51's Avatar
 
Join Date: Feb 2005
Location: Spain
Posts: 5,353
Quote:
Originally Posted by filler56789 View Post
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.
__________________
BeHappy, AviSynth audio transcoder, in Doom9 forums. NicAudio, BassAudio, audio decoders.
tebasuna51 is offline   Reply With Quote
Old 19th September 2015, 17:59   #17  |  Link
manolito
Registered User
 
manolito's Avatar
 
Join Date: Sep 2003
Posts: 2,163
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:
Quote:
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

Last edited by manolito; 19th September 2015 at 18:57.
manolito is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:16.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2018, vBulletin Solutions Inc.