Converting a regular Youtube 'link' into an embedded video

前端 未结 10 1833
别那么骄傲
别那么骄傲 2021-01-30 02:25

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

10条回答
  •  不思量自难忘°
    2021-01-30 03:02

    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{}
    end
    
    youtube_embed('youtu.be/jJrzIdDUfT4')
    # => 
    

    I put this in a helper. Change the height, width and options to taste.

提交回复
热议问题