Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a regular Youtube 'link' into an embedded video

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 asking the user to supply the link (not the entire embed code), from where I can somehow paste that link into the embed code.

I've tried doing a simple substitution with a few Youtube links (http://youtu.be/...) but they don't work, saying 'movie not loaded'. Is there a dependable way to do this?

like image 506
sscirrus Avatar asked May 06 '11 09:05

sscirrus


People also ask

How do you tell if a YouTube video can be embedded?

Find a video you like on YouTube, then click the Share button to see whether the embed functionality has been enabled. Do not alter that code when displaying videos on your practice website. Show the video non-commercially. Credit the creator of the video.

Is it better to embed video or link to YouTube?

One video can do double duty on your YouTube Channel and your website, so it's more useful to you overall. Keep in mind that it's important to actually embed your YouTube videos rather than just linking to them. Embedded videos are more searchable than links, which is good news for your SEO strategy.

Can a link be embedded in a video?

Adding clickable links to native mp4 video file is not possible. The way to add these is in the video player. When you upload your video to the specific player on YouTube, Vimeo, or Instagram, you can create and add embedded links through those specific video players.


4 Answers

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.

like image 178
Douglas F Shearer Avatar answered Oct 02 '22 15:10

Douglas F Shearer


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

like image 43
Andrew Kuklewicz Avatar answered Oct 02 '22 15:10

Andrew Kuklewicz


I used the highest rated answer about with the function youtube_embed but when I implemented in my view I was seeing the iframe code appear in my page, but no video. I added raw before the function call and all is good with the page now.

Inside my view.html.erb

<p><%= raw(youtube_embed(@experiment.researchsection.videolink)) %></p>
like image 37
Preston Spratt Avatar answered Oct 02 '22 17:10

Preston Spratt


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
like image 21
schiza Avatar answered Oct 02 '22 17:10

schiza