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.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/easyvideo_utils.rb', line 59

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

  @file_out, @working_dir, @debug = out, working_dir, debug
  
  if vid_in.is_a? String then
    @file_in = vid_in
  elsif vid_in.is_a? Array
    @files_in = vid_in
  end

end

Instance Method Details

#add_audio(audio_file, show: false) ⇒ Object

Add an audio track to a video which contains no audio



74
75
76
77
78
79
80
# File 'lib/easyvideo_utils.rb', line 74

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



84
85
86
87
88
89
90
# File 'lib/easyvideo_utils.rb', line 84

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



92
93
94
95
96
97
98
# File 'lib/easyvideo_utils.rb', line 92

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

#concat(files = @files_in, show: false) ⇒ Object

Concatenate 2 or more videos

Notes:

  • Video must be of same format and codec

  • Only video with no audio is currently supported



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/easyvideo_utils.rb', line 106

def concat(files=@files_in, show: false)
  
  inputs = files.map {|file| "-i #{file}"}.join(' ')
  filter = files.map.with_index {|file,i| "[%s:v]" % i}.join(' ')
  
  command = "ffmpeg #{inputs} \ " + 
  " -filter_complex \"#{filter} concat=n=#{files.length}:v=1 [v]\" \ " + 
  " -map \"[v]\"  #{@file_out}"

  run command, show
  
end

#create_poster(audiox = nil, imagex = nil, image: imagex, audio: audiox, show: false) ⇒ Object

creates a video from an audio file along with a single image file



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

def create_poster(audiox=nil, imagex=nil, image: imagex, 
                  audio: audiox, show: false)
  command = "ffmpeg -loop 1 -i #{image} -i #{audio} -c:v libx264 -c:a " + 
      "aac -strict experimental -b:a 192k -shortest #{@file_out}"
  run command, show
end

#create_slideshow(ext: '.jpg', image_duration: 5, show: false) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/easyvideo_utils.rb', line 128

def create_slideshow(ext: '.jpg', image_duration: 5, show: false)
  
  file_mask = @file_in || "image-%03d" + ext
  command = "ffmpeg -r 1/#{image_duration} -i #{file_mask} -c:v " + 
      "libx264 -r 30 -pix_fmt yuv420p #{@file_out}"  
  run command, show
  
end

#durationObject

Duration returned in seconds



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/easyvideo_utils.rb', line 139

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

#extract_images(show: false, ext: '.png', rate: 1) ⇒ Object

Extract images from the video

switches used:

-r – Set the frame rate. I.e the number of frames to be extracted into images per second. -f – Indicates the output format i.e image format in our case.



164
165
166
167
# File 'lib/easyvideo_utils.rb', line 164

def extract_images(show: false, ext: '.png', rate: 1)
  command = "ffmpeg -i #{@file_in} -r #{rate} -f image2 image-%2d#{ext}"
  run command, show
end

#play(show: false) ⇒ Object



169
170
171
172
# File 'lib/easyvideo_utils.rb', line 169

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

#preview(show: false) ⇒ Object



174
175
176
177
# File 'lib/easyvideo_utils.rb', line 174

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

#remove_audio(show: false) ⇒ Object



179
180
181
182
# File 'lib/easyvideo_utils.rb', line 179

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

#remove_video(show: false) ⇒ Object

removes the video track which leaves the audio track e.g. ffmpeg -i input.mp4 -vn output.mp3



187
188
189
190
# File 'lib/easyvideo_utils.rb', line 187

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

#resize(scale = '720', show: false) ⇒ Object Also known as: scale

Resize avi to 720p



194
195
196
197
# File 'lib/easyvideo_utils.rb', line 194

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

#slowdown(speed = :x2, show: false) ⇒ Object

slow down a video

note: presentation timestamp (PTS) ‘x2’ = half speed; ‘x4’ = quarter speed



206
207
208
209
210
211
212
213
# File 'lib/easyvideo_utils.rb', line 206

def slowdown(speed=:x2, show: false)
  
  factor = {x1_5: 1.5, x2: 2.0, x4: 4.0}[speed.to_s.sub('.','_').to_sym]
  command = "ffmpeg -i #{@file_in} -vf \"setpts=#{factor}*PTS\" " + 
      "#{@file_out} -y"
  run command, show
  
end

#speedup(speed = :x2, show: false) ⇒ Object

speed up a video

note: presentation timestamp (PTS) ‘x2’ = double speed; ‘x4’ = quadruple speed



220
221
222
223
224
225
226
227
228
229
# File 'lib/easyvideo_utils.rb', line 220

def speedup(speed=:x2, show: false)
  
  h = {x1_5: 0.75, x2: 0.5, x4: 0.25, x6: 0.166, x8: 0.125, x16: 0.0625, 
       x32: 0.03125, x64: 0.015625}
  factor = h[speed.to_s.sub('.','_').to_sym]
  command = "ffmpeg -i #{@file_in} -vf \"setpts=#{factor}*PTS\" " + 
      "#{@file_out} -y"
  run command, show
  
end

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

Transcodes avi -> mp4



233
234
235
236
237
238
# File 'lib/easyvideo_utils.rb', line 233

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’



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/easyvideo_utils.rb', line 245

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