Class: Cejo::Media::Play

Inherits:
Object
  • Object
show all
Defined in:
lib/cejo/media/play/play.rb

Overview

Play file, random media in folder or with url.

Constant Summary collapse

PLAYER =
'mpv'
PLAYER_CONFIG =
'--no-config --no-audio-display'
PLAYER_FORMAT =
'--ytdl-format="bestvideo[height<=?1080]+bestaudio/best"'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media) ⇒ Play

Returns a new instance of Play.



14
15
16
# File 'lib/cejo/media/play/play.rb', line 14

def initialize(media)
  @media = media
end

Instance Attribute Details

#mediaObject (readonly)

Returns the value of attribute media.



12
13
14
# File 'lib/cejo/media/play/play.rb', line 12

def media
  @media
end

Instance Method Details

#final_commandObject



36
37
38
# File 'lib/cejo/media/play/play.rb', line 36

def final_command
  "#{player_settings} #{pick_media}"
end

#pick_mediaObject



23
24
25
26
27
28
29
30
# File 'lib/cejo/media/play/play.rb', line 23

def pick_media
  filepath = Pathname.new(media)
  return filepath.to_path if filepath.file?

  return pick_random_media_in_folder(filepath) if filepath.directory?

  media
end

#pick_random_media_in_folder(folder) ⇒ Object



18
19
20
21
# File 'lib/cejo/media/play/play.rb', line 18

def pick_random_media_in_folder(folder)
  file = folder.children.sample
  folder.join(file).to_path
end

#player_settingsObject



32
33
34
# File 'lib/cejo/media/play/play.rb', line 32

def player_settings
  "#{PLAYER} #{PLAYER_CONFIG} #{PLAYER_FORMAT}"
end

#runObject



40
41
42
# File 'lib/cejo/media/play/play.rb', line 40

def run
  system final_command
end