Class: Evideo::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/evideo/vars1.rb,
lib/evideo/vars2.rb,
lib/evideo/processa.rb

Overview

classe permite analizar/processar videos para arquivo uniformizado

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ficheiro, opcoes) ⇒ Video

Returns videos processados para arquivo uniformizado.

Parameters:

  • ficheiro (String)

    video a processar

  • opcoes (Thor::CoreExt::HashWithIndifferentAccess)

    trabalho

Options Hash (opcoes):

  • :d (Array<String>) — default: /home/eu/lust, /media/eu/hrv2, /media/eu/hrv2/lust

    pastas onde procurar videos

  • :i (String) — default: ftv

    pasta inicial dos videos

  • :o (String) — default: out

    pasta final dos videos

  • :s (Boolean) — default: false

    10 segundos cortados no inicio do video final

  • :t (Integer) — default: 0

    segundos duracao video final 0=sem cortes



33
34
35
36
37
38
# File 'lib/evideo/vars1.rb', line 33

def initialize(ficheiro, opcoes)
  @ext = File.extname(ficheiro)
  @bas = File.basename(ficheiro, ext)
  @ops = opcoes
  @iopcao = {}
end

Instance Attribute Details

#basString (readonly)

Returns base ficheiro video.

Returns:

  • (String)

    base ficheiro video



19
20
21
# File 'lib/evideo/vars1.rb', line 19

def bas
  @bas
end

#extString (readonly)

Returns extensao ficheiro video.

Returns:

  • (String)

    extensao ficheiro video



21
22
23
# File 'lib/evideo/vars1.rb', line 21

def ext
  @ext
end

#opsThor::CoreExt::HashWithIndifferentAccess (readonly)

Returns opcoes trabalho.

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)

    opcoes trabalho



23
24
25
# File 'lib/evideo/vars1.rb', line 23

def ops
  @ops
end

Class Method Details

.to_t(tempo, pre = 8) ⇒ Time

Returns tempo no ruby standard library com precisao.

Returns:

  • (Time)

    tempo no ruby standard library com precisao



10
11
12
# File 'lib/evideo/vars2.rb', line 10

def self.to_t(tempo, pre = 8)
  Time.parse(tempo[0, pre])
end

Instance Method Details

#cmd_mpeg(base) ⇒ String

Returns comando mpeg.

Parameters:

  • base (String)

    ficheiro video final

Returns:

  • (String)

    comando mpeg



97
98
99
100
101
102
# File 'lib/evideo/processa.rb', line 97

def cmd_mpeg(base)
  oout = "#{base}.out"
  "ffmpeg -loglevel quiet -hide_banner -i #{inome} -y -an #{fparams}#{fcuts} "\
  '-metadata title= -metadata artist= -metadata comment= -metadata major_brand= -metadata compatible_brands= '\
  "#{base}.mp4 >#{oout} 2>&1;[ -s #{oout} ] || rm #{oout}"
end

#cmd_prob(ficheiro) ⇒ String

Returns comando probe.

Parameters:

  • ficheiro (String)

    video

Returns:

  • (String)

    comando probe



91
92
93
# File 'lib/evideo/processa.rb', line 91

def cmd_prob(ficheiro)
  "ffprobe -hide_banner -show_streams #{ficheiro} 2>&1|grep -v title"
end

#fcutsString

Returns cortes inicio video & duracao do video final processado.

Returns:

  • (String)

    cortes inicio video & duracao do video final processado



83
84
85
86
87
# File 'lib/evideo/processa.rb', line 83

def fcuts
  ict = Integer(ops[:s])
  fct = Integer(ops[:t])
  "#{ict.positive? ? " -ss #{ict}" : ''}#{fct.positive? ? " -t #{fct}" : ''}"
end

#fdimensionsString

Note:

final video dimensions hd480=852x480, hd720=1280x720, hd1080=1920x1080

Returns dimensions do comando processar video.

Returns:

  • (String)

    dimensions do comando processar video



70
71
72
73
74
75
# File 'lib/evideo/processa.rb', line 70

def fdimensions
  if    iheight < 480  then ' -s hd480'
  elsif iheight <= 720 then ' -s hd720'
  else                      ' -s hd1080'
  end
end

#fparamsString

Returns parametros do comando processar video.

Returns:

  • (String)

    parametros do comando processar video



63
64
65
66
# File 'lib/evideo/processa.rb', line 63

def fparams
  # frame rate & bitrate & dimensions & aspect ratio
  "-r #{[ifps, 25.0].min} -b:v #{[ibitrate, 2000].min}k#{fdimensions}#{fratio}"
end

#fratioString

Returns aspect ratio do comando processar video.

Returns:

  • (String)

    aspect ratio do comando processar video



78
79
80
# File 'lib/evideo/processa.rb', line 78

