Marilyn Perry's Website Content Youtube Video Embedding Notes

Marilyn Perry's Website Content Youtube Video Embedding Notes

0 comments

Youtube Video Embedding Notes

Marilyn Perry's Notes In Embedding Youtube Videos Into Website Content

Youtube Video Embed Notes

Photo Credit

Aditya Tiwari

Implementing Youtube video embedding isn't as difficult as the inadequate online documentation implies. With the turnkey solution HTML code snippet(s) provided below, embedding Youtube videos into website HTML text is quite easy.

Meanwhile though, within this website's state-of-the-art Drupal DXP infrastructure are various facilities for embedding video within its content and related components. Drupal extension modules some that specifically implement video embedding functionality, along with Drupal features such as custom content blocks and custom paragraph fields, implement video embedding for ease of use during website building. Within the Drupal DXP context just described, video embedding is often as easy as inserting a youtube video URL, or sometimes even just the Youtube video ID string, then clicking a few checkboxes to set display and playback options, and the video is ready to appear on the website. The facilities for video presentation available with the Drupal DXP provided convincing evidence that the Drupal DXP is the most advanced website construction system currently in existence.

Sometimes, however, it's helpful to be able to embed Youtube (or Vimeo) video content directly within the text of articles on a website, even with the possibility of embedding multiple Youtube videos within the text of a single article. At other times, it's also helpful to know about the custom options available in the Youtube video embedding protocol. For example, it's sometimes useful to play a video muted. It is also almost always important to tell youtube to play a video at a resolution of at least 1080p. Tracking down correct and accurate Youtube video embed documentation, and getting Youtube video embed syntax and protocol details just right has involved enough effort that it seems worthy of publishing some notes about the Youtube video embed process. Likely of greatest importance has been creating and testing a basic HTML snippet for Youtube video display that is ready to simply copy and paste into raw HTML article body text.

Although many website sources document the core syntax for using an HTML <iframe> element inside an HTML <div> element to present and play a Youtube video, few, if any such website resources seem to provide complete, or even adequate, information. Among the difficulties has been collecting the syntax and option details required for a working recipe that correctly displays a Youtube video, which is responsive to web browser window size change, and that also remains at a constant, correct 16:9 aspect ratio. After some research, and after assembling the various details of the required HTML, CSS, and so on, the following HTML code snippet implements Youtube video embed code that displays Youtube videos that are both responsive and which maintain their correct 16:9 aspect ratio.

<div style="aspect-ratio:16 / 9;float:left;margin:0.5em 0.5em 0 0;width:100%;">
<iframe style="aspect-ratio:16/9;" width="100%" height="auto" src="https://www.youtube.com/embed/rT27xfa9iik?vq=hd1080&mute=0;&controls=1;&autoplay=0;&disablekb=0;&fs=1;&loop=0;&rel=0" title="insert video title text here" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe>
</div>

Among the most obscure, but most effective approaches to specifying video aspect ratio correctly and accurately is the cascading style sheets (CSS) attribute:  aspect-ratio:16/9; which specifies the dimensions of an HTML element as an aspect ratio. An enclosing  <div style="aspect-ratio:16/9;">  HTML element with the CSS aspect ratio property is one of the most important aspects of this Youtube video embed code snippet. Most of the website sources that contain solution suggestions for solving the Youtube video aspect ratio display problem publish solutions that are either simply wrong, or just don't work very well. 

The second important aspect of the Youtube video embed HTML code snippet provided above is its <div style="width:100%;"> style attribute. Conveniently, the CSS width attribute component of the Youtube video embed snippet can be varied as desired so that a video inline within website article text can appear with whatever width is convenient, while also allowing text to wrap around the text using the CSS float:left attribute. The example embedded Youtube video presented at the left uses a CSS width:60% value.

Jesus Molina and his brothers playing his composition Jireh

At the core of this Youtube video embed code snippet is its HTML <iframe> element. The basic <iframe> element Youtube video embedding recipe is well documented by many website's including Google's own various API documentation websites. Unfortunately, though, few if any of them provide the complete Youtube video embedding code recipe provided here. The most obviously important attribute within the <iframe> element is its src= parameter with the URL of the Youtube video to display. However note that the Youtube video URL itself can optionally include several additional and important attributes. First, to inform Youtube to display a video with a 1080p resolution, or to include other additional options, follow the Youtube video URL with a question mark and then the desired option. Each additional option added to the Youtube video URL needs to be preceded by an ampersand. Within the code snippet for the example video presented with this article is the following option syntax: ?vq=hd1080&mute=0 where vq=hd1080 sets the video playback resolution to 1080p and the mute=0 tells Youtube not to mute this video when the Youtube video player starts to play it.  Some additional video resolution values that can be appended to the Youtube video embed URL include:

  • vq=hd2160 - append this value to the Youtube video embed URL to inform Youtube that 2160p (aka 4K) playback is desired.
  • vq=hd1440 - append this value to the Youtube video embed URL to inform Youtube that 1440p (aka 2K) playback is desired.
  • vq=hd1080 - append this value to the Youtube video embed URL to inform Youtube that 1080p (aka Full HD) playback is desired.
  • vq=hd720 - append this value to the Youtube video embed URL to inform Youtube that 720p playback is desired.
  • vq=large - append this value to the Youtube video embed URL to inform Youtube that 480p playback is desired.
  • vq=medium - append this value to the Youtube video embed URL to inform Youtube that 360p playback is desired.
  • vq=small - append this value to the Youtube video embed URL to inform Youtube that 240p playback is desired.

