Compile and use ffmpeg with Fraunhofer codec for youtube

Post fully complete "how to" guides and tutorials here. This is a great place to get feedback on stuff you might put in the wiki.

Moderators: MattKingUSA, khz

Post Reply
Bas King
Established Member
Posts: 3
Joined: Fri Sep 06, 2013 10:49 am

Compile and use ffmpeg with Fraunhofer codec for youtube

Post by Bas King »

Best encoderformats (both audio and video) for youtube?
https://support.google.com/youtube/answer/1722171?hl=en
Recommended bitrates, codecs, and resolutions, and more
Container: .mp4

Audio Codec: AAC-LC
Channels: Stereo or Stereo + 5.1
Sample rate 96khz or 48 khz

Video Codec: H.264
Progressive scan (no interlacing)
High Profile
2 consecutive B frames
Closed GOP. GOP of half the frame rate.
CABAC
Variable bitrate. No bitrate limit required
Color Space: 4.2.0

Frame rates
Content at 1080i 60, should be deinterlaced, going from 60 interlaced fields per second to 30 progressive frames per second before uploading.

Best AAC-LC encoder: http://trac.ffmpeg.org/wiki/AACEncodingGuide
libfdk_aac
Fraunhofer FDK AAC codec library. This is currently the highest-quality AAC encoder available with ffmpeg. Requires ffmpeg to be configured with --enable-libfdk_aac (and additionally--enable-nonfree if you're also using --enable-gpl). But beware, it defaults to a low-pass filter of around 14kHz. If you want to preserve higher frequencies, use -cutoff 18000. Adjust the number to the upper frequency limit you prefer.
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.
libfdk_aac has been designated as "non-free", and you cannot download a pre-built ffmpeg that supports it.
This can be resolved by compiling ffmpeg in a terminal like this:

Update repositories:

Code: Select all

sudo apt-get update
Create sourcefilefolder:

Code: Select all

mkdir ~/sourcefiles
Remove old precompiled version:

Code: Select all

sudo apt-get remove ffmpeg
Install some developer versions (source code) and compiler tools:

Code: Select all

sudo apt-get -y install autoconf automake build-essential git libass-dev libgpac-dev libsdl1.2-dev libtheora-dev libtool libva-dev libvdpau-dev libvorbis-dev libx11-dev libxext-dev libxfixes-dev pkg-config texi2html zlib1g-dev
Note: Check http://trac.ffmpeg.org/wiki/UbuntuCompilationGuide on how to install from source if any of these 3 below are not in your repositories:
1 YASM:

Code: Select all

sudo apt-get install yasm
2 libmp3lame, MP3:

Code: Select all

sudo apt-get install libmp3lame-dev
3 libvpx, VP8/VP9 video:

Code: Select all

sudo apt-get install libvpx-dev
H.264 video encoder:

Code: Select all

cd ~/sourcefiles
git clone --depth 1 git://git.videolan.org/x264.git
cd x264
./configure --prefix="$HOME/ffmpeg_build" --bindir="/usr/local/bin" --enable-static
#./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static
make
sudo make install
make distclean
Libopus:

Code: Select all

cd ~/sourcefiles
wget http://downloads.xiph.org/releases/opus/opus-1.1.tar.gz
tar xzvf opus-1.1.tar.gz
cd opus-1.1
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
sudo make install
make distclean
FDK-AAC, Fraunhofer AAC audio encoder:

Code: Select all

cd ~/sourcefiles
git clone --depth 1 git://git.code.sf.net/p/opencore-amr/fdk-aac
cd fdk-aac
autoreconf -fiv
./configure --prefix="$HOME/ffmpeg_build" --disable-shared
make
sudo make install
make distclean
FFMPEG

Code: Select all

cd ~/sourcefiles
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
export PKG_CONFIG_PATH
./configure --prefix="$HOME/ffmpeg_build" --extra-cflags="-I$HOME/ffmpeg_build/include" --extra-ldflags="-L$HOME/ffmpeg_build/lib" --bindir="/usr/local/bin" --extra-libs="-ldl" --enable-gpl --enable-libass --enable-libfdk-aac --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-x11grab
make
sudo make install
make distclean
Reload terminal
To see current version and compiled libraries:

Code: Select all

ffmpeg
All Codecs available:

Code: Select all

Ffmpeg -codecs
Fraunhofer libfdk_aac? H.264 libx264? Check:

Code: Select all

ffmpeg -codecs | grep fdk
ffmpeg -codecs | grep 264
So, you now can combine a (upto) 96khz audio.wav file with say: a picture.
If you have some picture.jpg (or any format ffmpeg understands) you can simply convert it to a png with the maximum youtube size like this:

Code: Select all

ffmpeg -y -i picture.jpg -s 1920x1080 picture.png 
-y autoanswers Yes, overwrite file when asked
-i picture.jpg is the Input, picture, with format .jpg, but could be any format like .gif, .bmp, .png etc
-s 1920x1080 output size
picture.png is the output, written to a .png file in this case.
Now you can create a video.mp4 file, that you can upload to youtube, like this:

Code: Select all

ffmpeg -y -loop 1 -i picture.png -i audio.wav -c:a libfdk_aac -cutoff 20000 -vf fps=30 -pix_fmt yuv420p -aspect 16:9 -c:v libx264 -shortest video.mp4
-y: overwrite? Yes.
-loop 1: loop = true for next input (the picture)
-i picture.png: input picture.png
-i audio.wav: input audio.wav
-c:a libfdk_aac: Codec for Audio uses Fraunhofer codec
-cutoff 20000: Fraunhofer cutoff value valid range is 173 - 20000, defaults to 14000 when not set
-vf fps=30: video format: frames per second to 30
-pix_fmt yuv420p: most widely recognized pixel color format
-aspect 16:9 :Set correct aspect ratio (default 4:3 gives black bands on sides)
-c:v libx264: Codec for Video uses libx264 (youtube best quality)
-shortest: stop the video when the first stream runs out (which is the audio, we loop the picture endlessly)
video.mp4: write output to video.mp4
Loop over a bunch of pictures:
Convert all pictures you want to use to fragments of video. -t 5.00 is the number of seconds. For example like this:

Code: Select all

ffmpeg -y -loop 1 -i picture000.png -s 1920x1080 -vf fps=30 -pix_fmt yuv420p -aspect 16:9 -c:v libx264 -t 1.00 picture000.mp4
ffmpeg -y -loop 1 -i picture001.jpg -s 1920x1080 -vf fps=30 -pix_fmt yuv420p -aspect 16:9 -c:v libx264 -t 15.00 picture001.mp4
ffmpeg -y -loop 1 -i picture002.gif -s 1920x1080 -vf fps=30 -pix_fmt yuv420p -aspect 16:9 -c:v libx264 -t 25.00 picture002.mp4
ffmpeg -y -loop 1 -i picture003.bmp -s 1920x1080 -vf fps=30 -pix_fmt yuv420p -aspect 16:9 -c:v libx264 -t 5.00 picture003.mp4
Save to mylist.txt enough videotime to fill the audiotime with:

Code: Select all

file 'picture000.mp4'
file 'picture001.mp4'
file 'picture002.mp4'
file 'picture001.mp4'
file 'picture002.mp4'
file 'picture003.mp4'
Combine them into one single video:

Code: Select all

ffmpeg -y -f concat -i mylist.txt -vf fps=30 -pix_fmt yuv420p -aspect 16:9 -c:v libx264 pictures.mp4
Now take that video, and include audio.wav to write video.mp4:

Code: Select all

ffmpeg -y -i pictures.mp4 -i audio.wav -c:a libfdk_aac -cutoff 20000 -vf fps=30 -pix_fmt yuv420p -aspect 16:9 -c:v libx264 -shortest video.mp4
Play it with vlc (or something) to check sound and picture are a good match, and upload it to youtube.

You can also download videos from youtube.

Code: Select all

sudo apt-get install youtube-dl 
Search for a nice video you want to grab a sample from, and select & copy the url, like for example:

Code: Select all

http://www.youtube.com/watch?v=jbQlsV0sB5Y 
Then, inside a terminal, you can type:
yout(tab) (ctrl-shift-insert) (enter):
Which will work out as something like:

Code: Select all

youtube-dl http://www.youtube.com/watch?v=jbQlsV0sB5Y

It will download the video from youtube to your /home/YOURNAME/ folder. (Note: Some videos will fail to download due to copyright restrictions I guess, leading to failure by 'secret keys missing for the file')

You can inspect information about the video file like duration, screensize, codec used etc:

Code: Select all

ffmpeg -i video.mp4 
To extract audio from single mp4/flv videos and convert to mp3/aac use something like this:

Code: Select all

ffmpeg -y -i video.flv -c:a libfdk_aac -cutoff 20000 audio.aac
ffmpeg -y -i video.flv -c:a libmp3lame audio.mp3
ffmpeg -y -i video.mp4 -c:a libfdk_aac -cutoff 20000 audio.aac
ffmpeg -y -i video.mp4 -c:a libmp3lame audio.mp3
Small script to extract audio from all videos at once:
save to /home/YOURNAME/extractaudio.py:

Code: Select all

from subprocess import call 
from os import listdir 
from os.path import isfile, join 
from sys import argv 
#cutoff any frequencies above (cutoff valid range is 173 - 20000, default=14000) in aac: 
aaccutoff="20000" 
#you should set mypath to YOUR home folder 
mypath="/home/bas" 

def convert(shortnamein, codec): 
  listfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ] 
  listfiles2 = sorted(listfiles) 
  for f1 in listfiles2: 
    if f1[-4:].lower()==".mp4" or f1[-4:].lower()==".flv": 
      shortname=f1[:-4]+"."+shortnamein 
      if shortnamein=='aac':      
        call(["sudo", "ffmpeg", "-y", "-i", f1, "-c:a", codec, "-cutoff", aaccutoff, shortname]) 
      elif shortnamein=='mp3': 
        call(["sudo", "ffmpeg", "-y", "-i", f1, "-c:a", codec, shortname]) 

