Table of Contents for
The Modern Web

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition The Modern Web by Peter Gasston Published by No Starch Press, 2013
  1. The Modern Web
  2. Cover
  3. The Modern Web
  4. Advance Praise for
  5. Praise for Peter Gasston’s
  6. Dedication
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Introduction
  11. The Device Landscape
  12. The Multi-screen World
  13. Context: What We Don’t Know
  14. What You’ll Learn
  15. A. Further Reading
  16. 1. The Web Platform
  17. A Quick Note About Terminology
  18. Who You Are and What You Need to Know
  19. Getting Our Terms Straight
  20. The Real HTML5
  21. CSS3 and Beyond
  22. Browser Support
  23. Test and Test and Test Some More
  24. Summary
  25. B. Further Reading
  26. 2. Structure and Semantics
  27. New Elements in HTML5
  28. WAI-ARIA
  29. The Importance of Semantic Markup
  30. Microformats
  31. RDFa
  32. Microdata
  33. Data Attributes
  34. Web Components: The Future of Markup?
  35. Summary
  36. C. Further Reading
  37. 3. Device-Responsive CSS
  38. Media Queries
  39. Media Queries in JavaScript
  40. Adaptive vs. Responsive Web Design
  41. Viewport-Relative Length Units
  42. Responsive Design and Replaced Objects
  43. Summary
  44. D. Further Reading
  45. 4. New Approaches to CSS Layouts
  46. Multi-columns
  47. Flexbox
  48. Grid Layout
  49. The Further Future
  50. Summary
  51. E. Further Reading
  52. 5. Modern JavaScript
  53. New in JavaScript
  54. JavaScript Libraries
  55. Polyfills and Shims
  56. Testing and Debugging
  57. Summary
  58. F. Further Reading
  59. 6. Device Apis
  60. Geolocation
  61. Orientation
  62. Fullscreen
  63. Vibration
  64. Battery Status
  65. Network Information
  66. Camera and Microphone
  67. Web Storage
  68. Drag and Drop
  69. Interacting with Files
  70. Mozilla’s Firefox OS and WebAPIs
  71. PhoneGap and Native Wrappers
  72. Summary
  73. G. Further Reading
  74. 7. Images and Graphics
  75. Comparing Vectors and Bitmaps
  76. Scalable Vector Graphics
  77. The canvas Element
  78. When to Choose SVG or Canvas
  79. Summary
  80. H. Further Reading
  81. 8. New Forms
  82. New Input Types
  83. New Attributes
  84. Datalists
  85. On-Screen Controls and Widgets
  86. Displaying Information to the User
  87. Client-side Form Validation
  88. The Constraint Validation API
  89. Forms and CSS
  90. Summary
  91. I. Further Reading
  92. 9. Multimedia
  93. The Media Elements
  94. Media Fragments
  95. The Media API
  96. Media Events
  97. Advanced Media Interaction
  98. Summary
  99. J. Further Reading
  100. 10. Web Apps
  101. Web Apps
  102. Hybrid Apps
  103. TV Apps
  104. Webinos
  105. Application Cache
  106. Summary
  107. K. Further Reading
  108. 11. The Future
  109. Web Components
  110. The Future of CSS
  111. Summary
  112. L. Further Reading
  113. M. Browser Support as of March 2013
  114. The Browsers in Question
  115. Enabling Experimental Features
  116. Chapter 1: The Web Platform
  117. Chapter 2: Structure and Semantics
  118. Chapter 3: Device-Responsive CSS
  119. Chapter 4: New Approaches to CSS Layouts
  120. Chapter 5: Modern JavaScript
  121. Chapter 6: Device APIs
  122. Chapter 7: Images and Graphics
  123. Chapter 8: New Forms
  124. Chapter 9: Multimedia
  125. Chapter 10: Web Apps
  126. Chapter 11: The Future
  127. N. Further Reading
  128. Introduction
  129. Chapter 1: The Web Platform
  130. Chapter 2: Structure and Semantics
  131. Chapter 3: Device-Responsive CSS
  132. Chapter 4: New Approaches to CSS Layouts
  133. Chapter 5: Modern JavaScript
  134. Chapter 6: Device APIs
  135. Chapter 7: Images and Graphics
  136. Chapter 8: New Forms
  137. Chapter 9: Multimedia
  138. Chapter 10: Web Apps
  139. Chapter 11: The Future
  140. Index
  141. About the Author
  142. Copyright

The Media Elements

To play either audio or video files, you need to use the audio or video element, respectively. They are similar, sharing the same attributes and child elements, with the type of media they deliver being the key difference. At their most simple, each element requires only a single attribute, src, which is the path to the media file to be played:

<audio src="foo.oga"></audio>
<video src="foo.ogv"></video>

