How to Embed a Part of any YouTube Video

H
Published in: Embed - YouTube

YouTube Video Embed

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 on a 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:

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 from YouTube 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).

B: Embed YouTube Video with Start & End Time

The following YouTube video recording from a Yanni concert is several minutes long but I’ve only embedded the most interesting segment where the lady is playing the violin.

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

    <div data-video = "Iq3zo432sAU"
         data-startseconds = "323"
         data-endseconds = "432"
         data-height = "309"
         data-width = "550"
         id = "youtube-player">
    </div>

    <script src="https://www.youtube.com/iframe_api"></script>
    <script type="text/javascript">
      function onYouTubeIframeAPIReady() {
        var ctrlq = document.getElementById("youtube-player");
        var player = new YT.Player('youtube-player', {
          height: ctrlq.dataset.height,
          width: ctrlq.dataset.width,
          events: {
            'onReady': function(e) {
              e.target.cueVideoById({
                videoId: ctrlq.dataset.video,
                startSeconds: ctrlq.dataset.startseconds,
                endSeconds: ctrlq.dataset.endseconds
              });
            }
          }
        });
      }
    </script>

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 data-video = "VIDEO_ID"
         data-startseconds = "100"
         data-endseconds = "200"
         data-height = "480"
         data-width = "640"
         id = "youtube-player">
    </div>

    <script src="https://www.youtube.com/iframe_api"></script>
    <script type="text/javascript">
      function onYouTubeIframeAPIReady() {
        var ctrlq = document.getElementById("youtube-player");
        var player = new YT.Player('youtube-player', {
          height: ctrlq.dataset.height,
          width: ctrlq.dataset.width,
          events: {
            'onReady': function(e) {
              e.target.cueVideoById({
                videoId: ctrlq.dataset.video,
                startSeconds: ctrlq.dataset.startseconds,
                endSeconds: ctrlq.dataset.endseconds
              });
            }
          }
        });
      }
    </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 see: YouTube as Audio Player

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Embed - YouTube

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube