Created
July 3, 2012 14:11
-
-
Save jamiehodge/3039944 to your computer and use it in GitHub Desktop.
HTML5 video preview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'sinatra' | |
| require 'slim' | |
| require 'coffee_script' | |
| get '/' do | |
| slim :new | |
| end | |
| __END__ | |
| @@new | |
| doctype html | |
| html | |
| head | |
| body | |
| form | |
| p | |
| input type='file' accept='video/*' | |
| .preview | |
| coffee: | |
| document.addEventListener 'DOMContentLoaded', -> | |
| input = document.querySelector 'input' | |
| preview = document.querySelector '.preview' | |
| input.addEventListener 'change', (e) -> | |
| file = e.target.files[0] | |
| URL = window.webkitURL or window.URL | |
| url = URL.createObjectURL file | |
| video = document.createElement 'video' | |
| video.addEventListener 'seeked', (-> | |
| canvas = document.createElement 'canvas' | |
| canvas.width = 320 | |
| canvas.height = 240 | |
| context = canvas.getContext '2d' | |
| context.drawImage @, 0, 0, canvas.width, canvas.height | |
| preview.appendChild canvas | |
| @currentTime += @duration / 10.0 unless @ended | |
| ), false | |
| video.addEventListener 'loadeddata', (-> | |
| preview.innerHTML = '' | |
| @currentTime = 0 | |
| ), false | |
| video.src = url | |
| ), false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment