Class: EasyVideoUtils

Inherits:
Object
  • Object
show all
Extended by:
CommandHelper
Defined in:
lib/easyvideo_utils.rb

Instance Method Summary collapse

Methods included from CommandHelper

list, search

Constructor Details

#initialize(vid_in = nil, vid_out = 'video.mp4', out: vid_out, working_dir: '/tmp', debug: false) ⇒ EasyVideoUtils

Returns a new instance of EasyVideoUtils.



50
51
52
53
54
55
# File 'lib/easyvideo_utils.rb', line 50

def initialize(vid_in=nil, vid_out='video.mp4', out: vid_out, 
               working_dir: '/tmp', debug: false)

  @file_in, @file_out, @working_dir, @debug = vid_in, out, working_dir, debug

end

Instance Method Details

#add_audio(audio_file, show: false) ⇒ Object

Add an audio track to a video which contains no audio



59
60
61
62
63
64
65
# File 'lib/easyvideo_utils.rb', line 59

def add_audio(audio_file, show: false)

  command = "ffmpeg -i #{@file_in} -i #{audio_file} -codec copy " + 
            "-shortest #{@file_out} -y"
  run command, show

end

#add_subtitles(file, show: false) ⇒ Object

To add subtitles, supply a .srt file



69
70
71
72
73
74
75
# File 'lib/easyvideo_utils.rb', line 69

def add_subtitles(file, show: false)
     
  command = "ffmpeg -i #{@file_in} -i #{file} -c copy -c:s mov_" + 
            "text #{@file_out} -y"
  run command, show
  
end

#capture_desktop(show: false) ⇒ Object



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

def capture_desktop(show: false)

  command = "ffmpeg -video_size 1024x768 -framerate 25 -f x11grab -i " + 
  ":0.0+0,0 #{@file_out}"
  run command, show

end

#durationObject

Duration returned in seconds



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

def duration()
  
  s = `exiftool #{@file_in}`
  puts 's: ' + s.inspect if @debug
  r = s[/Duration.*(\d{1,2}:\d{2}:\d{2})/,1]

  puts 'r: ' + r.inspect if @debug    
  
  if r then
    a = r.split(':').map(&:to_i)
    return Subunit.new(units={minutes:60, hours:60, seconds: 0}, a).to_i
  end
  
  puts 'r: ' + r.inspect if @debug
  s[/Duration.*: (\d+\.\d+) s/,1].to_f

end

#play(show: false) ⇒ Object



105
106
107
108
# File 'lib/easyvideo_utils.rb', line 105

def play(show: false)
  command = "mplayer #{@file_out}"
  run command, show
end

#resize(show: false) ⇒ Object Also known as: scale

Resize avi to 720p



112
113
114
# File 'lib/easyvideo_utils.rb', line 112

def resize(show: false)
  `ffmpeg -i #{@file_in} -vf scale="720:-1" #{@file_out} -y`
end

#transcode(show: false) ⇒ Object Also known as: convert

Transcodes avi -> mp4



120
121
122
123
124
125
# File 'lib/easyvideo_utils.rb', line 120

def transcode(show: false)
  
  command = "ffmpeg -i #{@file_in} #{@file_out} -y"
  run command, show    

end

#trim(start_time, end_time, show: false) ⇒ Object

Trim the start and end of the video times are expressed in human time format e.g. ‘1m 4s’, ‘2m 30’



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/easyvideo_utils.rb', line 132

def trim(start_time, end_time, show: false)
      
  t1, t2 = [start_time, end_time].map do |s|

    "%02d:%02d:%02d" % (s.sub(/m/,'\00s').split(/\D/).reverse + [0,0])\
                        .take(3).reverse

  end
  
  command = "ffmpeg -i #{@file_in} -ss #{t1} -t #{t2} -async 1 " + 
            "#{@file_out} -y"
  run command, show
  
end