if len(argv)==2 and argv[1].lower()=='mp3': 
  convert("mp3", "libmp3lame") 
elif len(argv)==2 and argv[1].lower()=='aac': 
  convert("aac", "libfdk_aac") 
else: 
  print 'Extracts audio from mp4/flv to lame-mp3 or Fraunhofer-aac' 
  print 
  print 'For MP3 use:' 
  print 'python extractaudio.py mp3' 
  print 'Note: perhaps you need mp3 lame encoder first, to install do' 
  print 'sudo apt-get install lame libavcodec-extra-53' 
  print 
  print 'for AAC use:' 
  print 'python extractaudio.py aac' 
  print 'Note: perhaps you need to compile ffmpeg with Fraunhofer' 
  print 'aac-library (aka libfdk_aac) support first'
To run:

Code: Select all

python extractaudio.py 
Screencast
Grab test video (size 1920x1080) from x11 (the screen)

Code: Select all

ffmpeg -f x11grab -r 30 -s 1920x1080 -i :0.0 /tmp/x11grab.mp4 
(press q to stop)

record audio:
For pulseaudio (on top of alsa):
Open pavucontrol, and include 'monitor'
Input devices: all input devices

For JACK: in patchage or qjcackctl->connections, connect all sound going to 'system' (like PulseAudio JACK Sink and/or any jack applications you have in front of system) to 'PulseAudio JACK Source'.

