Compile and use ffmpeg with Fraunhofer codec for youtube
Posted: Tue Mar 18, 2014 2:59 pm
Best encoderformats (both audio and video) for youtube?
https://support.google.com/youtube/answer/1722171?hl=en
Update repositories:
Create sourcefilefolder:
Remove old precompiled version:
Install some developer versions (source code) and compiler tools:
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:
2 libmp3lame, MP3:
3 libvpx, VP8/VP9 video:
H.264 video encoder:
Libopus:
FDK-AAC, Fraunhofer AAC audio encoder:
FFMPEG
Reload terminal
To see current version and compiled libraries:
All Codecs available:
Fraunhofer libfdk_aac? H.264 libx264? Check:
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:
Convert all pictures you want to use to fragments of video. -t 5.00 is the number of seconds. For example like this:
Save to mylist.txt enough videotime to fill the audiotime with:
Combine them into one single video:
Now take that video, and include audio.wav to write 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.
Search for a nice video you want to grab a sample from, and select & copy the url, like for example:
Then, inside a terminal, you can type:
yout(tab) (ctrl-shift-insert) (enter):
Which will work out as something like:
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:
To extract audio from single mp4/flv videos and convert to mp3/aac use something like this:
Small script to extract audio from all videos at once:
save to /home/YOURNAME/extractaudio.py:
To run:
Screencast
Grab test video (size 1920x1080) from x11 (the screen)
(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:
Record sound from speakers compressed with libmp3lame encoder:
Or record audio compressed with Fraunhofer libfdk_aac encoder:
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:
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.
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:
https://support.google.com/youtube/answer/1722171?hl=en
This can be resolved by compiling ffmpeg in a terminal like this: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.
Update repositories:
Code: Select all
sudo apt-get update
Code: Select all
mkdir ~/sourcefiles
Code: Select all
sudo apt-get remove ffmpeg
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
1 YASM:
Code: Select all
sudo apt-get install yasm
Code: Select all
sudo apt-get install libmp3lame-dev
Code: Select all
sudo apt-get install libvpx-dev
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
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
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
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
To see current version and compiled libraries:
Code: Select all
ffmpeg
Code: Select all
Ffmpeg -codecs
Code: Select all
ffmpeg -codecs | grep fdk
ffmpeg -codecs | grep 264
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
Now you can create a video.mp4 file, that you can upload to youtube, like this:-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.
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
Loop over a bunch of pictures:-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
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
Code: Select all
file 'picture000.mp4'
file 'picture001.mp4'
file 'picture002.mp4'
file 'picture001.mp4'
file 'picture002.mp4'
file 'picture003.mp4'
Code: Select all
ffmpeg -y -f concat -i mylist.txt -vf fps=30 -pix_fmt yuv420p -aspect 16:9 -c:v libx264 pictures.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
You can also download videos from youtube.
Code: Select all
sudo apt-get install youtube-dl
Code: Select all
http://www.youtube.com/watch?v=jbQlsV0sB5Y
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
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
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'
Code: Select all
python extractaudio.py
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
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
Code: Select all
ffmpeg -f alsa -ac 2 -i pulse -c:a libmp3lame -ar 48000 /tmp/mp3sound.mp3
Code: Select all
ffmpeg -f alsa -ac 2 -i pulse -c:a libfdk_aac /tmp/aacsound.aac
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
Code: Select all
ffmpeg -y -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 1920x1080 -i :0.0 /tmp/screencast.mkv
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