Approximate time to read: 6 minutes
If you wish to embed a YouTube video on your website then the YouTube site itself will give you the code you need. Underneath the video click on “Share” and then “Embed”. You can choose the video size and whether recommended videos should appear at the end and whether to choose a special privacy mode. In the case of the latter option this uses a special URL that doesn’t store cookies.
But there’s a lot more you can do than just this.
Let’s take some example code…
<iframe src="//www.youtube.com/embed/dp3gz7AM4VY" height="720" width="1280" allowfullscreen="" frameborder="0"></iframe>
The key here is the “src” parameter, which is a URL. At the end of this you can add additional options. The first parameter is separated by a question mark – after that you use an ampersand. Here’s the above example with a couple of extra parameters added…
<iframe src="//www.youtube.com/embed/dp3gz7AM4VY?autoplay=1& autohide=1" height="720" width="1280" allowfullscreen="" frameborder="0"></iframe>
So, what parameters are available? First of all it’s important to understand how the YouTube embedding works. If Flash is supported that will be used, as it supports the most functionality. If not then HTML5 will be used instead. For each of the parameters assume it works with both unless I state otherwise.
Values: 2 (default), 1, and 0. This parameter indicates whether the video controls will automatically hide after a video begins playing. The default behavior (autohide=2) is for the video progress bar to fade out while the player controls (play button, volume control, etc.) remain visible.
- If this parameter is set to 1, then the video progress bar and the player controls will slide out of view a couple of seconds after the video starts playing. They will only reappear if the user moves her mouse over the video player or presses a key on her keyboard.
- If this parameter is set to 0, the video progress bar and the video player controls will be visible throughout the video and in fullscreen.
Values: 0 or 1. Default is 0. Sets whether or not the initial video will autoplay when the player loads.
Values: 1. Default is based on user preference. Setting to 1 will cause closed captions to be shown by default, even if the user has turned captions off. This does not work with the HTML5 player.
This parameter specifies the color that will be used in the player’s video progress bar to highlight the amount of the video that the viewer has already seen. Valid parameter values are red and white, and, by default, the player will use the color red in the video progress bar.
Note: Setting the color parameter to white will disable the modestbranding option.
Values: 0, 1, or 2. Default is 1. This parameter indicates whether the video player controls will display and for embeds that load a Flash player, it also defines when the controls display in the player as well as when the player will load:
controls=0 – Player controls do not display in the player. For IFrame embeds, the Flash player loads immediately.
controls=1 – Player controls display immediately and the Flash player also loads immediately.
controls=2 – Player controls display and the Flash player loads after the user initiates the video playback.
Note: The parameter values 1 and 2 are intended to provide an identical user experience, but controls=2 provides a performance improvement over controls=1. Currently, the two values still produce some visual differences in the player, such as the video title’s font size. However, when the difference between the two values becomes completely transparent to the user, the default parameter value may change from 1 to 2.
Values: 0 or 1. Default is 0. Setting to 1 will disable the player keyboard controls. Keyboard controls are as follows:
Spacebar: Play / Pause
Arrow Left: Jump back 10% in the current video
Arrow Right: Jump ahead 10% in the current video
Arrow Up: Volume up
Arrow Down: Volume Down
This does not work with the HTML5 player.
Values: A positive whole number. This parameter specifies the time, measured in seconds from the start of the video, when the player should stop playing the video. This does not work with the HTML5 player.
Values: 0 or 1. For the AS3 player, the default value is 1, which causes the fullscreen button to display. Setting this parameter to 0 prevents the fullscreen button from displaying. This does not work with the HTML5 player.
Values: 1 or 3. Default is 1. Setting to 1 will cause video annotations to be shown by default, whereas setting to 3 will cause video annotations to not be shown by default.
The list parameter, in conjunction with the listType parameter, identifies the content that will load in the player.
If the listType parameter value is search, then the list parameter value specifies the search query.
If the listType parameter value is user_uploads, then the list parameter value identifies the YouTube channel whose uploaded videos will be loaded.
If the listType parameter value is playlist, then the list parameter value specifies a YouTube playlist ID. In the parameter value, you need to prepend the playlist ID with the letters PL. For example… http://www.youtube.com/embed?listType=playlist&list=PLC77007E23FF423C6
This does not work with the HTML5 player.
Note: If you specify values for the list and listType parameters, the embed URL does not need to specify a video ID.
The listType parameter, in conjunction with the list parameter, identifies the content that will load in the player. Valid parameter values are playlist, search, and user_uploads.
If you specify values for the list and listType parameters, the embed URL does not need to specify a video ID.
This does not work with the HTML5 player.
Values: 0 or 1. Default is 0. In the case of a single video player, a setting of 1 will cause the player to play the initial video again and again. In the case of a playlist player (or custom player), the player will play the entire playlist and then start again at the first video.
Note: Currently, the loop parameter only works in the player when used in conjunction with the playlist parameter. To loop a single video, set the loop parameter value to 1 and set the playlist parameter value to the same video ID already specified in the URL. For example…
Set the parameter value to 1 to prevent the YouTube logo from displaying in the control bar.
Note: a small YouTube text label will still display in the upper-right corner of a paused video when the user’s mouse pointer hovers over the player.
Value is a comma-separated list of video IDs to play. If you specify a value, the first video that plays will be the VIDEO_ID specified in the URL path, and the videos specified in the playlist parameter will play thereafter.
This parameter controls whether videos play inline or fullscreen in an HTML5 player on iOS. Valid values are:
0: This value causes fullscreen playback. This is currently the default value, though the default is subject to change.
1: This value causes inline playback.
This does not work with the Flash player.
Values: 0 or 1. Default is 1. This parameter indicates whether the player should show related videos when playback of the initial video ends.
Values: 0 or 1. The parameter’s default value is 1. If you set the parameter value to 0, then the player will not display information like the video title and uploader before the video starts playing.
If the player is loading a playlist, and you explicitly set the parameter value to 1, then, upon loading, the player will also display thumbnail images for the videos in the playlist.
Values: A positive whole number. This parameter causes the player to begin playing the video at the given number of seconds from the start of the video.
Note: the player will look for the closest keyframe to the time you specify. This means that sometimes the play head may seek to just before the requested time, usually no more than around two seconds.
This parameter indicates whether the embedded player will display player controls (like a play button or volume control) within a dark or light control bar. Valid parameter values are dark and light, and, by default, the player will display player controls using the dark theme.
This is an undocumented parameter! By default YouTube will pick a video quality based upon current bandwidth. However, with this parameter you can force it. Parameters are…
vq=small – 240p
vq=medium – 360p
vq=large – 480p
vq=hd720 – 720p
vq=hd1080 – 1080p
Those list parameters look a bit tricky so here are some examples…
Loading a Playlist
Note that you need to prepend the playlist ID with the letters PL.
Loading a user’s uploaded videos
Set the listType player parameter to user_uploads. In addition, set the list player parameter to the YouTube username whose uploaded videos you want to load.
Loading search results for a specified query
Set the listType player parameter to search. In addition, set the list player parameter to the query term for which you want the player to load search results. Ensure you replace any spaces in the query term with %20