Class: Convert2Ascii::Video2Ascii

Inherits:
Object
  • Object
show all
Defined in:
lib/convert2ascii/video2ascii.rb

Constant Summary collapse

DEFAULT_STEP_DURATION =
0.04

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Video2Ascii

Returns a new instance of Video2Ascii.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/convert2ascii/video2ascii.rb', line 22

def initialize(**args)
  @uri = args[:uri]
  @step_duration = args[:step_duration] || DEFAULT_STEP_DURATION
  @threads_count = set_threads_count

  @tmpdir = File.join(Dir.home, ".convert2ascii")
  @output = Dir.pwd

  # image2ascii attrs
  @width = args[:width] || IO.console.winsize[1]
  @style = args[:style] || Image2Ascii::STYLE_ENUM::Color # "color": color ansi , "text": plain text
  @color = args[:color] || Image2Ascii::COLOR_ENUM::Full # full
  @color_block = args[:color_block] || false

  check_packages
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



20
21
22
# File 'lib/convert2ascii/video2ascii.rb', line 20

def output
  @output
end

#step_durationObject

Returns the value of attribute step_duration.



20
21
22
# File 'lib/convert2ascii/video2ascii.rb', line 20

def step_duration
  @step_duration
end

#threads_countObject

Returns the value of attribute threads_count.



20
21
22
# File 'lib/convert2ascii/video2ascii.rb', line 20

def threads_count
  @threads_count
end

#uriObject

Returns the value of attribute uri.



20
21
22
# File 'lib/convert2ascii/video2ascii.rb', line 20

def uri
  @uri
end

#widthObject

Returns the value of attribute width.



20
21
22
# File 'lib/convert2ascii/video2ascii.rb', line 20

def width
  @width
end

Instance Method Details

#after_cleanObject



105
106
107
# File 'lib/convert2ascii/video2ascii.rb', line 105

def after_clean
  remove_tmpdir
end

#generate(**args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/convert2ascii/video2ascii.rb', line 39

def generate(**args)
  @width = args[:width] || @width
  @style = args[:style] || @style # "color": color ansi , "text": plain text
  @color = args[:color] || @color # full
  @color_block = args[:color_block] || @color_block

  remove_tmpdir
  Dir.mkdir(@tmpdir)
  @audio = get_audio_from_video(@tmpdir)
  screenshots_from_video(@tmpdir)
  convert_all_images(@tmpdir)
  @frames_path = order_frames_path

  # save config
  File.open("#{@tmpdir}/meta.json", "w") do |f|
    config = {
      step_duration: @step_duration,
      audio: @audio ? File.basename(@audio) : nil,
      frames_count: @frames_path.length,
    }
    json_data = JSON.generate(config, pretty: true)
    f.puts json_data
  end

  self
end

#order_frames_pathObject



77
78
79
80
81
82
83
84
# File 'lib/convert2ascii/video2ascii.rb', line 77

def order_frames_path
  frames_path = Dir.glob("#{@tmpdir}/*.txt")
  frames_path = frames_path.sort do |a, b|
    get_name_order(a) <=> get_name_order(b)
  end

  @frames_path = frames_path
end

#play(**args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/convert2ascii/video2ascii.rb', line 86

def play(**args)
  play_loop = args[:play_loop] || false
  step_duration = args[:step_duration] || @step_duration
  frames = @frames_path.map { |f| File.open(f).read }

  player_args = {
    frames:,
    audio: @audio,
    play_loop:,
    step_duration:,
  }

  TerminalPlayer.new(**player_args).play

  return true
ensure
  after_clean
end

#save(output_dir) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/convert2ascii/video2ascii.rb', line 66

def save(output_dir)
  system("rm -rf #{@tmpdir}/*.jpg")
  system("rm -rf #{output_dir} && mkdir #{output_dir}")
  system("cp -r #{@tmpdir}/* #{output_dir}")

  puts ""
  puts Rainbow("[info] save success!").green
ensure
  after_clean
end