3

I'm having a problem finding if this is even possible (no info anywhere to be found). Is it possible to EXTRACT a frame (thumbnail) during the video upload? Extract using jscript is also an option if it's possible to extract user side.

Thanks for the help!

2
  • It will not be possible client-side, but if you have uploaded a video to Youtube it is possible somehow.
    – Amberlamps
    Commented Jun 29, 2012 at 12:09
  • @Amberlamps, its possible client side with html5(you can read binary file contents), although you'd have to write the extract/conversion code from scratch :)
    – goat
    Commented Jun 29, 2012 at 16:39

5 Answers 5

8

Fast forward almost five years since this question was posted, and the answer is now a yes!

Live demo

How to extract a frame during video upload using JavaScript

Before showing code, this is what we'll do:

  1. Set an event handler on the input element that will take the video to be uploaded
  2. When a file is selected from the file system, use the URL object to create a url to the local video file.
  3. Load that URL into a video element.
  4. When the video has loaded into memory, draw a frame on a canvas object.
  5. Now export the frame rendered on the canvas into an image element (or optionally send that image to your server as a data url).

<input type="file" id="upload"/>
<img id="thumbnail"/>

<script>
var input = document.getElementById('upload');
var img = document.getElementById('thumbnail');

input.addEventListener('change', function(event){
    var file = this.files[0];
    var url = URL.createObjectURL(file);

    var video = document.createElement('video');
    video.src = url;

    var snapshot = function(){
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');

        ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
        img.src = canvas.toDataURL('image/png');

        video.removeEventListener('canplay', snapshot);
    };

    video.addEventListener('canplay', snapshot);
});
</script>
0

I don't think there is a good way to do that in php. (https://stackoverflow.com/a/1309425/1488032)

If it is not just some webspace that you have rented but have the rights to install and run other software on the server I would suggest using something like ffmpeg and calling it from within php using system(), passthru() or exec() to extract what you want and do the rest of the processing in php again.

I'm running a browser game that relies on svg images embedded in xhtml but some browsers (especially mobile ones) don't support the full svg syntax. In this case I use such a system() call to do svg-to-png conversion using imagemagick's convert binary and display the result on my page.

system("echo '$svgString' | convert svg:- png:- | base64");

I suppose you have to fall back to something similar.

Here's some information on extracting frames using ffmpeg: http://ubuntuforums.org/showthread.php?t=1141293

0

Javascript: no.

During upload: no.

Once you've uploaded, yes.

"Dev" provided the right link in the comments, but what you should do is save the video to your server and then run ffMpeg to grab the image. You can download ffMPEG here: http://ffmpeg.org/download.html (grab hte build you need if you're not confident on building it yourself - there are Linux and Windows builds).

The documentation is here: http://ffmpeg.org/ffmpeg.html but there is a slightly easier to read tutorial at http://linuxers.org/tutorial/how-extract-images-video-using-ffmpeg for grabbing an image.

Note: there is a PHP extension clled "phpFFMPEG" but I suggest you don't use it. Simply run the desired commands through "exec()" in PHP. Check for error return values as you can only run ffMPEG once per CPU core, so if you try it twice at the same time it might fail - either queue the actions or try again if it fails.

3
  • You have preview thumbnails during the upload on Youtube. How do they do it, if it is not possible according to you?
    – Amberlamps
    Commented Jun 29, 2012 at 17:54
  • Not having used You Tube, I can't answer definitively, but I presume this will be using flash. Flash is certainly an option.
    – Robbie
    Commented Jun 29, 2012 at 18:25
  • Found the answer: Silverlight for IE, and using gears for Chrome. code.google.com/p/gears/wiki/ResumableHttpRequestsProposal, neither of which are a standard. But definitely an option if you want the challenge. Flash and Java could do it too.
    – Robbie
    Commented Jun 29, 2012 at 18:35
0

First, you need to select a program to extract the frame. ffmpeg is commonly used for this. Whatever you pick, it needs to be able to work with partial file contents.

php scripts dont start executing until after the entire file upload has completed, but php recently got a feature so that a different php script can be executed during the file upload, and will be able to get at uploading scripts data(the filename is the thing of interest to you). http://php.net/manual/en/session.upload-progress.php

Then, basically call the external program to extract the frame from the monitoring script, using the temp file name being uploaded in the upload handling script.

to summarize: upload the file to upload.php. monitor.php will get the temp file name being uploaded in upload.php, and extract the frame.

0

Basically, I took rodrigo-silveira answer provided in this thread and modified it for my use and now the solution works like a charm. Even I was trying to upload the video thumbnail/poster of a video that a user wishes to upload and save the video and the thumbnail in a folder. Also, I didn't want to use ffmpeg.

Here is what I did: In the upload file called "upload.php" I have the following code with slight modification to rodrigo-silveira's solution above:

upload.php:

<input type="file" id="upload"/>
<img id="thumbnail"/>

<form action="action_page.php" method="post" target="_blank">
<input type="hidden" id="mytext" name="mytext" >
<input type="submit" value="Submit">
</form>

<script>
var input = document.getElementById('upload');
var img = document.getElementById('thumbnail');

input.addEventListener('change', function(event){
    var file = this.files[0];
    var url = URL.createObjectURL(file);

    var video = document.createElement('video');
    video.src = url;

    var snapshot = function(){
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
		canvas.width = 350;
		canvas.height = 250;
        ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
        img.src = canvas.toDataURL('image/png');
		document.getElementById("mytext").value = img.src;
        video.removeEventListener('canplay', snapshot);
    };
	
    video.addEventListener('canplay', snapshot);
});
</script>

Both the HTML part and the JavaScript above are within the upload.php's body tag.

Now on to the action_page.php file:

<?php
$data = $_POST['mytext'];
$file = "photos/file".time().".png";
$uri = substr($data,strpos($data, ",") + 1);
file_put_contents($file, base64_decode($uri));
?>

Save both PHP files in the same folder and create another folder called "photos" in that folder. Your video thumbnail/poster image from a video that is selected in the upload.php page gets saved in the "photos" folder as png file. (Note: this solution does not upload the video, just the video thumbnail. But that is straight forward from here on.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.