The video embedding developers at Google have implemented addition additional options that can be appended to the Youtube video embed URL. As documented by Google, some of the options available to enable or disable player features include: autoplay, controls display, computer keyboard response disablement, start and end specification, video looping, progress highlight color, and several other options as well.  The following list describes the most used parameters that can be appended to a Youtube video embed URL to modify the video's behaviors and features.

  • autoplay - The youtube video embed autoplay parameter specifies whether the video starts playing automatically when the player loads. It's supported values are 0 or 1. It's default value is 0 (meaning autoplay off).
  • controls - The Youtube video embed controls parameter indicates whether the play displays its controls features. The controls=0 option means the the player will not display its controls. The controls=1 (its default value) option means the video player will display its controls.
  • disablekb - The disablekb parameter controls whether or not the Youtube video player responds to keyboard input. With the disablekb=0 (its default value) the video player responds to keyboard input. The disablekb=1 option disables video player keyboard input control.
  • fs - The fs option determines whether or not the Youtube embed video player displays its fullscreen viewing option button With fs=1 (its default value) included in the video URL parameter list, the video player displays its fullscreen button.
  • loop - The loop option indicates when videos should loop and play infinitely.
  • rel - The rel option determines whether Youtube displays related videos based on Youtube's related videos algorithm or whether it displays additional videos related to the video's own Youtube channel. When the option rel=0 is used the player will present additional video options exclusively from the video's own Youtube channel.
  • start - The start option informs the Youtube embed video player to begin playing the video at the specified number of seconds from the start of the video. Note that the start and end options can be combined to play a segment from within a Youtube video.
  • end - The end option informs the Youtube embed video player to stop playing the video at the specified number of seconds from the start of the video. Note that the start and end options can be combined to play only a short segment from within a Youtube video if desired. Take note that the end value is measured in seconds from the beginning of the video, not as a relative distance from the position specified by the start parameter.

In addition to the options that can be appended to the Youtube embed video URL, there are several optional attributes that can be included within the HTML <iframe> element that implements Youtube video player integration. With the HTML <iframe>, in addition to its fundamental src= attribute, various other attributes may be specified, including a CSS style attribute, which in this context is structurally important to a video's display dimensions. Other HTML <iframe> element attributes include: height, width, title, loading, and allowfullscreen. Most often, the height attribute should be height="auto" so that the video player can calculate the correct height automatically. The width attribute in an <iframe> element will always be  width="100%" so that the video fills the available horizontal space within the <div> element that surrounds the video's <iframe> element.

To present an embedded Youtube video along with a text caption beneath it, HTML elements that differ slightly from the example above implement the desired features. The caption below the video is implemented with an HTML <figcaption> element. However, the HTML <figcaption> element must exist inside an HTML <figure> element. The HTML code snippet below is the verbatim HTML markup that implements the example video presentation included above with these Youtube video embedding notes. Take note that the added <p> element isn't at all necessary, but also doesn't do any harm. However, because Drupal uses the ckeditor5 JavaScript text editor when editing content, ckeditor5's internal automatic HTML sanitizer unnecessarily and annoyingly adds the <p> element to the HTML markup on its own.  In conclusion, an embedded Youtube video with a caption needs to substitute an outer <figure> element for a generic <div> element so that the nested <figcaption> element can be added beneath the <iframe> element that plays the embedded video.

<figure style="aspect-ratio:16 / 9;border:thin solid white;float:left;font-size:smaller;margin:0.1em 0.4em 0 0;padding:0.1em;text-align:left;text-indent:0;width:60%;">
    <p style="margin:0;padding:0;">
        <iframe style="aspect-ratio:16 / 9;" width="100%" height="auto" src="https://www.youtube.com/embed/rT27xfa9iik?vq=hd1080&amp;mute=0;&amp;controls=0;&amp;autoplay=0;&amp;disablekb=0;&amp;fs=1;&amp;loop=0;&amp;rel=0" title="Digital Keyboardist Jesus Molina and his Brothers grooving in Los Angeles on their composition - Jireh" frameborder="0" allow="accelerometer; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen=""></iframe>
    </p>
    <figcaption style="border-top:thin solid silver;margin-top:3px;">
        <b><i>Jesus Molina</i></b><i> and his brothers playing his composition </i><b><i>Jireh</i></b>
    </figcaption>
</figure>

Assembled together, the Youtube video embed HTML code snippet above can most often be simply pasted into website article raw HTML text, with only the Youtube video ID string needing to be changed, and possibly adjustment of the outer <div> width as desired, to enable a Youtube video to be embedded directly and playable within a website article's text within moments. Website Youtube video embedding can be just that simple.


Thank you for visiting this website, my personal website, and hopefully your enjoyment the information and content shared here publicly at www.marilynperry.comMarilyn Perry | Sunday, January 26, 2025