1

I need to strip YouTube embed codes down to their URL only.

This is the exact opposite of all but one question on StackOverflow. Most people want to turn the URL into an embed code. This question addresses the usage patttern I want, but is tied to a specific embed code's regex ( Strip YouTube Embed Code Down to URL Only )

I'm not familiar with how YouTube has offered embeds over the years - or how the sizes differ. According to their current site, there are 2 possible embed templates and a variety of options. If that's it, I can handle a regex myself -- but I was hoping someone had more knowledge they could share, so I could write a proper regex pattern that matches them all and not run into endless edge-cases.

The full use case scenario :

  • user enters content in web based wysiwig editor
  • backend cleans out youtube & other embed codes; reformats approved embeds into an internal format as the text is all converted to markdown.
  • on display, appropriate current template/code display for youtube or other 3rd party site is generated

At a previous company, our tech-team devised a plan where YouTube videos were embedded by listing the URL only. That worked great , but it was in a CMS where everyone was trained. I'm trying to create a similar storage, but for user-generated-content.

Community
  • 1
  • 1
Jonathan Vanasco
  • 15,111
  • 10
  • 48
  • 72

1 Answers1

0

You might consider using BBCode for user-generated content (here's a python module for it).

The vBulletin forum software allows YouTube embeds via a [youtube] tag. Your users would just enter the code for the video and not the complete URL, then you create the embed code on the server-side.

If they enter the full URL to a video, then you just render a link to it.

From this example:

Some video found at http://youtube.com/watch?v={video_id}

In your text editor: [youtube]{video_id}[/youtube]

Outputs this HTML: <object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/{video_id}"></param><embed src="http://www.youtube.com/v/{video_id}" type="application/x-shockwave-flash" width="425" height="350"></embed></object>

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
  • Thanks. I have that sort of functionality already built. We're also committed to Markdown. My issue is in catching all of the object/iframe embeds for YouTube if someone shares like that instead of with a shorthand code like you suggested. While I could just lock down everything to not allow for embeds, I've found that new users can get turned off from sharing content when that lockdown is enabled. So instead of just stripping out embeds/iframes that shouldn't be in there, I want to catch them and convert them to shorthand. – Jonathan Vanasco Nov 26 '12 at 19:08
  • I understand not wanting to lock down embeds, but if you allow them, are you going to restrict them to only YouTube URLs? You could get people trying to embed all kinds of videos. Giving them a YouTube icon tied to a JavaScript prompt() for a URL might be easier and safer. – Adrian J. Moreno Nov 26 '12 at 20:15
  • I'm restricting people to a handful of approved video types (basically anything on embedly). All the other providers we're supporting have had regexe's that are more predictable / unified because they haven't changed as much over the years. – Jonathan Vanasco Nov 26 '12 at 22:41