61

We filmed a spokesperson on a green screen and have the video files ready in multiple formats.

With Flash we could use the wmode transparent within the param and embed tags, but is there something similar to this with the video and source tags in HTML5? Is it even possible to properly save .m4v or .ogv videos so that we can play these files with transparent backgrounds on our browsers?

Thanks

skaffman
  • 398,947
  • 96
  • 818
  • 769
DavidE
  • 611
  • 1
  • 6
  • 3

10 Answers10

32

Yes, this sort of thing is possible without Flash:

However, only very modern browsers supports HTML5 videos, and this should be your consideration when deploying in HTML 5, and you should provide a fallback (probably Flash or just omit the transparency).

Phrogz
  • 296,393
  • 112
  • 651
  • 745
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • 44
    The first demo uses SVG clipping to 'create' alpha regions for the video. The second demo uses two HTML5 Canvas along with a video with [explicit alpha pixels](http://jakearchibald.com/scratch/alphavid/compressed.mp4) to explicitly set the alpha every frame. Neither of these are a video with an alpha channel. – Phrogz Feb 20 '11 at 14:29
  • With Flash yo can do transparent background video with an FLV file, I did it before, and that was around 2008. – Max S. Sep 14 '15 at 13:58
  • I tried the second solution. It is working on a desktop, but not on mobile, is it supposed to work on mobile also? – Derzu Jul 26 '18 at 20:11
  • you can now use webm with transparency https://caniuse.com/#search=webm – movAX13h Jun 21 '19 at 10:34
30

Chrome 30> supports video alpha transparency.

http://updates.html5rocks.com/2013/07/Alpha-transparency-in-Chrome-video

Praveen Vijayan
  • 6,521
  • 2
  • 29
  • 28
  • 9
    WebM is required for this to work, only works with that. – nycynik Mar 14 '14 at 19:09
  • 1
    This is the only real answer - actually using ` – mikemaccana Jan 08 '16 at 13:57
23

Update: Webm with an alpha channel is now supported in Chrome and Firefox.

For other browers, there are workarounds, but they involve re-rendering the video using Canvas and it is kind of a hack. seeThru is one example. It works pretty well on HTML5 desktop browsers (even IE9) but it doesn't seem to work very well on mobile. I couldn't get it to work at all on Chrome for Android. It did work on Firefox for Android but with a pretty lousy framerate. I think you might be out of luck for mobile, although I'd love to be proven wrong.

nullability
  • 10,545
  • 3
  • 45
  • 63
17

These days, if you use two different video formats (WebM and HEVC), you can have a transparent video that works in all of the major browsers except Internet Explorer with a simple <video> tag:

<video>
  <source src="video.mov" type="video/quicktime">
  <source src="video.webm" type="video/webm">
</video>

Note: It's important that the WebM version come second since Safari currently supports WebM, but not WebM transparency.

Here's a demo you can test with

Adam Taylor
  • 4,691
  • 1
  • 37
  • 38
  • How did you generate the videos with transparency? Was it with the techniques outlined here: https://kitcross.net/hevc-web-video-alpha-channel/ ? – Amit Apr 28 '21 at 15:46
  • @Amit Yes, I generated the HEVC video using Finder – Adam Taylor Apr 29 '21 at 01:03
  • mp4 with h.265 encoding seems to also support a transparent channel that is supported in ios safari: https://supergeekery.com/blog/transparent-video-in-chrome-edge-firefox-and-safari-circa-2022 – Kyle Baker Jan 20 '23 at 20:07
14

I struggled with this, too. Here's what I found. Expanding on Adam's answer, here's a bit more detail, including how to encode VP9 with alpha in a WebM container.

First, here's a CodePen playground you can play with. Feel free to use my videos for testing.

<video width="600" height="100%" autoplay loop muted playsinline>
    <source src="https://rotato.netlify.app/alpha-demo/movie-hevc.mov" type='video/mp4'; codecs="hvc1">
    <source src="https://rotato.netlify.app/alpha-demo/movie-webm.webm" type="video/webm">
</video>

And here's a full demo page using z-index to layer the transparent video on top and below certain elements. (You can clone the Webflow template.) enter image description here

So, we'll need a WebM movie for Chrome, and an HEVC with Alpha (supported by Safari on all platforms since 2019).

Which browsers are supported?

For Chrome, I've tested successfully on version 30 from 2013. (Caniuse WebM doesn't seem to say which WebM codec is supported, so I had to try my way.) Earlier versions of Chrome seem to render a black area.

For Safari, it's simpler: Catalina (2019) or iOS 11 (2019).

Encoding

Depending on which editing app you're using, I recommend exporting the HEVC with Alpha directly.

But many apps don't support the WebM format, especially on Mac, since it's not a part of AVFoundation.

I recommend exporting an intermediate format like ProRes4444 with an alpha channel to not lose too much quality at this step. Once you have that file, making your WebM is as simple as:

ffmpeg -i "your-movie-in-prores.mov" -c:v libvpx-vp9 movie-webm.webm

See more approaches in this blog post.

mfluehr
  • 2,832
  • 2
  • 23
  • 31
Morten J
  • 814
  • 10
  • 21
  • 1
    This helped me a lot thanks :) Important note if someone read this, you may want to add a `background: transparent` to your video styles, because things like normalize add a black background to video elements – Nicolas R Jun 23 '21 at 15:21
  • Very thorough explanation, thanks a lot! – Jos Nov 10 '21 at 07:29
  • That's great and so far I think is the best solution unfortunately doesn't work with Opera. Positive results with Chrome, Firefox, Brave, and Safari on OS. – markobarna Apr 23 '23 at 11:40
2

At this time, the only video codec that truly supports an alpha channel is VP8, which Flash uses. MP4 would probably support it if the video was exported as an image sequence, but I'm fairly certain Ogg video files have no support whatsoever for an alpha channel. This might be one of those rare instances where sticking with Flash would serve you better.

Andrew
  • 1,819
  • 5
  • 23
  • 31
2

While this isn't possible with the video itself, you could use a canvas to draw the frames of the video except for pixels in a color range or whatever. It would take some javascript and such of course. See Video Puzzle (apparently broken at the moment), Exploding Video, and Realtime Video -> ASCII

Phrogz
  • 296,393
  • 112
  • 651
  • 745
cthom06
  • 9,389
  • 4
  • 36
  • 28
1

Mp4 files can be playable with transparent background using seeThrou Js library. All you need to combine actual video and alpha channel in the single video. Also make sure to keep video height dimension below 1400 px as some of the old iphone devices wont play videos with dimension more than 2000. This is pretty useful in safari desktop and mobile devices which doesnt support webm at this time.

more details can be found in the below link https://github.com/m90/seeThru

Prathap Parameswar
  • 489
  • 1
  • 3
  • 11
0

webm format is the best solution for Chrome > 29, but it is not supported in Firefox IE and Safari, the best solution is using Flash (wmode="transparent"). but you have to forget "ios".

prakashapkota
  • 321
  • 2
  • 6
0

Quicktime movs exported as animation work but in safari only. I wish there was a complete solution (or format) that covered all major browsers.