Class: Transcode::Transcoder

Inherits:
Object
  • Object
show all
Defined in:
lib/transcode/transcoder.rb

Overview

Transcodes any video file to m4v format.

Instance Method Summary collapse

Constructor Details

#initializeTranscoder

Returns a new instance of Transcoder.



14
15
16
17
# File 'lib/transcode/transcoder.rb', line 14

def initialize
  @cfg = Configuration.new
  @rep = Reporter.new(@cfg.act?, "#{@cfg.dir} -> #{@cfg.out}", @cfg.wid)
end

Instance Method Details

#dataObject

Converts files, audibale, subtitles and titles arrays to array:

[ file1 [ aud1, sub1, tit1 ] ]
[ file2 [ aud2, sub2, tit2 ] ]


44
45
46
# File 'lib/transcode/transcoder.rb', line 44

def data
  @data ||= @cfg.files.zip([@cfg.aud, @cfg.sub, @cfg.tit].transpose)
end

#doObject

rubocop:disable Metrics/AbcSize



65
66
67
68
69
70
71
72
73
74
# File 'lib/transcode/transcoder.rb', line 65

def do # rubocop:disable Metrics/AbcSize
  if @cfg.mp3?
    @cfg.files.each { |f| @rep.add(f, @cfg.act? ? run(mp3_cmd(f)) : true) }
  elsif @cfg.sca?
    @cfg.files.each { |f| @rep.add(f, run(scn_cmd(f))) }
  else
    m4v
  end
  @rep.do
end

#m4vObject



48
49
50
51
52
53
# File 'lib/transcode/transcoder.rb', line 48

def m4v
  data.each do |f, as|
    res = @cfg.act? ? run(m4v_cmd(f, as[0], as[1], as[2])) : true
    @rep.add(f, res, as[0], as[1], as[2])
  end
end

#m4v_cmd(file, aud, sub, tit) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/transcode/transcoder.rb', line 29

def m4v_cmd(file, aud, sub, tit)
  c = 'transcode-video --m4v --no-log --preset veryslow'\
      " --output #{@cfg.out}"
  unless tit == '0'
    c += "/#{File.basename(file.shellescape)}-#{tit}.m4v"
    c += " --title #{tit}"
  end
  c += " --main-audio #{aud}" unless aud == '0'
  c += " --burn-subtitle #{sub}" unless sub == '0'
  c + " #{file.shellescape}"
end

#mp3_cmd(file) ⇒ Object



55
56
57
58
59
# File 'lib/transcode/transcoder.rb', line 55

def mp3_cmd(file)
  file = file.shellescape
  "ffmpeg -i #{file} -vn -ar 44100 -ac 2 -ab 192k -f mp3 "\
    "#{@cfg.out}/#{File.basename(file, '.*')}.mp3"
end

#run(cmd) ⇒ Object

Runs command and prints output instantly. Returns true on success.



20
21
22
23
24
25
26
27
# File 'lib/transcode/transcoder.rb', line 20

def run(cmd)
  cmd += ' 2>&1'
  puts "Run: #{cmd}."
  IO.popen(cmd).each do |line|
    puts line.chomp
  end.close
  $CHILD_STATUS.success?
end

#scn_cmd(file) ⇒ Object



61
62
63
# File 'lib/transcode/transcoder.rb', line 61

def scn_cmd(file)
  "transcode-video --scan #{file.shellescape}"
end