YouTube Video Embed

How do you embed just that green segment of a YouTube video?

Sometimes you may want to embed just a portion of a YouTube video in your web pages.

For instance, you are embedding a movie from YouTube but want the viewer to focus of one particular scene that begins at ‘x’ seconds and ends at ‘y’ seconds. When the scene has finished, the embedded clip should stop playing irrespective of the length of the video.

Well, here are two simple ways to help you embed a part of any YouTube video:

Case A: Embed YouTube Video with Start Time

This is a scenario where you specify a start time for the embedded video and let it play through the end. Here you can use the standard embed code and append the start time parameter to the YouTube URL as illustrated in the following example:

<iframe width="500" height="300" frameborder="0" allowfullscreen
   src="http://www.youtube.com/embed/VIDEO_ID#t=1234s"></iframe>

Replace VIDEO_ID with the actual ID of your YouTube video and replace 1234s with the start time (in seconds). For instance, if you want the video to start playback at the 03:24 (mm:ss) mark, you’ll specify the time as t=204s (60*3 + 24).

Case B: Embed YouTube Video with Start & End Time

The following YouTube video from an Apple event is over an hour long but I have only embedded the most interesting 2-minute segment (22:10 – 24:26) where Steve Jobs first announced the iPhone.

Hit the play button inside the embedded player for a quick demo.

The standard YouTube embed code doesn’t support the end time parameter but we can make use of the YouTube JavaScript API to embed a part of any YouTube video. Without boring you with the technical details, here’s your new embed code:

<div videoID="t2MOwQ089eQ" startTime="1330" endTime="1466" height="400" width="400" id="youtube-player"></div>

<script src="http://labnol.googlecode.com/files/youtube-embed.js"></script>

You just have to replace the Video ID, the start time (in seconds), the end time (in seconds), the height of the player (in pixels) and the width in the <DIV> tag as per your needs. See this annotated source code to learn how the playback is controlled via the YouTube API.

Also, if you are planning to use this embed code on a popular site, please host the youtube-embed.js file on your own server as googlecode.com may offer limited bandwidth.