Can I have a video with transparent background using HTML5 video tag?
Asked Answered
B

10

63

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

Brimful answered 1/11, 2010 at 21:41 Comment(0)
X
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).

Xhosa answered 5/11, 2010 at 13:18 Comment(4)
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 to explicitly set the alpha every frame. Neither of these are a video with an alpha channel.Fagoting
With Flash yo can do transparent background video with an FLV file, I did it before, and that was around 2008.Arian
I tried the second solution. It is working on a desktop, but not on mobile, is it supposed to work on mobile also?Thereabouts
you can now use webm with transparency caniuse.com/#search=webmVocable
P
30

Chrome 30> supports video alpha transparency.

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

Persevering answered 15/8, 2013 at 5:25 Comment(2)
WebM is required for this to work, only works with that.Thruster
This is the only real answer - actually using <video>. See developers.google.com/web/updates/2013/07/… for more.Lablab
N
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.

Noteworthy answered 12/3, 2013 at 19:8 Comment(4)
I also had no success on mobile devices, just on desktop. Is there any solution?Thereabouts
Do you also lose control of video playback by using Canvas? I would imagine that you do.Aglitter
Webm is supportedWhiffet
update: safari now supports an alpha channel in mp4 with h.265 encoding, as documented and explained here: supergeekery.com/blog/…Ascend
L
19

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

Laylalayman answered 27/8, 2020 at 1:10 Comment(3)
How did you generate the videos with transparency? Was it with the techniques outlined here: kitcross.net/hevc-web-video-alpha-channel ?Indohittite
@Indohittite Yes, I generated the HEVC video using FinderLaylalayman
mp4 with h.265 encoding seems to also support a transparent channel that is supported in ios safari: supergeekery.com/blog/…Ascend
B
16

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.

Bopp answered 7/1, 2021 at 13:40 Comment(3)
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 elementsDisinclined
Very thorough explanation, thanks a lot!Youngstown
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.Erastes
B
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.

Became answered 5/11, 2010 at 12:41 Comment(0)
B
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

Beckon answered 5/11, 2010 at 12:50 Comment(0)
G
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

Gynecologist answered 5/11, 2018 at 5:58 Comment(0)
I
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".

Improvisatory answered 10/12, 2013 at 10:45 Comment(0)
M
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.

Merciless answered 30/7, 2014 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.