• Bubble
  • Bubble
  • Line
FFMPEG video encoding to save GB of data - which encoding is the best?
Andrei Clinciu Article AUthor
Andrei Clinciu
  • 2018-03-11T10:13:00Z
  • 4 min to read

I was back-up'ing my system and I reviewed everything after the stupid debian bug I've encountered.
I noticed that I had some 37 GB of video's from the 2nd semester of the CCCP study.
For 50 minutes of lecutre I had around 900 MB.
For a resolution of 1280x720 at 30 frames per second with 24bit color quality per pixel you would get a 30 GB file without any compression.
So clearly this is a 30:1 win and even the 900MB is a great compression if you stop to think about it since it was already compressed with the H264 codec.
I started to look around and test things with libtheora and ffmpeg I eventually got a 401 MB file.
For a aproximate bitrate of 1024K/s for the video the file size was roughly half. One problem I encountered with the libtheora was that it could not use all cores, which made it VERY SLOW.
Ok, i could convert all my videos at once, but that's pretty lousy. Not to mention the quality was reduced so theora wasn't an option.

ffmpeg theora

ffmpeg -i videofile.mp4 -c:a libvorbis -c:v libtheora -q:v 5 -q:a 3 -threads 16 vorbis_output.ogg

frame=71526 fps= 23 q=-0.0 Lsize= 392258kB time=00:39:44.20 bitrate=1347.8kbits/s dup=3 drop=0
video:371010kB audio:19197kB subtitle:0kB other streams:0kB global headers:6kB muxing overhead: 0.525613%

Don't let the threads option fool you, it went on 1 thread.
I also remembered about my wedding raw video amounted to 125.4 GB for 7 hours of raw video. The guys that filmed everything provided me with a beautiful 74.5 GB final edited video.
This totaled to around 200 GB or more of data for some videos. Converting this to OGG/theora would be great, but I didn't want to lose that much quality and I certainly didnt want it to be slow.
I have a dual octo core Xeon with hiperthreading after all. This means 32 total threads. I'd like to use some threads for the compression.

I looked started researching and I ended up with 3 main choices:

  • Mathrouska mkv
  • H.264,  HEVC better known as H.265
  • VPx, vp8, vp9


I set out to try the newest and best - H.265 - which claims it can get half the filesize of H264 while remaining at the same quality.

FFmpeg with AAC and H.265. Note that AAC is just the next generation MP3 which has no fees for distributing like MP3 and it's better.
I use 16 threads because that's the maximum allowed by the libx265 in FFMPEG.

ffmpeg -i forensic_lab_3.mp4 -c:v libx265 -crf 28 -c:a libvo_aacenc -b:a 92k -threads 16 forensic_lab_3.mp4

Past duration 0.763664 too large 58503kB time=00:40:28.69 bitrate= 197.3kbits/s dup=3 drop=0
frame=72914 fps=102 q=-0.0 Lsize= 61125kB time=00:40:30.40 bitrate= 206.0kbits/s dup=3 drop=0
video:31259kB audio:27294kB subtitle:0kB other streams:0kB global headers:1kB muxing overhead: 4.393103%
x265 [info]: frame I: 292, Avg QP:23.65 kb/s: 7448.88
x265 [info]: frame P: 17920, Avg QP:32.97 kb/s: 166.65
x265 [info]: frame B: 54702, Avg QP:35.30 kb/s: 44.80
x265 [info]: Weighted P-Frames: Y:0.1% UV:0.0%
x265 [info]: consecutive B-frames: 0.5% 1.4% 1.2% 91.0% 5.9%

encoded 72914 frames in 716.24s (101.80 fps), 104.40 kb/s, Avg QP:34.68

 

The input file was 800MB , the output was 62.6MB. Thus a 1277% improvement!! The quality was better than the theora encoding, this at a lower filesize!

Batch processing of files. Replace the *.mp4 to anyother fileformat:)

for i in *.mp4; do
 ffmpeg -i "$i" -c:v libx265 -crf 25 -c:a libvo_aacenc -b:a 92k -threads 16   "${i%.*}-H265.mp4"done

So from 37 GB of H264 mp4 converted to H265 I managed to get 4 GB of video. My PC hasn't had a good stresstest like this in a long time.

How about some Royalty Free goodness?

Now. HEVC and H264, H265 are all patented and not royalty free. Meaning sometimes you might have to pay money to use this codec.

If you're like me, you certainly liked Theora, Ogg and pure open source stuff.

There;s an alternative.
Google made VP8 and VP9. VPx is an open, royalty-free media file format.
What makes VP9 so great? It uses SIMD and can beat H265 in terms of performance to compression ratio. Another cool thing is that the webm format - VPx - can be run directly from your browser.
Meaning no other codecs are required to run them in your BROWSER.
And the best thing of all? You can use OPUS for AUDIO. Opus is the next generation vorbis/ogg. It's the best audio encoding out there for anything related to audio!

for i in *.mp4; do
 ffmpeg -i "$i" -c:v libvpx-vp9  -threads 16 -slices 16 -cpu-used -4 -crf 15 -b:v 0 -c:a libopus -vbr on -b:a 192k  -vf fps=30 "${i%.*}.webm"done

 

Unfortunately the threads option doesn't seem to have to much of a big impact with the libvpx-vp9. I figured out that if i use -threads 64 then it go a little faster but won't use all cores.. Or maybe it's so efficient that I CPU usage is not that high. Anyway, the speed of frames processed is quite slow.

Conclusion

  • H 265 gets around 115 FPS on 16 threads and is a great encoding. It's great if you have lots of videos you want to encode and save space AND retain quality. If you plan on only storing films and data I'd recommend using H265. It's the fastest one out there.
  • VP9 gets roughly 19 FPS on on 16 threads with 16 slices it's the best compression available but speed is a little slower. It's great for archiving data and web videos plus streaming if you don't want to get into paying high usage fees like H265 does. VP8 and VP9 are also available

 

So how did I store precious memories when I want bigger quality?

Audio above 192K will usually not have any difference unless you want to play it in cinema.

 ffmpeg -i "$i" -c:v libx265 -crf 21 -c:a libvo_aacenc -b:a 192k -threads 16   "${i%.*}-H265.mp4"

Ideas and comments

Andrei Clinciu
Andrei Clinciu

I'm a Full Stack Software Developer specializing in creating websites and applications which aid businesses to automate. Software which can help you simplify your life. Let's work together!

Building Great Software
The digital Revolution begins when you learn to automate with personalized software.

Find me on social media