Record pure wavefiles from speakers:

Code: Select all

ffmpeg -f alsa -ac 2 -i pulse /tmp/rawsound.wav
Record sound from speakers compressed with libmp3lame encoder:

Code: Select all

ffmpeg -f alsa -ac 2 -i pulse -c:a libmp3lame -ar 48000 /tmp/mp3sound.mp3
Or record audio compressed with Fraunhofer libfdk_aac encoder:

Code: Select all

ffmpeg -f alsa -ac 2 -i pulse -c:a libfdk_aac /tmp/aacsound.aac
So, to grab video and audio 'as you hear it coming from the speakers' all at once, and compress audio on the fly with Fraunhofer encoder:

Code: Select all

ffmpeg -f alsa -ac 2 -i pulse -c:a libfdk_aac -f x11grab -r 30 -s 1920x1080 -i :0.0 /tmp/videowithaacsound.mp4 
But that can be too much for your machine, so perhaps its better to do in steps: First audio and video are streamed live 'raw' to disk, then edited, then tightly compressed into a mp4.

Code: Select all

ffmpeg -y -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 1920x1080 -i :0.0 /tmp/screencast.mkv
A mkv can hold raw audio and video (does not apply any codec).
Now we can edit the mkv, cut out failures, add noise filter, text or effects etc. When we are happy with the result, compress it for upload to youtube:

Code: Select all

ffmpeg -y -i /tmp/screencast.mkv -c:a libfdk_aac -cutoff 20000 -vf fps=30 -pix_fmt yuv420p -c:v libx264 /tmp/screencast.mp4
tony-j
Established Member
Posts: 42
Joined: Fri Aug 25, 2017 11:36 pm
Has thanked: 6 times

Re: Compile and use ffmpeg with Fraunhofer codec for youtube

Post by tony-j »

Hi

I've noticed that youtube re-encode everything i put in there ,making hours of mastering useless: the volume is always lower and some frequencies are faded like compressed.I would like to know if this Fraunhofer is still the best codec to use for audio streaming and if it's still the best choice to compile ffmpeg. Thank you :-)
Jack Winter
Established Member
Posts: 381
Joined: Sun May 28, 2017 3:52 pm

Re: Compile and use ffmpeg with Fraunhofer codec for youtube

Post by Jack Winter »

Try limiting it less, try maybe a dynamic range of -13 to -16 dB rms
Reaper/KDE/Archlinux. i7-2600k/16GB + i7-4700HQ/16GB, RME Multiface/Babyface, Behringer X32, WA273-EQ, 2 x WA-412, ADL-600, Tegeler TRC, etc 8) For REAPER on Linux information: https://wiki.cockos.com/wiki/index.php/REAPER_for_Linux
tony-j
Established Member
Posts: 42
Joined: Fri Aug 25, 2017 11:36 pm
Has thanked: 6 times

Re: Compile and use ffmpeg with Fraunhofer codec for youtube

Post by tony-j »

Do you mean to sample the tracks with the master level about -13 db and then upload in youtube?

are they compressing it because the volume is too high? (-3db)

sounds like strange but i will try thank you.
Jack Winter
Established Member
Posts: 381
Joined: Sun May 28, 2017 3:52 pm

Re: Compile and use ffmpeg with Fraunhofer codec for youtube

Post by Jack Winter »

What I mean is that maybe they are quite heavily limited, so they get turned down in volume by the streaming services. So try to make it peak at maybe -0.3dB and to be around -15dB rms, and then see if the problems continues. What final max levels you can use, depends on the streaming service and varies a few dB between the different ones.
Reaper/KDE/Archlinux. i7-2600k/16GB + i7-4700HQ/16GB, RME Multiface/Babyface, Behringer X32, WA273-EQ, 2 x WA-412, ADL-600, Tegeler TRC, etc 8) For REAPER on Linux information: https://wiki.cockos.com/wiki/index.php/REAPER_for_Linux
tony-j
Established Member
Posts: 42
Joined: Fri Aug 25, 2017 11:36 pm
Has thanked: 6 times

Re: Compile and use ffmpeg with Fraunhofer codec for youtube

Post by tony-j »

i've never used a limiter with rms, i don't know how to do it.
I don't even use a limiter in the mastering part only compression sometimes.
I can only use the limiter with the peak threshold at 0 db before the outputs. Just in case something goes wrong.
Jack Winter
Established Member
Posts: 381
Joined: Sun May 28, 2017 3:52 pm

Re: Compile and use ffmpeg with Fraunhofer codec for youtube

Post by Jack Winter »

Let me see if I can explain what I suspect is going on. In simple terms youtube reencodes the audio after you have uploaded it, if it thinks that it's "too loud" then it will make it less loud. If it's too compressed it will just turn it down until it's somewhere in the range of -15dB rms.

Thus my suspicion that the material had been compressed/limited to make it sound loud, and my recommendation to target somewhere close to -0.3dB peak and -15dB rms. That is for the final audio file, and not necessarily something to do with whatever limiter you are using..

This is a simplified discussion and probably using some kind of LUFS metering would be more appropriate. I'm also not sure about the -15dB rms specifically for youtube, but all streaming services do something similar now days, though the exact peak, rms, lufs, etc values vary slightly between them.

I can't think of any other reason why youtube would turn down the volume of your uploaded audio..

But rereading the post, maybe you shouldn't encode to mp3 and just upload wave files instead. After all you'll lose quality at each encoding step..
Reaper/KDE/Archlinux. i7-2600k/16GB + i7-4700HQ/16GB, RME Multiface/Babyface, Behringer X32, WA273-EQ, 2 x WA-412, ADL-600, Tegeler TRC, etc 8) For REAPER on Linux information: https://wiki.cockos.com/wiki/index.php/REAPER_for_Linux
Post Reply