Showing posts with label HTML5. Show all posts
Showing posts with label HTML5. Show all posts

Tuesday, October 15, 2013

How to deploy a HTML5 project on Google App Engine (http://appspot.com)

How to deploy a HTML5 project on Google App Engine (appspot.com)

Step-by-step tutorial showing how to use appspot.com to deploy and host your HTML5 projects.

Steps

  1. Signup to Google App Engine
  2. Download, install, and setup Eclipse IDE
  3. Deploy your application
Before we get started, let me give you a few features that makes Google App Engine a really good alternative to you host your projects.

App Engine applications are easy to build, easy to maintain, and easy to scale as your traffic and data storage needs grow. With App Engine, there are no servers to maintain: You just upload your application, and it's ready to serve your users.

With App Engine, you only pay for what you use. There are no set-up costs and no recurring fees. The resources your application uses, such as storage and bandwidth, are measured by the gigabyte, and billed at competitive rates. You control the maximum amounts of resources your app can consume, so it always stays within your budget.

App Engine costs nothing to get started. All applications can use up to 1 GB of storage and enough CPU and bandwidth to support an efficient app serving around 5 million page views a month, absolutely free.
  • Run your web applications on Google's infrastructure.
  • Google App Engine is fully-integrated development environment
  • You can serve your applications with your own domain (such as http://example.com), or you can use the App Engine domain for free (just like http://emersonestrella.appspot.com)
  • You can use server side languages such as PHP, Java, Python, and Go.
  • For storage on Google App Engine you can use the App Engine

1. Signup to Google App Engine (Appspot)

Go to http://appengine.google.com and signup to App Engine with your Google Account. After that you can create your application on Google App Engine using the Admin Console. Just click the button "Create Application"...


Fill up the form...

2. Download, install, and setup Eclipse

Go to http://www.eclipse.org/downloads/ and download the lasted version "Eclipse IDE for Java EE Developers"...

Then extract and execute the binary.

Next step is to configure Google Plug-in for Eclipse. Open the Eclipse IDE and go to "Help" -> "Install new software"...

Click in the "Add..." button to add a new update site and then fill the field location with one of the following urls based on the version of Eclipse you have installed:

After you add the new update site you can go back to "Help" -> "Install new software" and select the Google Plugin Update Site from the list...

... a list of available plugins and tools will be populated. Choose the "Google Plugin for Eclipse" and click "Next >". After that restart Eclipse and the Google Plugin will be available.


3. Deploy your application

Create a new project on Eclipse to "hold" your application. To do that go to "File" -> "New Project" -> "Google" -> "Web Application Project"...

Add the project files under the folder "war" that is inside your project folder on Eclipse Workspace.

Before deploy your application to Google App Engine you have to link your Eclipse project to the application that you have created using the App Engine Console (Step #1). To do that you have to edit two little things in the file: "../war/WEB-INF/appengine-web.xml"

  • Add the same name of the application you have created on App Engine Console between the application tag...
    • <application>my-application</application>
  • Add the application version number between the version tag...
    • <version>0.1</version>
Now you're ready to deploy your application. Sign in with your Google Account by clicking in the bottom right button "Sign in to Google"...


And finally, click on the Google button in the menu bar and choose the option: "Deploy to App Engine"...

After that the upload process will start and in a few seconds your application will be available on Google App Engine.


That is all. Hope that help you guys who are wondering how to use Google App Engine. If you find any problem during this process, please leave a comment. Thanks!

Sunday, March 3, 2013

How to grab YouTube playback video files

How to grab YouTube playback video files

Step-by-step tutorial showing how to get all available video source files from a video on YouTube using only Javascript. No plugins and/or server side scripts are required.

First of all I want to list here a few benefits from being able to do that.

  1. You can download any video from YouTube without plugins or server-side scripts. You just need a browser.
  2. You can use YouTube as an encoder. Let's say you have to convert a FLV video to MP4 or
    WebM.
  3. You can set a cron job to run once a week to download all available video files from any channel.
  4. You can play any YouTube video out of the YouTube player (not recommend).
  5. You can play YouTube videos on HTML5 canvas and go crazy! Check this out at: http://videopuzzle.possum-cms.com

How to do it



Step 1: Extract YouTube video ID from URL

  var url = 'http://www.youtube.com/watch?v=yntfUjenls0';  
  var video_id = '';  
  var regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;  
  var match = url.match(regExp);  
  if(match){  
   video_id = match[1];  
  }  

Step 2: Request YouTube playback files information

Easy as pie. Just make an Ajax request to: http://www.youtube.com/get_video_info sending the desired video ID...
  $.ajax({  
   url: "http://www.youtube.com/get_video_info?video_id="+video_id,  
   dataType: "text"  
  }).done(function(data) {  
   ...  
  });  


Step 3: Parse the response data information from YouTube

The information that we are looking for is "url_encoded_fmt_stream_map". This variable contains all streams available for the requested movie.

 function(data) {  
  var info = {};  
  parse_str(data, info);  
  var streams = explode(',', info['url_encoded_fmt_stream_map']);  
  ...  


Step 4: Trick. Parse again each one of the streams

YouTube encode the information twice, so you have to parse it again.
There is other trick here, You also have to add a signature value on each stream url, otherwise it won't work.

  var results = [];  
  for(var i=0; i<streams.length; i++){  
   var real_stream = {};  
   parse_str(streams[i], real_stream);  
   real_stream['url'] += '&signature=' + real_stream['sig'];  
   results.push(real_stream);  
  }  



Step 5: Have fun!


By now you should have a variable "results" with all YouTube video files available for the requested movie.

You can do whatever you want with the files. For example: display a link, play the video, grab snap shots of the video... Have fun!

To display the a link for each video file available...

   if(results.length > 0){  
    $.each(results, function(index, value) {  
     $('#results').append('<a href="'+value.url+'">'+value.quality+' - '+value.type</a>');  
    });  
   }  

Thursday, October 18, 2012

How to play a video on HTML5 canvas tag


How to play a video on HTML5 canvas tag

Step-by-step tutorial on how to play a video on HTML5 canvas tag



First you need a video file. Actually, two. One video file in ogg format (for Chrome and Firefox) and the other one in mp4 format (for Safari). To get any video in these file formats you can use a website like www.online-convert.com or www.mediaconverter.org.

Once you have the video files, the next step is to create a simple HTML page with one canvas tag, and one video tag. Just like the code below.

 <!DOCTYPE html>  
 <head>  
 <title>Playing YouTube video on HTML5 canvas</title>  
 </head>  
 <body>  
  <video id="video" autoplay="true" loop="true">  
   <source src="./video/BigBuckBunny_640x360.ogv" type="video/ogg" />  
   <source src="./video/BigBuckBunny_640x360.mp4" type="video/mp4" />  
  </video>  
  <canvas id="canvas"></canvas>  
 </body>  
 </html>  

Right now, you should be able to test the video and check if it is playing well on Chrome Safari and Firefox.

Next, we add some CSS and a new hidden div wrapping the video tag.

 <!DOCTYPE html>  
 <head>  
 <title>Playing YouTube video on HTML5 canvas</title>  
 </head>  
 <body>  
  <video id="video" autoplay="true" loop="true">  
   <source src="./video/BigBuckBunny_640x360.ogv" type="video/ogg" />  
   <source src="./video/BigBuckBunny_640x360.mp4" type="video/mp4" />  
  </video>  
  <canvas id="canvas"></canvas>  
 </body>  
 </html>  

Since we will be rendering the video on canvas, we need to make the video hidden. But we will be using the audio from the video tag.

Now the only missing piece is the javascript that renders the video on canvas. To do this we need to grab a image from the video and then paint this image on canvas. If we repeat this process every X milliseconds... TA-DA! We have the video playing on canvas.

Here is the full source code:

 <!DOCTYPE html>  
 <head>  
 <title>Playing YouTube video on HTML5 canvas</title>  
 <meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, width=device-width" />  
 <style type="text/css">  
  html, body {  
   width: 100%;  
   height: 100%;  
   padding: 0px;  
   margin: 0px;  
  }  
  #canvas {  
   padding: 0px;  
   margin: 0px;  
   top:0;  
   left:0;  
   z-index: 30;  
   position: absolute;  
   width: 100%;  
   height: 100%;  
  }  
 </style>  
 </head>  
 <body>  
  <div style="display: none;">  
   <video id="video" autoplay="true" loop="true">  
    <source src="./video/BigBuckBunny_640x360.ogv" type="video/ogg" />  
    <source src="./video/BigBuckBunny_640x360.mp4" type="video/mp4" />  
   </video>  
  </div>  
  <canvas id="canvas"></canvas>  
  <script>  
  document.addEventListener('DOMContentLoaded', function(){  
   var v = document.getElementById('video');  
   var canvas = document.getElementById('canvas');  
   var context = canvas.getContext('2d');  
   //var cw = Math.floor(canvas.clientWidth / 100);  
   //var ch = Math.floor(canvas.clientHeight / 100);  
   var cw = Math.floor(canvas.clientWidth);  
   var ch = Math.floor(canvas.clientHeight);  
   canvas.width = cw;  
   canvas.height = ch;  
   v.addEventListener('play', function(){  
    draw(this,context,cw,ch);  
   },false);  
  },false);  
  function draw(v,c,w,h) {  
   if(v.paused || v.ended) return false;  
   c.drawImage(v,0,0,w,h);  
   setTimeout(draw,20,v,c,w,h);  
  }  
  </script>  
 </body>  
 </html>  


If you want to play a video from YouTube, check this tutorial: Playing YouTube videos on HTML5 canvas with Javascript.

If you want to download a video from YouTube, check this one: Download YouTube video files with Javascript

Next, I'll show you how to make a simple HTML5 game puzzle. Stay tuned!


Wednesday, October 17, 2012

Playing YouTube videos on HTML5 canvas with Javascript

Playing YouTube videos on HTML5 canvas with Javascript

How to play YouTube videos on HTML5 canvas tag

This blog post is coming soon. Stay tuned!

Saturday, October 13, 2012

Step-by-step creating a HTML5 image puzzle game

Step-by-step creating a HTML5 image puzzle game

How to create a HTML5 image puzzle game with images, canvas, audio and fullscreen support.

Tutorial coming soon 

Saturday, April 28, 2012

HTML5 AppCache API - How to prevent browser caching unsupported resources in manifest file

The method describe below doesn't work in all browsers.  
For now I'm using two different html files. But the best solution is on server side, making a rewrite rule for redirecting the user to the correct manifest file.


I'm writing a web application that uses the AppCache API for offline browsing. But I'm also using the Audio API to play back-ground music and a few audio effects.

For audio support in different browsers I'm delivering each sound/music in two different file formats: OGG and MP3.

The problem is regarding the cache manifest file. If we add all audio files (MP3 and OGG) in the cache manifest file, all browsers will cache all files. Including the unsupported ones. So, we end up with a huge storage requirement. Which is really bad if you are on a 3G connection for example.

So, to prevent browser caching unsupported resources, the best approach I've found was to split the manifest file. This way we can tell browsers that sopport OGG files to cache only OGG files, and do tha same for other formats like MP3.

Ok then. I have two manifest cache files, one with all OGG files listed (ogg.appcache) and the other one with the MP3 files (mp3.appcache).

Now what!? How do I swap from one to other?

Easy. First we should consider the browser supports OGG. If so, we can add the manifest attribute into the html tag with the right manifest file, just like that:


<html manifest="ogg.appcache">

Now, we have to check if the browser supports MP3. If it does, we change the value of the attribute "manifest" from the html tag. We can do that with this javascript:

var audio = document.createElement('audio');
if(audio.canPlayType('audio/mpeg;'))
  document.documentElement.setAttribute("manifest"document.documentElement.getAttribute("manifest").replace('ogg','mp3'));



Monday, April 23, 2012

HTML5 Polyfills - Fullscreen & RequestAnimationFrame APIs

So far, those are the best polyfills for using Fullscreen and RequestAnimationFrame features from HTML5 on today's browsers.


RequestAnimationFrame API

Optimize concurrent animations together into a single reflow and repaint cycle, leading to higher fidelity animation, and using less CPU, GPU, memory, and battery.


http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating

Fullscreen API

Provides an easy way for web content to be presented using the user's entire screen.


https://github.com/sindresorhus/screenfull.js