V7: Video Killed the Web Browser Star
An HTML odyssey
So I thought I knew as much as I needed to know about the HTML <video> element, and as usual, I was wrong. I knew I could specify a source file with a src attribute on either <video> or a nested <source> element. I knew what to expect from a bunch of other attributes, like autoplay, controls, loop, muted, and the obvious height and width. I knew the poster attribute gave me control over the video’s static preview image. And I knew that in the absence of poster, most browsers display the first frame of the video as a preview image.
One notable exception on that last bit is iOS Safari, which simply displays a transparent rectangle with a play button, a perplexing default that is useful to absolutely no one. The vast majority of my site’s hundreds of videos had no associated poster images, and I generally prefer not to negotiate with terrorists, so I found a workaround to force iOS Safari to behave in the same reasonable manner as other browsers: media fragments, which can specify the point in the video where the browser should begin playing. Tacking #t=0.001 to the end of the video’s src value tells the browser to start at the one-millisecond mark, which makes iOS Safari display the first frame as a preview image. Problem solved! Or at least that problem was solved. Another soon emerged.
I noticed that pages with multiple videos were loading slowly. It had long been my understanding that browsers initially loaded a video’s first frame, or maybe buffered the first few seconds, and didn’t load more until the user pressed play. Could it be that my assumption was wrong and the browser was just preloading the entire video? A quick test on PageSpeed Insights revealed to my great horror that indeed, my concert diary landing page, which at the time had 37 videos on it, weighed two hundred fifty-six megabytes. Yikes.
Consequently, I soon learned about the preload attribute, which lets you tell the browser how to load the video. A value of none means nothing from the video should be preloaded, a value of metadata indicates that only the metadata (like the video length) should be preloaded, and a value of auto means the entire video file should be preloaded. metadata is the spec’s recommended default, but apparently different browsers have different defaults, so results vary across browsers if you’re not intentional about a preload value.
OK, fine, preload="metadata" seemed to be what I was looking for. Problem solved! Except it wasn’t. The page weight remained the same, with multiple browsers apparently interpreting preload="metadata" exactly the same as preload="auto", which was a frustrating thing to discover for a person who believes words mean things. After testing different values across browsers, as far as I can tell, the only way to be sure no browsers will take it upon themselves to preload your entire big-ass video file is to use preload="none", which sounds fine until you learn that means not only that iOS Safari is right back where we started, but every other browser has joined its no-preview-without-poster crusade.
Let’s recap:
- iOS Safari won’t show a video preview image without a
posterattribute, - unless you force its hand by adding a media fragment like
#t=0.001to the end of the video’ssrc; - meanwhile, the only thing that will stop iOS Safari and other browsers from preloading the entire video file is
preload="none", - which obliterates not only the iOS Safari preview images enabled by the media fragment hack, but preview images in all other browsers as well.
The moral of the story is: Browsers are not smart about video and will force you to go to an unreasonable amount of trouble to get reasonable results, which is how, after beginning this journey by trying to avoid generating nearly 400 poster images, I ended up having to generate nearly 400 poster images.
Someone more clever than I would have approached this task by finding a way to add a batch process to an FFmpeg workflow, but I opted instead to spend a few hours in a hospital waiting room dragging the video files one-by-one into the Video Thumbnail Generator web app, made by a kind soul known only as Curtis, who in the end is this story’s only hero.