def fratio
  ART.include?(iratio) ? " -aspect 16:9 -filter:v 'pad=max(iw\\,ih*16/9):ih:(ow-iw)/2:(oh-ih)/2'" : ''
end

#i1scanArray<String>

Returns parametros video inicial [:tempo, :bitrate].

Returns:

  • (Array<String>)

    parametros video inicial [:tempo, :bitrate]



46
47
48
# File 'lib/evideo/vars1.rb', line 46

def i1scan
  @i1scan ||= iprobe.scan(RE1).flatten
end

#i2scanArray<String>

Returns parametros video inicial [:height, :fps].

Returns:

  • (Array<String>)

    parametros video inicial [:height, :fps]



51
52
53
# File 'lib/evideo/vars1.rb', line 51

def i2scan
  @i2scan ||= iprobe.scan(RE2).flatten
end

#iaudioInteger

Returns parametro video inicial :audio Hz.

Returns:

  • (Integer)

    parametro video inicial :audio Hz



81
82
83
# File 'lib/evideo/vars1.rb', line 81

def iaudio
  @iopcao[:audio] ||= Integer(iprobe.scan(RE4).flatten[0] || 0)
end

#ibitrateInteger

Returns parametro video inicial :bitrate kb/s.

Returns:

  • (Integer)

    parametro video inicial :bitrate kb/s



61
62
63
# File 'lib/evideo/vars1.rb', line 61

def ibitrate
  @iopcao[:bitrate] ||= Integer(i1scan[1] || 0)
end

#ifpsFloat

Returns parametro video inicial :fps frame_rate.

Returns:

  • (Float)

    parametro video inicial :fps frame_rate



71
72
73
# File 'lib/evideo/vars1.rb', line 71

def ifps
  @iopcao[:fps] ||= Float(i2scan[1] || 0)
end

#iheightInteger

Returns parametro video inicial :height.

Returns:

  • (Integer)

    parametro video inicial :height



66
67
68
# File 'lib/evideo/vars1.rb', line 66

def iheight
  @iopcao[:height] ||= Integer(i2scan[0] || 0)
end

#inomeString

Returns ficheiro inicial absoluto.

Returns:

  • (String)

    ficheiro inicial absoluto



86
87
88
# File 'lib/evideo/vars1.rb', line 86

def inome
  "#{ops[:d][0]}/#{ops[:i]}/#{bas}#{ext}"
end

#inoutString

Returns params video inicial & comando mpeg.

Returns:

  • (String)

    params video inicial & comando mpeg



29
30
31
# File 'lib/evideo/processa.rb', line 29

def inout
  "#{bas}#{ishow} OUT: #{fparams}"
end

#iprobeString

Returns texto probe do video inicial.

Returns:

  • (String)

    texto probe do video inicial



41
42
43
# File 'lib/evideo/vars1.rb', line 41

def iprobe
  @iprobe ||= `#{cmd_prob(inome)}`
end

#iratioString

Returns parametro video inicial aspect :ratio 16:9.

Returns:

  • (String)

    parametro video inicial aspect :ratio 16:9



76
77
78
# File 'lib/evideo/vars1.rb', line 76

def iratio
  @iopcao[:ratio] ||= (iprobe.scan(RE3).flatten[0] || '0:1')
end

#ishowString

Returns mostra dados do ficheiro video inicial.

Returns:

  • (String)

    mostra dados do ficheiro video inicial



91
92
93
# File 'lib/evideo/vars1.rb', line 91

def ishow
  "# r:#{ibitrate} h:#{iheight} #{iratio}"
end

#itempoString

Returns parametro video inicial :tempo hh:mm:ss.

Returns:

  • (String)

    parametro video inicial :tempo hh:mm:ss



56
57
58
# File 'lib/evideo/vars1.rb', line 56

def itempo
  @iopcao[:tempo] ||= (i1scan[0] || '00:00:00')
end

#ivideo?true, false

Returns video inicial ok.

Returns:

  • (true, false)

    video inicial ok



34
35
36
37
38
39
# File 'lib/evideo/processa.rb', line 34

def ivideo?
  return false if ext != '.mp4' || iaudio.positive? || ibitrate >= 3000 || iheight <= 480

  puts("mv #{inome} #{onome} #{ishow}")
  true
end

#o1scanArray<String>

Returns parametros video final [:tempo, :bitrate].

Returns:

  • (Array<String>)

    parametros video final [:tempo, :bitrate]



30
31
32
# File 'lib/evideo/vars2.rb', line 30

def o1scan
  @o1scan ||= oprobe.scan(RE1).flatten
end

#o2scanArray<String>

Returns parametros video final [:height, :fps].

Returns:

  • (Array<String>)

    parametros video final [:height, :fps]



35
36
37
# File 'lib/evideo/vars2.rb', line 35

def o2scan
  @o2scan ||= oprobe.scan(RE2).flatten