What you see depends on the element: with video, you see the video file at its natural dimensions with the first frame showing; with audio, you see nothing. In either case, you can’t play the file because there are no on-screen controls. You add them using the controls attribute:

<audio src="foo.oga" controls></audio>
<video src="foo.ogv" controls></video>

Now you’ll see on-screen controls for each of the media elements, as illustrated in Figure 9-1, and you can take a look for yourself in the file media-elements.html. I suggest you view this example file in Chrome, Firefox, or Opera, for reasons that will become clear in this chapter.

Native controls for the video (top) and audio (bottom) elements on Chrome for Ubuntu
Figure 9-1. Native controls for the video (top) and audio (bottom) elements on Chrome for Ubuntu

Using these attributes, a media file plays only at the user’s request and only once; if you prefer, you could have the file play as soon as it has loaded by using the autoplay attribute and make it loop indefinitely (until the user pauses it or leaves the page) by using the loop attribute; both are Boolean, so they require no value:

<audio src="foo.oga" autoplay loop></audio>

Warning

Autoplayed sound has accessibility drawbacks; see the WCAG Audio Control advice, listed in Appendix J, for more information on how best to cater to everyone.

On some mobile devices, media files won’t play where they’re embedded in the page; instead, they’re launched and played in the device’s own media player framework. If this happens, the autoplay attribute is ignored.

If you do autoplay or loop a file, keep in mind that the sound can be annoying or confusing to some users and consider muting the volume by default using the Boolean muted attribute:

<video src="foo.ogv" autoplay muted></video>

The full media file isn’t normally downloaded when the page loads, but only when playback is requested; instead, metadata about the file—length, file size, and so on—is loaded into memory (a key exception to this is when the autoplay attribute is applied, which requires the full file to be downloaded). If you prefer to change this behavior, you can use the preload attribute, which has three values: metadata is the default and behaves as mentioned; auto indicates to the browser that it should download and cache the file because the user will probably play it; and none means the file probably won’t be used, so don’t download anything.

<video src="foo.ogv" preload="auto"></video>

Note that these are only hints, not commands; the browser makes the final decision about when to download media files based on variables such as the current network and the device that is running the browser. Mobile browsers will likely ignore the auto value, as downloading a potentially huge video when the user has metered bandwidth is a real no-no. In many cases, metadata is the sole acceptable value.

Extra Attributes for the video Element

Although the audio and video elements share many attributes, the latter, by virtue of it having more of a presence in page layout, also has several extra attributes. Video files will, by default, be displayed on the page at the dimensions in which they were coded. You can change this using the height and width attributes:

<video src="foo.ogv" height="360" width="240"></video>

Note that these attributes set the size of the video element, not the video itself, so if the values you supply aren’t in the same ratio as the file, the video won’t be stretched to fill the space; it maintains its aspect ratio and position in the center of the element, as you can see in video-dimensions.html (and in Figure 9-2). If you prefer to change this behavior, use the object-fit and object-position properties from Chapter 3.

The video file won’t lose its aspect ratio when the video element is resized.
Figure 9-2. The video file won’t lose its aspect ratio when the video element is resized.

The video element is blank or uses a platform-specific graphic by default, until the first frame of the video has loaded, at which point that first frame is displayed as a still image. If you prefer a different still or image entirely, you can specify your own with the poster attribute:

<video src="foo.ogv" poster="foo.png"></video>

Multiple Source Files

In the examples I’ve shown so far, I’m using the .ogv extension for video files and .oga for audio files, which are extensions of the Ogg format. Ogg is a container format, which means it can hold different video and audio formats within it; for Ogg video, the most common format is Theora, and for audio, Vorbis.

Both Theora and Vorbis are free formats—that is, no licensing fees are involved in using them. This fact would seem to make them ideal for Web use, and indeed, some sites like Wikimedia do encode their media with them. They’re not supported in many browsers, however, for reasons I explain in Format Wars. So what can you do?

Both audio and video elements have provisions for specifying multiple source files. You leave out the src attribute and use source child elements instead. In the following example, I set two different source files for a video element: The first is an .ogv file; the second, a .webm file; and the third, an .mp4 file. The browser plays the first video format that it supports.

<video>
  <source src="foo.ogv" type="video/ogg"></source>
  <source src="foo.webm" type="video/webm"></source>
  <source src="foo.mp4" type="video/mp4"></source>
</video>

The example file media-elements-sources.html demonstrates this feature; the file appears to be the same as media-elements.html, but unlike that first example, it shouldn’t matter which browser you use to open this file. As long as the browser supports HTML5 video, one of the provided source files should play.

