Lossless Video

preview_16x9

# Today I figured out how to make compressed video files with lossless video with lossless audio.

# I used a program called FFmpeg to create a MKV file that compresses video using the VP9 codec and audio using the FLAC codec. Here’s the DOS incantation that makes it happen.

#Lossless Video and Lossless Audio as a MKV file

ffmpeg.exe -i "%~1" -c:v libvpx-vp9 -threads 11 -lossless 1 -pix_fmt yuv444p -c:a flac "%~n1.mkv"

# VP9 and MP4 video codecs are both capable of lossless compression if you use the right settings. But VP9 tends to be a little smaller and is more compatible with older software such as VirtualDub when it reads the video using Windows codecs.

# This is not the first time I’ve experimented with lossless compression. Until now I have been using AVI files that store video compressed using a codec called TSCC, which came from the Camtasia screen recorder software. An excellent program that I still use to this day. But it has been 20 years since that program came out and newer compression formats can create files that are about half the size.

# How do I know if it’s lossless? By recording one of my pixel-art games, exporting a single frame from the middle of the video, and then using Photoshop’s magic wand tool to see if each color is uniform.

# I only care about lossless compression for the original recording, because I can make changes and conversions without destroying all the tiny pixels I drew.

# For presentation I don’t mind using normal lossy compression, since the human eye won’t be able to tell the difference, especially in motion.

# That said it is technically possible to display lossless video in a browser. A WEBM file can also store VP9 compressed video. But the audio has to be lossy. So it’s not quite good enough for archival purposes.

#Lossless Video and Lossy audio as a WEBM file

ffmpeg.exe -i "%~1" -c:v libvpx-vp9 -threads 11 -lossless 1 -pix_fmt yuv444p -acodec libvorbis -f webm "%~n1.webm"

# Lossless MP4 is possible but not all web browsers can play it.

#Lossless Video and Lossy audio as a MP4 file

ffmpeg.exe -i "%~1" -pix_fmt yuv444p -c:v libx264 -qp 0 -preset veryslow -c:a libmp3lame -abr 1 -b:a 128k -threads 10 "%~n1.mp4"

# This information will change in the future. While this technique will continue to work, newer compression formats are likely to emerge over time. I have watched video compression constantly change over the past 40 years, so nothing remains “standard” for long. Here’s a rough timeline just off the top of my head.

  • # Around 1991 Cinepak was created to enable playing video from single-speed CD rom drives.

  • # Around 1995 MPEG 1 was the best video codec and built into things like the PlayStation 1.

  • # Around 2000 Sorenson Video was the best video codec in Apple’s Quicktime video player and Macromedia Flash, with better compression and image quality.

  • # Around 2001 DVD used MPEG 2 compression and the PlayStation 2 could play them.

  • # Around 2004 DivX was the king of pirated video, able to (just barely) store an entire movie on a single CD-r.

  • # Around 2006 the PlayStation 3 came out with Blue-ray disc support which used MP4 h264 compression, so that format took over most hardware video players and graphics cards.

  • # Around 2015 web browsers added the ability to play WEBM compressed with the open-source VP8 codec, to avoid paying MP4 patents.

  • # MP4 then added (patented) h265 HEVC compression

  • # Web browsers then added (open-source) WEBM with VP9 compression

  • # AV1 (open-source) is being developed to replace VP9

# Every new codec created smaller files with better image quality. It has been quite a ride.