Using HTML5 video in ePub
by Liza Daly
This was an experiment and is a bit of a hack. Most people seeking to embed video in ePub should use the Flash method described earlier.
I wanted to see if could construct a valid ePub file using HTML5 (in this case, employing the HTML5 <video> element). The problem is that ePub only supports one kind of HTML: XHTML 1.1, a flavor of markup which seems to exist only to annoy people trying to create valid ePub files.
There are two methods to include “foreign” media types in ePub. The first is out-of-line items which have separate fallback items, either out-of-line XML in another vocabulary, or a separate type of binary media like a movie falling back to a static image.
The other is Inline XML islands, which uses a unique <ops:switch> element to toggle among two or more different XML vocabularies.
My first instinct, if I were going to include the HTML5 <video> element, was to make an inline XML island and switch between that and the Flash fallback. But <ops:switch> depends on the XML island having a different namespace. HTML5 uses the standard XHTML namespace (http://www.w3.org/1999/xhtml), so there’s no way to indicate that we’re using an XHTML vocabulary that isn’t XHTML 1.1.
Got that?
But there’s another option: treat the whole HTML5 page as an out-of-line island. How? By not using the XHTML vocabulary at all, but instead treating it as media-type text/html:
<!-- HTML5 -->
<item id="video"
href="video.html"
fallback="video-fallback"
media-type="text/html"/>
<!-- XHTML 1.1 fallback -->
<item id="video-fallback"
href="fallback.html"
media-type="application/xhtml+xml" />
Here I defined a document with an unsupported media-type (text/html) and a fallback in a supported type (application/xhtml+xml). Unfortunately there’s no way for me to actually specify that it’s HTML5, since HTML5 doesn’t even have a real DOCTYPE.
I also include the media files in the OPF:
<item id="video-mp4"
href="20143.mp4"
media-type="video/mpeg4"/>
<item id="video-ogg"
href="20143.ogv"
media-type="video/ogg"/>
<item id="video-png"
href="video.png"
media-type="image/png"/>
And here’s the complete HTML5 island:
<!DOCTYPE html>
<html>
<head>
<title>Video test</title>
</head>
<body>
<p>
<video width="320" height="240" controls="true" autoplay="true">
<source type="video/ogg" src="20143.ogv" />
<source type="video/mp4" src="20143.mp4" />
</video>
</p>
</body>
</html>
Here’s the sample file: HTML5 video in ePub. This will actually work in Bookworm, but nowhere else. It’s also not valid according to epubcheck, but only because epubcheck thinks that the text/html item is an error, and it doesn’t like that the HTML5 island is missing a namespace (because it’s not actually XHTML, but text/html).
Comments
[...] approach. But how about ePub books with videos embedded. Liza Daly, creator of Bookworm, had just shared some tips (via Reading 2.0 list). Digg us. Slashdot us. Facebook us. Twitter us. Share the [...]
Good catch! I think ops:switch also can take required module name in addition to the namespace, but it should be given a specific value for (X)HTML5. I have created an issue in IDPF database, so this can be addressed by the WG in errata.
BTW, XHTML has to be used instead of HTML so that only a standard XML parser is all what is ever needed in a (potentially resource-contrained) reading system. Also most members of the WG have an opinion that rigid syntax rules are better for interoperability. HTML can always be mechanically converted to XHTML at authoring time, so there is no loss of functionality there. Any HTML5 syntax can also be expressed in XML by design.
[...] a recent blog post, Liza conducted an experiment to include a HTML5 <video> in an ePub file, which she accomplished by using out-of-island XML markup. Okay, so this is something of a hack, [...]
Thanks Peter. I didn’t know if I wanted to submit it as an issue since it’s kind of an edge case.
An epub reader has to also have a CSS parser in addition to an XML parser: http://idpf.org/2007/ops/OPS_2.0_final_spec.html#Section1.4.2
I would have liked to see the CSS requirement dropped (for the reason you state), and allow any XHTML schema, but my wishlist for epub is another post.
See Peter’s issue and the resulting comments: Using ops:switch with HTML5.
Actually, you can produce valid epub files using HTML 4.0 strict or transitional standards to get valid epubs too.
Great job Liza, but how about the size of the final epub.
How many videos can you really embed in an epub without “breaking the limits” ? Imagine you want to insert some training videos into a graphist learning epub for student or pro, how many of videos can you reasonnably put in ?
Thanks and congrates again for thoses developpments and communicate on it.
Seb,
You can make epub files as big as you like, according to the spec. The practical limit on OPS file size in the Sony Reader doesn’t apply here since it doesn’t support video anyway.
The main thing I’d be concerned with is ordinary download size. You can get a sense of the range in sizes for epub books, but I’d say that bigger than 5MB is pushing it, and 10MB will be too much for people to download on small devices.
In theory you aren’t supposed to embed resources from the web in an epub book (for example, hotlinking an image), but I think it makes sense to do so with video. If you were distributing an ebook that required a lot of video, I’d probably provide a huge version with everything bundled (for offline use) and another one that simply plays video hosted on YouTube or elsewhere.
Thanks Liza.
In fact, we (my publishing society) plot to release ebooks with video like we did illustrated books before. We’d like to offer new kinds of perspectives, like in a Photoshop manual where illustrations are replaced by videos.
So, i guess, for smartphone devices and netbook, epub is the solution.
Seb
[...] is op dit moment nog geen audio of video mogelijkheid in de ePub standaard, maar er zijn wel manieren om toch audio en video op te nemen in een ePub document. Er zijn tot nu toe alleen weinig devices die dit [...]