end

#obitrateInteger

Returns parametro video final :bitrate kb/s.

Returns:

  • (Integer)

    parametro video final :bitrate kb/s



45
46
47
# File 'lib/evideo/vars2.rb', line 45

def obitrate
  @oopcao[:bitrate] ||= Integer(o1scan[1] || 0)
end

#ofpsFloat

Returns parametro video final :fps frame_rate.

Returns:

  • (Float)

    parametro video final :fps frame_rate



55
56
57
# File 'lib/evideo/vars2.rb', line 55

def ofps
  @oopcao[:fps] ||= Float(o2scan[1] || 0)
end

#oheightInteger

Returns parametro video final :height.

Returns:

  • (Integer)

    parametro video final :height



50
51
52
# File 'lib/evideo/vars2.rb', line 50

def oheight
  @oopcao[:height] ||= Integer(o2scan[0] || 0)
end

#oinitObject

inicia variaveis do video final



15
16
17
18
19
20
# File 'lib/evideo/vars2.rb', line 15

def oinit
  @oopcao = {}
  @oprobe = nil
  @o1scan = nil
  @o2scan = nil
end

#onomeString

Returns video final absoluto.

Returns:

  • (String)

    video final absoluto



73
74
75
76
# File 'lib/evideo/vars2.rb', line 73

def onome
  dir = ops[:d]
  (pos == -1 ? "#{dir[0]}/#{ops[:i]}" : dir[pos]) + "/#{ops[:o]}/#{bas.downcase}.mp4"
end

#opastas?true, false

Returns pastas com video final ok.

Returns:

  • (true, false)

    pastas com video final ok



51
52
53
54
55
56
57
58
59
60
# File 'lib/evideo/processa.rb', line 51

def opastas?
  ary = ops[:d]
  if pos == ary.size then false
  elsif ovideo?      then true
  else
    # proxima pasta
    @pos += 1
    opastas?
  end
end

#oprobeString

Returns texto probe do video final.

Returns:

  • (String)

    texto probe do video final



23
24
25
26
27
# File 'lib/evideo/vars2.rb', line 23

def oprobe
  return '' unless File.exist?(onome)

  @oprobe ||= `#{cmd_prob(onome)}`
end

#oratioString

Returns parametro video final aspect :ratio 16:9.

Returns:

  • (String)

    parametro video final aspect :ratio 16:9



60
61
62
# File 'lib/evideo/vars2.rb', line 60

def oratio
  @oopcao[:ratio] ||= (oprobe.scan(RE3).flatten[0] || '0:1')
end

#oshowString

Returns mostra dados do ficheiro video final.

Returns:

  • (String)

    mostra dados do ficheiro video final



79
80
81
# File 'lib/evideo/vars2.rb', line 79

def oshow
  "# r:#{obitrate} h:#{oheight} #{oratio} #{ops[:d][pos]}/#{ops[:o]}"
end

#otempoString

Returns parametro video final :tempo hh:mm:ss.

Returns:

  • (String)

    parametro video final :tempo hh:mm:ss



40
41
42
# File 'lib/evideo/vars2.rb', line 40

def otempo
  @oopcao[:tempo] ||= (o1scan[0] || '00:00:00')
end

#ovideo?true, false

Returns video final ok.

Returns:

  • (true, false)

    video final ok



42
43
44
45
46
47
48
# File 'lib/evideo/processa.rb', line 42

def ovideo?
  oinit
  return false if Video.to_t(otempo, 5) != Video.to_t(itempo, 5) || obitrate >= 3000 || oheight < 480

  puts("rm #{inome} #{oshow}")
  true
end

#posInteger

Returns posicao array pastas onde procurar videos finais.

Returns:

  • (Integer)

    posicao array pastas onde procurar videos finais



65
66
67
68
69
70
# File 'lib/evideo/vars2.rb', line 65

def pos
  # -1 #{ops[:d][0]}/#{ops[:i]}/#{ops[:o]}"
  #  0 #{ops[:d][pos]}/#{ops[:o]}"
  #  1 #{ops[:d][pos]}/#{ops[:o]}"
  @pos ||= -1
end

#processaObject

processa video - somente se necessario



11
12
13
14
15
# File 'lib/evideo/processa.rb', line 11

def processa
  return if ivideo? || opastas?

  work(cmd_mpeg("#{ops[:d][0]}/#{ops[:i]}/#{ops[:o]}/#{bas.downcase}"))
end

#work(cmd) ⇒ Object

executa/mostra comando mpeg

Parameters:

  • cmd (String)

    comando mpeg



19
20
21
22
23
24
25
26
# File 'lib/evideo/processa.rb', line 19

def work(cmd)
  if ops[:x]
    puts("processar #{inout}")
    system(cmd)
  else
    puts(cmd)
  end
end