My goal: I am trying to allow users to embed a link to a Youtube video in my site, while giving me control over the player\'s settings.
I would like to do this by only a
I think the simplest way to achieve what you're trying to do is the following:
You're trying to get from this:
http://www.youtube.com/watch?v=zb-gmJVW5lw
To this:
<iframe width="640" height="360" src="http://www.youtube.com/embed/zb-gmJVW5l" frameborder="0" allowfullscreen></iframe>
So you can just simply do this:
#### Use a regular expression to extract the video code
@video_id = (/([\w-]{11})/.match(@v_url)).to_s
#### Put this information in the right format
@embed_code = "<iframe width='640' height='360' src='http://www.youtube.com/embed/#{@video_id}' frameborder='0' allowfullscreen></iframe>"
And then in your view, just do:
<%= raw(@embed_code) %>
I do this quite often for clients, the gist of it is that you parse out the ID from the URL, then generate the iframe
HTML using this.
def youtube_embed(youtube_url)
if youtube_url[/youtu\.be\/([^\?]*)/]
youtube_id = $1
else
# Regex from # http://stackoverflow.com/questions/3452546/javascript-regex-how-to-get-youtube-video-id-from-url/4811367#4811367
youtube_url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/]
youtube_id = $5
end
%Q{<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/#{ youtube_id }" frameborder="0" allowfullscreen></iframe>}
end
youtube_embed('youtu.be/jJrzIdDUfT4')
# => <iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/jJrzIdDUfT4" frameborder="0" allowfullscreen></iframe>
I put this in a helper. Change the height, width and options to taste.
I have created a simple helper to embed YouTube videos:
# Helpers for better embedding and manipulation of videos
module VideosHelper
# Regex to find YouTube's video ID
YOUTUBE_REGEX = %r(^(http[s]*:\/\/)?(www.)?(youtube.com|youtu.be)\/(watch\?v=){0,1}([a-zA-Z0-9_-]{11}))
# Embeds YouTube video of given URL in an iframe
#
# @param url [String] URL of desired video
# @param width [String] width of embedded video. Can be any valid CSS unit
# @param height [String] height of embedded video. Can be any valid CSS unit
# @return [String] HTML string of embedded video
def youtube_embed(url, width = "100%", height = "250px")
youtube_id = find_youtube_id(url)
result = %(<iframe title="YouTube video player" width="#{width}"
height="#{height}" src="//www.youtube.com/embed/#{ youtube_id }"
frameborder="0" allowfullscreen></iframe>)
result.html_safe
end
# Finds YouTube's video ID from given URL or [nil] if URL is invalid
# The video ID matches the RegEx \[a-zA-Z0-9_-]{11}\
#
# @param url [String] URL of desired video
# @return [String] video ID of given URL
def find_youtube_id(url)
url = sanitize(url).to_str
matches = YOUTUBE_REGEX.match url
if matches
matches[6] || matches[5]
end
end
end
Another answer is to use this gem which handles youtube and vimeo, and could be expanded to more. It also integrates well with AR so you can cache the resulting html instead of filtering on each render:
https://github.com/dejan/auto_html
something like this (for ex. in model):
@@video_regexp = [ /^(?:https?:\/\/)?(?:www\.)?youtube\.com(?:\/v\/|\/watch\?v=)([A-Za-z0-9_-]{11})/,
/^(?:https?:\/\/)?(?:www\.)?youtu\.be\/([A-Za-z0-9_-]{11})/,
/^(?:https?:\/\/)?(?:www\.)?youtube\.com\/user\/[^\/]+\/?#(?:[^\/]+\/){1,4}([A-Za-z0-9_-]{11})/
]
def video_id
@@video_regexp.each { |m| return m.match(source_url)[1] unless m.nil? }
end
where source_url
is the full link to video.
then a helper:
def youtube_video(video_id)
render :partial => 'youtube_video', :locals => { :id => video_id }
end
and sample partial (haml):
%iframe{:allowfullscreen => "", :frameborder => "0", :height => "349",
:src => "http://www.youtube.com/embed/#{id}", :width => "560"}
and in view as simple as:
= youtube_video Model.video_id
This is what youtube uses:
<iframe width="425" height="349" src="http://www.youtube.com/embed/zb-gmJVW5lw" frameborder="0" allowfullscreen></iframe>
Then just use a regexp to change the link from:
http://www.youtube.com/watch?v=zb-gmJVW5lw
into:
http://www.youtube.com/embed/zb-gmJVW5lw
Here's a proof of concept regexp for matching regular youtube links:
And here's a proof of concept regexp for matching youtu.be links:
Note that the embed url can also be loaded in the browser which opens a page where the video is fullscreen.