Notice that I’ve included the type attribute, which contains the MIME type of the different formats and lets the browser know which type of file it should play without having to access the file itself. This attribute isn’t required, but including it is considered good practice (although some older browsers used to have trouble with the type attribute). As always, test to see if it causes any problems.

The source element has a media attribute, which takes a media query as a value (see Chapter 3), meaning the specified file is used only if the query returns true—handy for supplying different files to different devices. Most likely you won’t supply a 1920×1080 video file to a 480×320 screen, or vice versa. The following code example sets a different source file, foo-hd.ogv, if the screen is at least 1920px wide and has an aspect ratio of 16/9:

<video>
  <source src="foo-hd.ogv" media="(device-aspect-ratio: 16/9) and (min-device-width: 1920px)"></source>
  <source src="foo.ogv"></source>
</video>

The real utility of this attribute has been debated; some feel a better solution is to use JavaScript to adapt the quality of the video, depending on device resolution and network status, and a spec is being proposed to handle this. For the near future, however, the media attribute is the best approach to adaptive video.

Fallbacks

You should provide some kind of fallback if a user’s browser doesn’t support the media elements, so the user doesn’t just see empty space on the page. Anything inside the video element is displayed when the video is not present, so the fallback can be as simple as a line of text that explains the problem or perhaps an image showing a key frame as an alternative.

<video src="foo.ogv">
  <img src="foo.png">
</video>

If you simply must have media available to everyone, you can include a Flash object as fallback; if the user’s browser doesn’t have native audio or video, the plug-in is used instead. And you can put a fallback to the fallback inside the object element to cover all bases.

<video src="foo.ogv" height="240" width="360" poster="foo.png">
  <object width="640" height="360" type="application/x-shockwave-flash" data="foo.swf">
    <param name="movie" value="foo.swf">
    <param name="flashvars" value="controlbar=over&amp;image=foo.png&amp;file=foo.mp4">
    <img src="foo.png" width="360" height="240">
  </object>
</video>

This code is quite lengthy, but it does seem to cover all eventualities. Kroc Camen developed it, and I recommend you read his article “Video for Everybody!” to see all of the notes and caveats that I was unable to include here (see Appendix J).

Subtitles and Captions

You can add optional text tracks to run alongside the media file; these could be subtitles or captions for video files, or associated metadata files for either media type. You can have as many tracks as you like—for example, different languages for subtitles or a caption track and a metadata track—and each is added with a track child element.

This element has a few key attributes: kind tells the browser the purpose of your track—subtitles, captions, and descriptions are among the possible values; src describes the path to the file; srclang is the language of the track; label is the description of the track shown to the user if multiple options are available (if the browser UI supports it); and the Boolean default sets the track to use if more than one is available or ensures that a track is shown even if the user hasn’t explicitly requested one (only the first instance is recognized).

The following code shows how you might offer subtitles in two different languages—English and Brazilian Portuguese—on the video element:

<video src="foo.ogv">
  <track kind="subtitles" src="foo.en.vtt" srclang="en" label="English" default>
  <track kind="subtitles" src="foo.pt.vtt" srclang="pt-br" label="Brazilian Portuguese">
</video>

You can see an example of subtitles in the file video-subtitles.html and in Figure 9-3. If your browser’s native video UI supports it, try changing the language of the subtitles.

Subtitles added using the track element
Figure 9-3. Subtitles added using the track element

Different browsers accept different types of files for subtitles and captions, but the emerging standard is known as WebVTT. This format is simple, displaying captions from a start time to an end time. Here’s an example:

WEBVTT
1
00:00:00.500 --> 00:00:04.000
Hello to all readers
2
00:00:06.000 --> 00:00:10.000
Thanks for reading this book

The first caption appears on screen after half a second and is shown until the fourth second; the second caption appears in the sixth second and is shown until the tenth second. Although extra configuration options are available, the core has been kept very simple.

Encoding

You have many different variables to consider when encoding video for the Web; for example, my experience is that some files encoded for iOS may not play on Android, and vice versa. If you’re happy using the command line, then FFmpeg is your best bet; it’s a free and open source encoder that’s extremely configurable. You need to give it the path to the source video, a series of (optional) encoding parameters, and then a path to the output file. Here’s a simple example, converting from AVI to MP4:

ffmpeg -i foo.avi foo.mp4

If you prefer a GUI, there are many video-encoding tools available, but I rely on two that are easy to use and free. Miro Video Converter is a Mac or Windows program handy for quick conversions; you simply drag a file into the app, choose an output mode from a list that includes MP4, Ogg Theora, and WebM, and the tool outputs the converted file for you. If you prefer to have more options to control your conversions, HandBrake is probably the easiest and most powerful tool, although it outputs only MP4 so you may need to convert to Miro anyway. Both these tools are graphical shells for FFmpeg.