views:

77

answers:

2

Hi,

I want to capture the first frame of an embedded video as an image without using any server side scripting. Probably with javascript, is it possible?

A: 

simple answer: no.

pstanton
wow html 5 and video objects eh? not sure a downvote is necessary when the question doesn't state it can use html5 which is clearly html(4).
pstanton
+3  A: 

Actually, pretty sure you can, using HTML5. Take a look at this link: HTML5 Video Destruction.

It's copying the video frame into another canvas every 33ms. You could play around with this and see if you can capture the first frame when the video starts running.

I can look into this further if you like (it fascinates me)

EDIT: oh my GOD THIS IS COOL. I just came up with a solution. Go to sambro.is-super-awesome.com/videofirstframe/

You need to open this in Google Chrome. Firefox doesn't support mp4 (I think).

First time I've ever done anything with HTML5, I CANNOT wait until this is in the majority of browsers :)

EDIT EDIT: Okay I uploaded the .ogg version of this video too, and setup my web server to handle the video type correctly, Firefox should work in this little example too.

EDIT EDIT EDIT: Nitpickers wanting source up here, well here it is:

// Create a video element.
var vid = document.createElement("video");

// We don't want it to start playing yet.
vid.autoplay = false;
vid.loop = false;

// No need for user to see the video itself.
vid.style.display = "none";

// This will fire when there's some data loaded for the video, should be at least 1 frame here.
vid.addEventListener("loadeddata", function()
{
    // Let's wait another 100ms just in case?
    setTimeout(function()
    {
        // Create a canvas element, this is what user sees.
        var canvas = document.createElement("canvas");

        // Set it to same dimensions as video.
        canvas.width = vid.videoWidth;
        canvas.height = vid.videoHeight;

        // Put it on page.
        document.getElementById("done").innerHTML = "";
        document.getElementById("done").appendChild(canvas);

        // Get the drawing context for canvas.
        var ctx = canvas.getContext("2d");

        // Draw the current frame of video onto canvas.
        ctx.drawImage(vid, 0, 0);

        // Done!
    });
}, false);

// Have to include .ogv for firefox. I don't think this is working atm because my webserver isn't serving up
// videos properly.
if(BrowserDetect.browser == "Firefox")
{
var source = document.createElement("source");
source.src = "BigBuckBunny_640x360.ogv";
source.type = "video/ogg";
vid.appendChild(source);
}
else
{
var source = document.createElement("source");
source.src = "BigBuckBunny_640x360.mp4";
source.type = "video/mp4";
vid.appendChild(source);
}

// Add video to document to start loading process now.
document.body.appendChild(vid);
Sam Day
hari
@hari: Note that this will require a very up-to-date browser.
T.J. Crowder
@Sam Day: **Wow** that's cool.
T.J. Crowder
No problem Crowder......I just want this to work in this scenario even if older browsers dosn't support
hari
I have attached a demonstration of how to do this, check out my updated answer.
Sam Day
hari
hari, take a look at the code in index.html to see how I did this. As far as doing this to a youtube video, it might be possible, I'm not sure if there's any way to get a direct link to HTML5 encoded videos from YouTube yet though. The iframe embedding would not be adequate.
Sam Day
Nitpicking a little, but scripts like Modernizr would definitely do better if you're trying to detect which video format to serve. Why include jQuery if you're not using it for anything? Also, you should include the code here so that we don't have to go over to your site just to see the code.
Yi Jiang
@Yi Jiang: Everything you said is fair enough, you need to consider the following though: I was excited and wanted to get a demo running ASAP. I used jQuery for proper dom loaded (why not?), I've never worked with HTML5 video or Canvas at all, and this was just supposed to be a quick little proof of concept :)
Sam Day
@Yi Jiang: Also thanks for the headsup with Modernizr, never seen that tool and it looks incredibly useful!
Sam Day
Hi sam, can this be done with embed tag instead of video tag
hari
@hari: this will only work with video tag!
Sam Day
hari
@hari: There's not really any way this is going to work for embed or object tags unfortunately, as these are usually Flash videos and there's not really any reliable way to extract what video is playing from a container (not from JS anyway). Even if you did come up with a way to do that, the video needs to be inside a HTML5 <video> tag, and that will only work with h264/ogg/webM videos (support varies by browser)
Sam Day