ProRes 4444<\/a>. This is an important step, as the Encode Media tool won\u2019t accept just any video \u2014 it will fail unless you provide the format it accepts. It gave me a headache or two until I found out the correct encoding for the input file.<\/p>\n\n\n\nWe can create input video using FFmpeg. As we did when creating WebM, we just have to run FFmpeg with the proper arguments:<\/p>\n\n\n\n
ffmpeg -framerate 25 -i unscreen-%3d.png -c:v prores_ks -pix_fmt yuva444p10le -alpha_bits 16 -profile:v 4444 -f mov -vframes 150 output.mov<\/code><\/pre>\n\n\n\nFrom this point on, a Mac is needed. But the process is simple and does not involve typing anything in terminal, so even if you ask a non-techy friend with a Mac to do it for you, I\u2019m sure they can manage.<\/p>\n\n\n\n
Having ProRes4444 video ready, you can navigate to the containing folder in Finder, right-click on it, and in the context menu, select Encode Selected Video Files<\/strong>.<\/p>\n\n\n\n <\/figure>\n\n\n\nA window will appear. Select the desired HEVC quality (there are two to choose from), and check the option to Preserve Transparency.<\/strong> Then click the \u201cContinue\u201d button.<\/p>\n\n\n\n <\/figure>\n\n\n\nAfter a moment you should see a new file, which is encoded in HEVC with alpha. It\u2019s done! Opening this file in Safari confirm that the transparency works.<\/p>\n\n\n\n <\/figure>\n\n\n\nLast but not least, it\u2019s time to use videos on the website!<\/p>\n\n\n
Serving transparent videos for all browsers<\/h3>\n\n\n We can use <video><\/code> element to serve videos. If the browser supports the given format in the src<\/code> attribute, it will just work. But as I\u2019ve mentioned, we need to serve HEVC for Safari and WebM for other browsers.<\/p>\n\n\n\nThere is one more thing to take care of: some browsers support WebM or HEVC itself but without transparency<\/em>. Those browsers will play the video but with a black background instead of an alpha channel. For example, Safari 12 will play HEVC without transparency. That\u2019s unacceptable for us.<\/p>\n\n\n\nNormally, I’d use the <source \/> element to provide the video along with multiple formats as fallbacks, and the browser would pick the one it supports. However, since we want to show HEVC only on specific Safari versions, we will have to set the video src<\/code> attribute instead using JavaScript.<\/p>\n\n\n\nHEVC with transparency is supported in iOS 13 and Mac Safari 13, so let\u2019s start by detecting those. The recommended way to adjust website features based on support is by detecting the existence of an API rather than looking at the User Agent, but I didn\u2019t find any simple way to detect if the browser supports transparency in video format. Instead, I came up with my own solution. It\u2019s not pretty, but it does the job.<\/p>\n\n\n\n
function supportsHEVCAlpha() {\n const navigator = window.navigator;\n const ua = navigator.userAgent.toLowerCase()\n const hasMediaCapabilities = !!(navigator.mediaCapabilities && navigator.mediaCapabilities.decodingInfo)\n const isSafari = ((ua.indexOf('safari') != -1) && (!(ua.indexOf('chrome')!= -1) && (ua.indexOf('version\/')!= -1)))\n return isSafari && hasMediaCapabilities\n}<\/code><\/pre>\n\n\n\nThis targets Safari 13 and above by looking at navigator.mediaCapabilities<\/code>, which is not supported in older versions, as well as looking that the browser is Safari at all. If the function returns true<\/code>, then I\u2019m good to go with HEVC alpha. For any other case, I\u2019ll load WebM video.<\/p>\n\n\n\nHere’s an example of how this comes together in HTML:<\/p>\n\n\n\n
<video id=\"player\" loop muted autoplay playsinline><\/video>\n\n<script>\nconst player = document.getElementById('player');\nplayer.src = supportsHEVCAlpha() ? 'output.mov' : 'output.webm';\n<\/script><\/code><\/pre>\n\n\n\nIf you\u2019re curious about those loop muted autoplay playsinline<\/code> arguments on video element, those are about replicating a GIF-like experience:<\/p>\n\n\n\nWithout muted<\/code>, the video will not play without user interaction.<\/li>With playsinline<\/code>, on iOS, the video plays right where it is in the layout instead of opening up in fullscreen.<\/li>With loop<\/code>, the video repeats over and over.<\/li>With autoplay<\/code>, it will start playing the video automatically on page load (as long as we also have muted<\/code> in place).<\/li><\/ul>\n\n\n\nThat\u2019s it! We have a lightweight, performant and high-quality solution for transparent videos, that works on most modern browsers (at least the last two recent versions of Chrome, Firefox, Safari and Edge). We can add some basic HTML and CSS to integrate static content with it and make it a little bit more real-life, or just use the same idea that\u2019s in my demo. That wasn\u2019t too bad, was it?<\/p>\n\n\n\n
Hey, but what about IE 11? My whole company is using it!<\/p><\/blockquote>\n\n\n\n
In general, I\u2019d recommend limiting a feature like this to modern browsers and hiding the video in IE. The video is probably not a critical element of the website. Although, I used to work on a commercial project where IE 11 support was a must and I was forced to figure something out to show the transparent video there. In the end, I ended up cycling through PNG images with JavaScript. I reduced the amount of frames and switched between them using a timer. Sure, the performance was awful, but it worked. The video was quite important to the whole design so we intentionally decided to sacrifice performance on IE 11 to provide the full experience.<\/p>\n\n\n
Summary<\/h3>\n\n\n I hope that I\u2019ve saved you some time researching the idea of alpha-transparent video and that now you\u2019re be able to incorporate animated elements like this on your website. I bet you\u2019ll need it someday!<\/p>\n\n\n\n
Do you have a different idea of how to use transparent video? Or maybe some experience with that already? Let me know in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"
As websites are becoming more and more dynamic when it comes to design, there is sometimes a need to incorporate complex, animated elements. There are many ways to do that from CSS transitions to 3D rendering on canvas, and animated SVG. But it is often easier to use a <video> since they can be rather […]<\/p>\n","protected":false},"author":279977,"featured_media":328366,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"Overlaying Video With Transparency While Wrangling Cross-Browser Support by @MaciekCaputa","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4],"tags":[1200,1139,672],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/12\/transparent-video-overlay.png?fit=1200%2C600&ssl=1","jetpack-related-posts":[{"id":64,"url":"https:\/\/css-tricks.com\/css-transparency-settings-for-all-broswers\/","url_meta":{"origin":328311,"position":0},"title":"CSS Transparency Settings for All Browsers","date":"November 4, 2007","format":false,"excerpt":"Transparency is one of those CSS properties that has a weird history and requires lots of different properties and values to ensure cross browser compatibility that goes back as far as you can. To cover all your bases, you need a bunch of CSS statements. Fortunately they don't interfere with\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":318216,"url":"https:\/\/css-tricks.com\/nailing-the-perfect-contrast-between-light-text-and-a-background-image\/","url_meta":{"origin":328311,"position":1},"title":"Nailing the Perfect Contrast Between Light Text and a Background Image","date":"August 7, 2020","format":false,"excerpt":"Have you ever come across a site where light text is sitting on a light background image? If you have, you'll know how difficult that is to read. A popular way to avoid that is to use a transparent overlay. But this leads to an important question: Just how transparent\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/07\/contrast-bg.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":359032,"url":"https:\/\/css-tricks.com\/maybe-there-kinda-is-background-opacity\/","url_meta":{"origin":328311,"position":2},"title":"Maybe there kinda is background-opacity?","date":"December 15, 2021","format":false,"excerpt":"I was reading Jake's \"Cross-fading any two DOM elements is currently impossible\" which is a wonderfully nerdy deep dive into how there is no real way to literally cross-fade elements. Yeah, you can animate both of their opacities, but even that isn't quite the same. Turns out there is a\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/12\/Screen-Shot-2021-12-10-at-2.39.42-PM.png?fit=1169%2C1200&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2151,"url":"https:\/\/css-tricks.com\/rgba-browser-support\/","url_meta":{"origin":328311,"position":3},"title":"RGBa Browser\u00a0Support","date":"May 19, 2010","format":false,"excerpt":"This article was originally published on February 2, 2009 and is now being updated to clarify the concept and update the information for modern browsers. RGBa is a way to declare a color in CSS that includes alpha transparency support. It looks like this: div { background: rgba(200, 54, 54,\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/csstricks-uploads\/rgbaexample.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":20295,"url":"https:\/\/css-tricks.com\/uniqlo-stripe-hovers\/","url_meta":{"origin":328311,"position":4},"title":"Uniqlo Stripe Hovers","date":"February 27, 2013","format":false,"excerpt":"For the larger promotional boxes in the design of Uniqlo.com, they use animated stripes that reveal on hover. It's kind of awesome, if you ask me. Perhaps because they wanted to share the awesome with as many different browsers as possible, they used an animated GIF to produce the effect.\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":377264,"url":"https:\/\/css-tricks.com\/some-cross-browser-devtools-features-you-might-not-know\/","url_meta":{"origin":328311,"position":5},"title":"Some Cross-Browser DevTools Features You Might Not Know","date":"March 22, 2023","format":false,"excerpt":"I spend a lot of time in DevTools, and I\u2019m sure you do too. Sometimes I even bounce between them, especially when I\u2019m debugging cross-browser issues. DevTools is a lot like browsers themselves \u2014 not all of the features in one browser\u2019s DevTools will be the same or supported in\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2023\/02\/s_7E25E76B5C1A2A0120D43B477F8F8B0FA75578B215D36F3A918DC29D997AA0F4_1674721838803_Screenshot2023-01-26at14.00.01.png?fit=1200%2C862&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"featured_media_src_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/12\/transparent-video-overlay.png?fit=1024%2C512&ssl=1","_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/328311"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/279977"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=328311"}],"version-history":[{"count":6,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/328311\/revisions"}],"predecessor-version":[{"id":330590,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/328311\/revisions\/330590"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media\/328366"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=328311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=328311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=328311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}