Meta:Create YouTube playlists on-the-fly with semantic queries

From protonation
Jump to navigation Jump to search
VLC.svg Under construction

This page is under construction.

It is easy to create a YouTube playlist from a query. All you need is the (upgraded) Widget:YouTube from the MediaWiki Widgets extension.

In essence, what we describe here could be seen as a new semantic results format; we could call it the YouTube playlist format!


YouTube allows the creation of playlists on the fly, i.e. without even logging in, just from the URL. All you need to do is provide the URL with a comma separated list of the video IDs you want to play sequentially.[1]

The URL must have the following form:,VIDEO_ID_2, … ,VIDEO_ID_n

Think of the above not so much as a URL but rather as a command to YouTube to create a playlist. When YouTube receives this "command" it automatically creates a playlist, as if it was created by a user, and then assigns it a playlist ID as well, which is permanent. Then it redirects you to a "proper" YouTube playlist URL.

For example, if you click this URL:,iZ9vkd7Rp-g,L4GrDOOIIbk

you will be redirected to this URL:

where TLGGUeJrTjDLyMgyMjA1MjAyMA is the newly created playlist ID. This playlist ID is as good as any playlist created by a user. You may use it the same way you would use a "proper" playlist ID and it never expires.

Yeah, but how do I embed this?

If you click on the YouTube share button and then select the embed option you will get a simple iframe code with some control params such as dimensions etc and the embed URL which has the following form:

If you try to share a playlist you will get a slightly different URL:

Note that you need to prepend the playlist ID with the letters PL as shown in the following example:,OyiNc7zNN9c


Embeding YouTube player in MediaWiki

All the above refer to pure HTML webpages. In MediaWiki we cannot just use raw iframe code. Instead we can use the Widget:YouTube which does the embeding in a wiki-secure way.

So, to embed an on-the-fly playlist, along with the param id we use the extra param additionalids with a comma separated list of IDs, as follows:

{{#widget:YouTube|id=VIDEO_ID_1|additionalids=VIDEO_ID_2,VIDEO_ID_3, … ,VIDEO_ID_n}}

where id is the video ID of the first video and additionalids is a comma separated list of the additional video IDs, i.e. from the second to the n-th one.

For example:



Boom! We've just embeded a playlist from three separate video IDs!

Get the video IDs from a semantic query

The query

Let's suppose that we have a Category:YouTube videos where each page stores the details of a YouTube video such as title (that could be the page's title), video ID, publication date, views etc. Note, that we can easily populate these pages with the video data via the YouTube API using the ExternalData extension.

So, a simple query to get a comma separated list of video IDs would look like this:

{{#ask: [[Category:YouTube videos]]
| mainlabel=-
| ?Has video ID=
| sort=Is published on
| order=descending
| limit=10
| searchlabel=


  • mainlabel=- to omit the page titles from results. That way we get a list, otherwise we would get a table.
  • ?Has video ID=(nothing) so in the results we get only the video ID without the property name. Otherwise we would get: Has video ID:ntpt0fq3Jn8, Has video ID:iZ9vkd7Rp-g, etc
  • sort=Is published on and order=descending: We may sort this list any way that we want. In this example it is sorted by publication date in reverse order, i.e. latest videos first.
  • searchlabel=(nothing) to omit the ...further results message at the end of the list. We don't want to feed that too into YouTube URL!

A query like this would produce a comma separated list of plain video IDs such as: ntpt0fq3Jn8|additionalids=iZ9vkd7Rp-g,L4GrDOOIIbk, etc

Format the list

The problem now is that the list contains spaces between items. Moreover we need the first ID separate from the others.

There are two ways to deal with this:

  1. Using a template inside the query to manually format the result without the spaces. That way we will need two queries, one to get the first ID and one to get the list of the rest of the IDs.
  2. Using some other tool such as the StringFunctions extension to manipulate the query result. That way we only need one query.

I personally prefer the second method because it uses just one query. Imagine that all this will eventually end up inside a template, or more for that matter. In a big wiki, one query more could mean millions of extra queries in the long run. Which means more processing time, more errors, more maintenance, and so one. In a semantic wiki it is essential to keep the least number of queries possible. That's why I have come to appreciate the power of the Variables[3] extension and use it almost everywhere. I will use it here as well.

Let's begin with storing the query output into a variable named query_output:

{{#vardefine:query_output|{{#ask: … }} }}

First template

Create a template to get the first item of a comma separated list. The StringFunctions[4] extension is required. I actually use this template on this wiki.

Template:First item of a comma separated list [view code]


Second template

Create another template to get the rest of the items of a comma separated list, i.e. minus the first item, and remove spaces.

Template:Comma separated list minus first item spaces removed [view code]

{{#replace:{{#explode:{{{1|}}}|,|1|2}}| |}}

Wrap it all up

The widget call should look like this:

 | id={{first item of a comma separated list|{{#var:query_output}} }}
 | additionalids={{Comma separated list minus first item spaces removed|{{#var:query_output}} }}

Let's put them all together:

 | {{#ask: [[Category:YouTube videos]]
   | mainlabel=-
   | ?Has video ID=
   | sort=Is published on
   | order=descending
   | limit=10
   | searchlabel=

 | id={{first item of a comma separated list|{{#var:query_output}} }}
 | additionalids={{Comma separated list minus first item spaces removed|{{#var:query_output}} }}



blog comments powered by Disqus