How to apply CSS filter to video without affecting controls in HTML5? - html

If I apply a CSS filter to a video element, for example:
video:first-child {
filter: brightness(40%);
}
<video controls></video> vs.
<video controls></video>
Then I find that the same effect applies to the media controls UI that the browser provides (via controls attribute on the video tag).
Are there any techniques that allow for applying the filter to the video content without the controls being affected?

Don't think this is possible, but you could make your own play/pause buttons etc using javascript, then style the video? Like below:-
(function (window, document, $, undefined) {
'use strict';
$(document).ready(function () {
$('#play').on('click', function(){
$('.my-video')[0].play();
});
$('#stop').on('click', function(){
$('.my-video')[0].pause();
});
});
})(window, document, jQuery);
video {
border: 10px solid green !important;
opacity: 0.6;
box-shadow: 12px 9px 13px rgba(255, 0, 255, 0.75);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<video width="320" height="240" class="my-video">
<source src="https://www.w3schools.com/html/movie.mp4" type="video/mp4">
<source src="https://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
<button type="button" id="play">Play</button>
<button type="button" id="stop">Stop</button>

Related

Starting a video by hovering over a div

I used this solution to start videos (each separately) when I hover them:
//play video on hover
$(document).on('mouseover', 'video', function() {
$(this).get(0).play();
});
//pause video on mouse leave
$(document).on('mouseleave', 'video', function() {
$(this).get(0).pause();
});
It works really nice. But actually I want to start a video when I hover its titles (so another div, start each video separately when hovering its own titles). Does anyone has a solution for that?
I have my Titles in a div called »video-titles« and my videos embedded like this:
<HTML Embed>
<div id="w-embed">
<video class="video" position= "absolute" top= "0px" margin= "0 auto" height= "auto" width= "100%" preload="auto" muted loop>
<source src="myvideo.mp4" type='video/mp4;' />
</video>
</div>
Here is my site:https://monkeyberlin-preview-3a28-d94aa03c105a2.webflow.io/
Cheers and thanks for the help!
Yes, here is an example to play a video when you hover its title.
$(document).ready(function() {
$(".video_inside h3").hover(function() {
$(this).parent().children('video')[0].play();
}, function() {
var el = $(this).parent().children("video")[0];
el.pause();
el.currentTime = 0;
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="video_inside">
<video preload="auto" muted="muted" loop>
<source src="https://giant.gfycat.com/VerifiableTerrificHind.mp4" type="video/mp4">
<source src="https://giant.gfycat.com/VerifiableTerrificHind.webm" type="video/webm">
</video>
<h3>Video Title</h3>
</div>
Note: make sure to paste the JS code below the jQuery file.
You can use like this
$(document).ready(function() {
$('#title').hover(function() {
$('#video').get(0).play();
});
});

How to set exact width and height of video content in video tag

Having this video tag:
<video id="video" width="300" height="300" autoplay></video>
and
var video = document.getElementById('video') as any;
var mediaConfig = { video: true };
// Put video listeners into place
navigator.mediaDevices.getUserMedia(mediaConfig).then(function (stream) {
video.src = window.URL.createObjectURL(stream);
video.play();
}, (e) => {
console.log('An error has occurred!', e)
});
I display my camera stream on the video tag. However, width & height of 300px are width of whole tag, and video itself has some bottom / top padding, however this padding is not in CSS. How can I make video size to be exactly 300x300?
Is that what you are looking for? I just tried with overlapping a div upon the video with 300px*300px. May be this will help you..Try once.
<div style="width: 300px; height:300px; overflow: hidden;">
<video loop="loop" style="width: 100%;" autoplay="autoplay">
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4"/>
<source src="https://www.w3schools.com/html/mov_bbb.ogg" type="video/ogg"/>
</video>
</div>

Full-screen background video does not autoplay

I'm making a simple web page, and I wanted the first window to have a full screen playing video. I tried to embed it like this:
<div class="section active">
<video id="video_background" preload="auto" autoplay="true" loop="loop" muted="muted" volume="0">
<source src="videos/Space.mp4" type="video/mp4">
<source src="videos/Space.webm" type="video/webm">
Video not supported
</video>
and
#video_background {
position: absolute;
bottom: 0px;
right: 0px;
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
z-index: 69;
overflow: hidden;
}
The first frame shows up, but it doesn't start playing. The files are in their proper location. Why could this be? I'm using fullPage.js if it makes any difference.
Please check this link:
https://github.com/alvarotrigo/fullPage.js/issues/267
You will have to make use of the callbacks afterRender or afterLoad for it.
$.fn.fullpage({
afterRender: function(){
$('#video')[0].play();
}
});
Now you can find also an available example to download in the examples folder of the plugin.
Try this
<div class="section active">
<video id="video_background" autoplay loop muted preload="auto">
<source src="videos/Space.mp4" type="video/mp4">
<source src="videos/Space.webm" type="video/webm">
Video not supported
</video>

HTML5 video - Get only soundtrack from video

I would like to load a video where I only present the sound of the video to the user. In fact if I could only download the sound track from the video, it would be best.
Is it possible to load a video with the tag and then only get the sound?
The videos are encoded into H.264 codec in .m4v or .mp4 file extensions.
I think downloading only the sound track is not possible, so you have to play the video anyway, but you can hide it.
You can use a js-library (such as jplayer or videojs, so you have the controls) and remove the video via css.
Have a look at this js-fiddle: http://jsfiddle.net/8Hm6y/
Insert the plugin into the Head of your document:
<link href="http://vjs.zencdn.net/4.1/video-js.css" rel="stylesheet">
<script src="http://vjs.zencdn.net/4.1/video.js"></script>
The HTML where you specify the source files:
<video id="" class="video-js vjs-default-skin"
controls preload="auto" data-setup='{"controls": true}'>
<source src="http://video-js.zencoder.com/oceans-clip.mp4" type='video/mp4' />
<source src="http://video-js.zencoder.com/oceans-clip.webm" type='video/webm' />
<source src="http://video-js.zencoder.com/oceans-clip.ogv" type='video/ogg' />
</video>
The CSS:
#vjs_video_4_html5_api {
display:none; /* remove video */
}
.vjs-big-play-button {
display:none !important; /* remove play button inside video*/
}
#vjs_video_4{
height:38px !important; /* make container smaller */
}
.vjs-control-bar { /* make controlbar visible */
visibility: visible !important;
opacity: 1 !important;
transition-duration: 0s!important;
display:block !important;
}
In this case I used videojs so I have the controls for the video, then I hid the video with css. Feel free to adapt the css to your needs.
Hope that helps.

Cannot change color of html5 video black bars in IE and iOS

I am attempting to display a video in a responsive design such that the scaling borders blend into the background.
I allow the dimensions of the video element to vary within specified bounds. As a result, when the video playing doesn't fill the actual html element, I get the black padding bars around my video.
Using the css background property I have been able to change the color of the bars shown in Chrome, FireFox, and Opera. I cannot figure out how to change the color shown for Internet Explorer or iOS (ipad).
Can anyone help me out with this?
fiddle as requested: http://jsfiddle.net/swaEe/
html:
<video width="320" height="240" controls>
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
css:
video {
width: 500px;
background: blue;
}
***_ edit _***
This is a better fiddle: http://jsfiddle.net/swaEe/40/
The video playback should stay vertically and horizontally centered in the container. I want the "bars" to be transparent or the same color as the container (red in this case...).
<div style="width:200px; height:600px; background-color:red;">
<video width="100%" height="100%" style="background-color:red;" controls>
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
<br />
<div style="width:600px; height:200px; background-color:red;">
<video width="100%" height="100%" style="background-color:red;" controls>
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
I think I've managed to come up with a solution:
The problem is that it seems to be impossible to style these letterboxes cross-browser. So the trick then would be not to have letterboxes, by scaling the video element to the aspect ratio of the video file.
This solution has two potential drawbacks:
it requires Javascript
you need to know the dimensions of the video file and write them into data-attributes
<video data-video-width="320" data-video-height="240" controls>
The reason for this is that the browser does not know the dimensions of the video file until it has started loading it. Most browsers do load a few bytes of the video before it is played, but not all - some older versions of Android wait until the user starts playing the video.
If you do not care about Android 2.3, waiting for the loadedmetadata event to get videoWidth and videoHeight as jaicabs answer does it is the right way.
Take a look at this: run fiddle / fiddle editor
We basically do three things:
calculate the aspect ratio of the video
resize it so that it fits snugly into its container
center it horizontally and vertically within the container
You can now simply set the background-color of the container, and you're done.
I've tested it with iOS 7 on iPhone and iPad, Chrome, Firefox and Safari. No IE testing so far, since I currently don't have my virtual machines handy, but I foresee no problems here for the current IEs.
How about div with css as background? [I'm not familiar with iOS, tasted on IE11]
html:
<div id="container">
<video width="320" height="240" controls>
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
CSS:
video {
display: block;
margin-left: auto;
margin-right: auto;
}
#container{
width: 500px;
height: 240px;
background: blue;
}
jsfiddle: http://jsfiddle.net/c9aHf/1/
Solved! Link to the live jsbin: http://jsbin.com/AVOZoXu/9
The edit jsbin to follow the explanation: http://jsbin.com/AVOZoXu/9/edit
I couldn't test it on IE but it should work like a charm.
There is a real problem with iOS. You won't be able to set a background color to the player and the default player size is 150x300 as you can see in Safari Developer Library:
Because the native dimensions of a video are not known until the movie
metadata loads, a default height and width of 150 x 300 is allocated
on devices running iOS if the height or width is not specified.
Currently, the default height and width do not change when the movie
loads, [...]
So, what you have to do to remove the black bars is do change the default size and adapt it to the movie size as soon as you can. And yes, we'll need JavaScript.
// set height and width to native values
function naturalSize() {
var myVideo = document.getElementById('theVideo');
var myContent = document.getElementById('content');
myVideo.height = myVideo.videoHeight;
myVideo.width = myVideo.videoWidth;
//if the video is bigger than the container it'll fit
ratio = myVideo.videoWidth/myVideo.videoHeight;
if(parseInt(myContent.offsetWidth,10)<myVideo.videoWidth){
myVideo.height = myVideo.videoHeight*parseInt(myContent.offsetWidth,10)/myVideo.videoWidth;
myVideo.width=parseInt(myContent.offsetWidth,10);
}
}
// register listener function on metadata load
function myAddListener(){
var myVideo = document.getElementById('theVideo');
myVideo.addEventListener('loadedmetadata', naturalSize, false);
}
window.onload = myAddListener();
And now you'll get a video player size of the video as soon as the meta data loads.
Since the video doesn't have its black bars anymore, I just had to center it as text.
Oh! And you wanted it to be responsive? Check it out, it doesn't matter the width you set to the #content because naturalSize() checks the ratio and the container's width and sets a smaller height for the video than the original, preventing the black bars appearing in original video height with a smaller width.
The width is controlled with the max-width:100%; property so there's no need to change it manually.
#content{
background:blue;
width:50%;
height:auto;
text-align:center;
}
video {
max-width:100%;
vertical-align:top;
}
I know, I know, the video doesn't get resized till you have started playing it, but it's the closest you're gonna get on iOS to do what you want. Anyway, I think it's a great solution, I hope it helps you.
I know some time has already passed since the question was asked, but I also had to implement a workaround for this problem and would like to share.
The problem was similar to OP's, in that the video could be any size or aspect ratio.
If the HTML <video> element is contained within a <div> which specifies no size at all, the container will automatically fit itself around the video and it will have no black "padding".
Knowing this, we can take advantage of the loadedmetadata event: we don't actually need the video's dimensions for any calculations, but we must wait for this data to load so that the container will resize. As soon as that happens, we can adjust the container's position horizontally and/or vertically.
Here's a fiddle tested in IE11:
http://jsfiddle.net/j6Lnz31y/
Source (in case the fiddle ever becomes unavailable):
<div class="whatever-container" style="width:200px; height:600px; background-color:red;">
<div class="video-container">
<video controls>
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
</div>
<br />
<div class="whatever-container" style="width:600px; height:200px; background-color:red;">
<div class="video-container">
<video controls>
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
</div>
.whatever-container {
position: relative;
}
.video-container {
position: absolute;
opacity: 0;
}
.video-container > video {
position: relative;
width: 100%;
height: 100%;
}
/*
* After the video dimentions have been acquired, its container will have
* resized and we can adjust its position as necessary.
*/
function adjustVideoContainer() {
console.log("video metadata loaded");
var videoContainer = $(this).parent().filter(".video-container");
if (videoContainer.length === 0) {
//abort
console.log(
"adjustVideoContainer() was called but no it wasn't "+
"wrapped in an appropriate container"
);
return;
}
var containerParent = videoContainer.parent();
var parentWidth = containerParent.width(),
parentHeight = containerParent.height(),
containerWidth = videoContainer.width(),
containerHeight = videoContainer.height();
if (containerWidth < parentWidth) {
videoContainer.css(
"left",
(parentWidth - containerWidth) / 2
);
}
if (containerHeight < parentHeight) {
videoContainer.css(
"top",
(parentHeight - containerHeight) / 2
);
}
else {
videoContainer.height("100%");
}
videoContainer.css("opacity", 1);
}
$("video").on("loadedmetadata", adjustVideoContainer);
remove the inline width/height attributes. You want to use CSS to control your layout!
Use the magic keyword 'auto' for your height
Make sure to also use a poster with the same aspect ratio of your video
Here is a fiddle: http://jsfiddle.net/swaEe/13/
video {
width: 500px;
min-width: 200px;
max-width: 100%;
height: auto;
}
Solved with no JS:
<div style="display: table-cell; vertical-align: middle; width:200px; height:600px; background-color:red;">
<video width="100%" style="background-color:red;" controls>
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
<br />
<div style="width:600px; height:200px; background-color:red;">
<video height="100%" style="background-color:red; display: block; width: auto; margin: 0 auto;" controls>
<source src="http://www.w3schools.com/html/movie.mp4" type="video/mp4">
<source src="http://www.w3schools.com/html/movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
</div>
Basically, in both cases removing the height or width setting for the video (for the thin div, I removed the height, and for the short one, the width). Then centered the video elements (horizontal with display:block and then the margin trick, vertical with display:table-cell, and there's probably a better way to do that one).