/============================================\
              || ____|  ____|   \  |   _ \   ____|   ___| ||
              || |      |      |\/ |  |   |  __|    |     ||
              || __|    __|    |   |  ___/   |      |   | ||
              ||_|     _|     _|  _| _|     _____| \____| ||
              \============================================/      
  mar 2024                                                      by Nobodyatall

\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /
 \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/

 ___ _   _ _____ ____   ___
|_ _| \ | |_   _|  _ \ / _ \
 | ||  \| | | | | |_) | | | |
 | || |\  | | | |  _ <| |_| |
|___|_| \_| |_| |_| \_\\___/

  greetings and bienvenue,
  to my zine

  a lot of people on certain Internet forums and chat areas ask me how I fit
  certain high quality videos in such small yet high quality constraints.
  this is purely using ffmpeg and ab-av1.

  I'll be showing you exactly how to use these programs to achieve better file
  sizes while also maintaining visual quality as best as possible.

 /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \

  before we talk about ffmpeg or ab-av1 we need to discuss VMAF. VMAF being the
  visual quality of a video in relation to our eyes. the scale reaches from
  0-100 after 93 our eyes can't fully tell the difference between the quality
  of the original or the new version.

  I try to to keep vmaf at 94-95 for 1080p videos and higher but normally no 
  lower than 88 for 720p videos. you can still use 95 for 720p and get an amazing 
  result with a small file size and great quality.

 /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \
  
  Now ab-av1, what is it? It's an application to easily use ffmpeg to both
  calculate VMAF and encode using optimal settings. How do you get it?
  there's a github with a windows binaryand you can install it with your
  linux package managers.

  sudo pacman -Sy ab-av1
  sudo apt-get install ab-av1
  or just clone it and build it via the guide.
  make sure you have ffmpeg installed as well.

 /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \

  lets use ab-av1.

  I have a very highly lossless animated video.
  name: bdan.mp4
  video: h264 high 1080p 24fps 14989kb/s
  audio: aac 317kb/s
  size: 858mb
  length: 8 min
  
  see I want to send bdan.mp4 to a friend but its just too damn big! I want it
  smaller than 25mb.

  this is the command we'll run just to see if we can do it with 1080p:

  ab-av1 crf-search --encoder libx264 --min-vmaf 95 --pix-format yuv420p \
  -i bdan.mp4

  after running this it says I can get a file size of 236.94mb using 
  --crf 19.7! but that's not what I'm after. I want 25mb or less.
  
  lets add --vmaf-scale="1280x720" --vfilter "scale=-1:720" to the previous
  ab-av1 command.

  111.62mb with 95 VMAF. Very good. lets change the --min-vmaf to 88

  we got crf 28.4 VMAF 88 31.37mb. SO CLOSE. remember what I said about not
  normally going below 88? this is one of those cases. --min-vmaf 84.

  now we have crf 31.2 VMAF 84 and 23.46mb. almost perfect. except thats just the
  video stream. the audio stream is 18mb alone. but we can reduce that to about
  4mb using ffmpeg. (we'll do that in a bit) but lets make room for that audio.

  --vmaf-min 81
  
  finally 32.8 VMAF 81 at 20.00mb we are ready for ffmpeg.

 /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \

  ffmpeg is a beautiful thing. its robust, its got codecs, and its fast.
  lets use ffmpeg to make that audio about 4mb in size.

  ffmpeg -y -i bdan.mp4 -c:a aac -b:a 64k -vn output.m4a

  3.9mb close enough! audio is kind of trial and error but it should be quick.

  Now we can convert just the video. not the audio we can stich them back
  together once we finish the video.

  ffmpeg -y -i bdan.mp4 -c:v libx264 \
  -profile:v high -level:v 4.2 -crf 32.8 -pix_fmt yuv420p -keyint_min 72 \
  -g 72 -s 1280x720 -movflags +faststart -preset veryslow -an output.mp4

  so there's not TOO much to explain here. -s scales, -movflags +faststart
  allows for streaming the mp4 file, -preset veryslow for quality and 
  compression, -an for no audio. Finally -keyint_min and -g, which should be
  calculated thusly. how many seconds before an I-Frame. 1? 2? 3? 10?

  this is easy. this is an 8 minute video at 24fps for 2 seconds per I-Frame

  2 * 24 = 48. so -keyint_min 48 -g 48 would be the command.

  i chose 3. 3 * 24 = 72 -keyint_min 72 -g 72. easy. the larger the seconds
  the smaller the video but the worse it could look.

  the video file output.mp4 is 19.6mb and the audio file output.m4a is 3.9mb

 /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \

  Lets combine the video and audio.
  ffmpeg -i output.mp4 -i output.m4a -c copy -movflags +faststart output-va.mp4

  name: output-va.mp4
  video: h264 high 720p 24fps 340kb/s
  audio: aac 66kb/s
  size: 23.6mb
  length: 8 min

  so as you can see even when the video in question is almost 1gb we can still
  create a more than watchable experience and shareable with friends, enemies,
  and other weirdos on the Internet. 

  ~Nobodyatall

 /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \