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.
-
#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.
- #create_slideshow(ext: '.jpg', image_duration: 5, show: false) ⇒ Object
-
#duration ⇒ Object
Duration returned in seconds.
-
#extract_images(show: false, ext: '.png', rate: 1) ⇒ Object
Extract images from the video.
- #grab_image(start_time = 1, show: false) ⇒ Object
-
#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 = '320x240', show: false) ⇒ Object
(also: #scale)
Resize e.g.
-
#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, duration, show: false) ⇒ Object
Trim the start and end of the video times can be 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
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/easyvideo_utils.rb', line 67 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
82 83 84 85 86 87 88 |
# File 'lib/easyvideo_utils.rb', line 82 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
92 93 94 95 96 97 98 |
# File 'lib/easyvideo_utils.rb', line 92 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
100 101 102 103 104 105 106 |
# File 'lib/easyvideo_utils.rb', line 100 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
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/easyvideo_utils.rb', line 114 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
129 130 131 132 133 134 |
# File 'lib/easyvideo_utils.rb', line 129 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
136 137 138 139 140 141 142 143 |
# File 'lib/easyvideo_utils.rb', line 136 def (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 |
#duration ⇒ Object
Duration returned in seconds
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/easyvideo_utils.rb', line 147 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.
172 173 174 175 |
# File 'lib/easyvideo_utils.rb', line 172 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 |
#grab_image(start_time = 1, show: false) ⇒ Object
177 178 179 180 |
# File 'lib/easyvideo_utils.rb', line 177 def grab_image(start_time=1, show: false) command = "avconv -i #{@file_in} -ss #{start_time.to_s} -frames:v 1 #{@file_out} -y" run command, show end |
#play(show: false) ⇒ Object
182 183 184 185 |
# File 'lib/easyvideo_utils.rb', line 182 def play(show: false) command = "mplayer #{@file_out}" run command, show end |
#preview(show: false) ⇒ Object
187 188 189 190 |
# File 'lib/easyvideo_utils.rb', line 187 def preview(show: false) command = "ffplay #{@file_out}" run command, show end |
#remove_audio(show: false) ⇒ Object
192 193 194 195 |
# File 'lib/easyvideo_utils.rb', line 192 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
200 201 202 203 |
# File 'lib/easyvideo_utils.rb', line 200 def remove_video(show: false) command = "ffmpeg -i #{@file_in} -vn #{@file_out}" run command, show end |
#resize(scale = '320x240', show: false) ⇒ Object Also known as: scale
Resize e.g. to 320x240
207 208 209 210 |
# File 'lib/easyvideo_utils.rb', line 207 def resize(scale='320x240', show: false) command = "avconv -i #{@file_in} -s #{scale} #{@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
220 221 222 223 224 225 226 227 |
# File 'lib/easyvideo_utils.rb', line 220 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
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/easyvideo_utils.rb', line 234 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
247 248 249 250 251 252 |
# File 'lib/easyvideo_utils.rb', line 247 def transcode(show: false) command = "ffmpeg -i #{@file_in} #{@file_out} -y" run command, show end |
#trim(start_time, duration, show: false) ⇒ Object
Trim the start and end of the video times can be expressed in human time format e.g. ‘1m 4s’, ‘2m 30’
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/easyvideo_utils.rb', line 259 def trim(start_time, duration, show: false) t1 = "%02d:%02d:%02d" % (start_time.to_s.sub(/m/,'\00s').split(/\D/)\ .reverse + [0,0]).take(3).reverse command = "avconv -i #{@file_in} -ss #{t1} -t #{duration.to_s} " + "-codec copy #{@file_out}" run command, show end |