Sha256: 11d1d0e4e17dfe0022daa00f0511c0ab20fbafc97d257648e17d4b51fe4f8cca

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

require 'uri'
require 'mime/types'

module AirPlayer
  # http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/AirPlayGuide/PreparingYourMediaforAirPlay/PreparingYourMediaforAirPlay.html
  #
  # File Extension | MIME type       | Ruby `mime-types`
  # -------------- | --------------- | -----------------------------
  # .ts            | video/MP2T      | video/mp2t
  # .mov           | video/quicktime | video/quicktime
  # .m4v           | video/mpeg4     | video/m4v
  # .mp4           | video/mpeg4     | application/mp4, video/mp4
  SUPPORTED_MIME_TYPES = %w(
    application/mp4
    video/mp4
    video/m4v
    video/mp2t
    video/quicktime
    video/mpeg4
  )

  SUPPORTED_DOMAINS = %w(
    youtube
    youtu.be
  )

  class Media
    attr_reader :title, :path, :type

    def initialize(target)
      path = File.expand_path(target)

      if File.exist? path
        @path  = path
        @title = File.basename(path)
        @type  = :file
      else
        uri = URI.encode(target)
        @path  = online_media_path(uri)
        @title = online_media_title(uri)
        @type  = :url
      end
    end

    def self.playable?(path)
      MIME::Types.of(path).map(&:simplified).each do |mimetype|
        return SUPPORTED_MIME_TYPES.include?(mimetype)
      end

      host = URI.parse(URI.escape(path)).host
      SUPPORTED_DOMAINS.each do |domain|
        return true if host =~ /#{domain}/
      end

      false
    end

    def file?
      @type == :file
    end

    def url?
      @type == :url
    end

    private
      def online_media_path(uri)
        case URI.parse(uri).host
        when /youtube|youtu\.be/
          uri = `youtube-dl -g #{uri}`
        else
          uri
        end
      end

      def online_media_title(uri)
        case URI.parse(uri).host
        when /youtube|youtu\.be/
          title = `youtube-dl -e #{uri}`
        else
          title = File.basename(uri)
        end
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
airplayer-1.0.1 lib/airplayer/media.rb
airplayer-1.0.0 lib/airplayer/media.rb
airplayer-1.0.0.pre lib/airplayer/media.rb