Class: EasyVideoUtils
- Inherits:
-
Object
- Object
- EasyVideoUtils
- Extended by:
- CommandHelper
- Defined in:
- lib/easyvideo_utils.rb
Instance Method Summary collapse
-
#add_audio(audio_file, show: false) ⇒ Object
Add an audio track to a video which contains no audio.
-
#add_subtitles(file, show: false) ⇒ Object
To add subtitles, supply a .srt file.
- #capture_desktop(show: false) ⇒ Object
-
#concat(files = @files_in, show: false) ⇒ Object
Concatenate 2 or more videos.
-
#duration ⇒ Object
Duration returned in seconds.
-
#extract_images(show: false, ext: '.png', rate: 1) ⇒ Object
Extract images from the video.
-
#initialize(vid_in = nil, vid_out = 'video.mp4', out: vid_out, working_dir: '/tmp', debug: false) ⇒ EasyVideoUtils
constructor
A new instance of EasyVideoUtils.
- #play(show: false) ⇒ Object
- #preview(show: false) ⇒ Object
- #remove_audio(show: false) ⇒ Object
-
#remove_video(show: false) ⇒ Object
removes the video track which leaves the audio track e.g.
-
#resize(scale = '720', show: false) ⇒ Object
(also: #scale)
Resize avi to 720p.
-
#slowdown(speed = :x2, show: false) ⇒ Object
slow down a video.
-
#speedup(speed = :x2, show: false) ⇒ Object
speed up a video.
-
#transcode(show: false) ⇒ Object
(also: #convert)
Transcodes avi -> mp4.
-
#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.
Methods included from CommandHelper
Constructor Details
#initialize(vid_in = nil, vid_out = 'video.mp4', out: vid_out, working_dir: '/tmp', debug: false) ⇒ EasyVideoUtils
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/easyvideo_utils.rb', line 57 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
72 73 74 75 76 77 78 |
# File 'lib/easyvideo_utils.rb', line 72 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
82 83 84 85 86 87 88 |
# File 'lib/easyvideo_utils.rb', line 82 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
90 91 92 93 94 95 96 |
# File 'lib/easyvideo_utils.rb', line 90 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
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/easyvideo_utils.rb', line 104 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 |
#duration ⇒ Object
Duration returned in seconds
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/easyvideo_utils.rb', line 119 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.
144 145 146 147 |
# File 'lib/easyvideo_utils.rb', line 144 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
149 150 151 152 |
# File 'lib/easyvideo_utils.rb', line 149 def play(show: false) command = "mplayer #{@file_out}" run command, show end |
#preview(show: false) ⇒ Object
154 155 156 157 |
# File 'lib/easyvideo_utils.rb', line 154 def preview(show: false) command = "ffplay #{@file_out}" run command, show end |
#remove_audio(show: false) ⇒ Object
159 160 161 162 |
# File 'lib/easyvideo_utils.rb', line 159 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
167 168 169 170 |
# File 'lib/easyvideo_utils.rb', line 167 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
174 175 176 177 |
# File 'lib/easyvideo_utils.rb', line 174 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
186 187 188 189 190 191 192 193 |
# File 'lib/easyvideo_utils.rb', line 186 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
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/easyvideo_utils.rb', line 200 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
213 214 215 216 217 218 |
# File 'lib/easyvideo_utils.rb', line 213 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’
225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/easyvideo_utils.rb', line 225 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 |