Converting a regular Youtube 'link' into an embedded video

前端 未结 10 1830
别那么骄傲
别那么骄傲 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:20

    I had to incorporate this functionality in one of my recent projects. I had to support linking both YouTube and Vimeo videos. I am using the 'uri' module of Ruby and the HTTParty. Basically I came with the following:

    class LinkVideo < ActiveRecord::Base
      require 'uri'
      include HTTParty
    
      cattr_reader :per_page
      @@per_page = 12
    
      belongs_to :user
    
      validates :weblink, :presence => true, :domain => true
    
    
      def embed(width = "640", height = "390")
        embed_code = nil
    
        case base_uri
          when "www.youtube.com"
            embed_code = "" +
                  "" +
                  "" +
                  "" +
                  " " +
                ""
            when "www.vimeo.com"
              embed_code = ""
          end
    
          embed_code
      end
    
      def url
        url = nil
        case base_uri
          when "www.youtube.com"
            url = "http://www.youtube.com/v/" + video_id + "&hl=en_US&fs=1"
          when "www.vimeo.com"
            url = "http://player.vimeo.com/video/" + video_id
        end
    
        url
      end
    
      def thumbnail
        url = nil
        case base_uri
          when "www.youtube.com"  
            url = "http://img.youtube.com/vi/" + video_id + "/2.jpg"
          when "www.vimeo.com"
            url = thumbnail_path( image_base_uri, video_id )
        end
    
        url  
      end
    
      # Video Paths:
      #   http://www.youtube.com/watch?v=Gqraan6sBjk
      #   http://www.vimeo.com/21618919
      # Thumbnail Paths:
      #   http://img.youtube.com/vi/Gqraan6sBjk/2.jpg
      private
        def image_base_uri
          image_base_uri = nil
          case base_uri
            when "www.youtube.com"
              image_base_uri = "http://img.youtube.com/vi/"
            when "www.vimeo.com"
              image_base_uri = "http://vimeo.com/api/v2/video/"
          end
    
          image_base_uri
        end
    
        def thumbnail_path(base_uri, videoid = nil, format = 'xml')
          path = nil
    
          return path if base_uri.nil?
    
          xml     = HTTParty.get( base_uri + ( videoid.nil? ? video_id : videoid ) + format.insert(0, '.') )
          values  = xml.parsed_response.values_at("videos").first.fetch('video')
          if values["user_portrait_medium"].include?('100')
            path  = values["user_portrait_medium"]
          else values["user_portrait_large"].include?('100')
            path = values["user_portrait_large"]
          end
    
          path
        end
    
        def base_uri
          @uri ||= parse_it
    
          @uri.host
        end
    
        def video_id
          video_id = nil
          case base_uri
            when "www.youtube.com"
              video_id = @uri.query.split('=')[1].slice(0, 11)
            when "www.vimeo.com"
              video_id = @uri.path.delete('/')
          end
    
          video_id
        end
    
        def parse_it
          @uri = URI.parse( weblink )
        end
    end
    

提交回复
热议问题