How Do I Include YouTube Enclosures in My RSS Feed?

Album Cover: It's Not Me, It's You

"I'm not a saint, but I'm not a sinner...everything's cool as long as I'm getting thinner."
Lily Allen / The Fear

Posted on November 29, 2006 11:28 PM in XML
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

Does anyone out there have experience including YouTube videos as enclosures in their RSS feed?

Every now and then I post YouTube videos to various sites and would love to update my feed generation scripts to convert them to enclosures in my feeds.

The RSS 2.0 Specification says that the <enclosure> element "has three required attributes. url says where the enclosure is located, length says how big it is in bytes, and type says what its type is, a standard MIME type."

Setting the url (URL of the YouTube video) and type (application/x-shockwave-flash) attributes is easy, but how do I figure out the correct length in bytes? Can I just put something arbitrary in there and hope that most feed readers are smart enough to grab the content correctly anyway?

I suppose I could do some experimentation, but just 'cause it works or doesn't work in Bloglines doesn't necessarily mean it works or doesn't work in other feed readers.

I'd love to get some advice from anyone who's gone down this path before. Please leave a comment or contact me if comments aren't your cup of tea. Thanks!


Ian Clifton on December 01, 2006 at 4:01 PM:

I don't think I've ever seen them done with the enclosure tag. All I've seen is them included in the as CDATA and then the regular html (object tag).


Bernie Zimmermann on December 01, 2006 at 10:18 PM:

Ian, that's the problem. Right now I include them as (X)HTML as part of the rest of the <item> content. In order to get Bloglines (and presumably other feed readers) to display the video inside the feed reader itself, I apparently need to figure out how to use the <enclosure> method.


Ian Clifton on December 04, 2006 at 8:36 AM:

One (probably overly complicated) way of doing it is to "wget" whatever movie to see the length. For instance (line breaks added for clarity)

wget --spider

returns a size of 23508.


Ian Clifton on December 04, 2006 at 8:48 AM:

I guess you could actually do it automatically with php using cURL.

// urlSize checks the remote file's header to see what size it is.

function urlSize($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$curlCheck = curl_exec($ch);

$header = ob_get_contents();

$regex = '/Content-Length:s([0-9].+?)s/';
$regexCheck = preg_match($regex, $header, $matches);

return isset($matches[1]) ? $matches[1] : null;


papa louie on August 03, 2017 at 1:32 AM:

I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work.


Post Comments

If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.


Email Address:



Check this box if you hate spam.