Primarily to extract video frames from streams and files for processing by AI.
-
Add the dependency to your
shard.yml
:dependencies: ffmpeg: github: spider-gazelle/ffmpeg
-
Run
shards install
-
ensure the required libraries are installed
sudo apt install libswscale-dev
sudo apt install libav-tools
orsudo apt install ffmpeg
parsing a video file, outputs StumpyCore Canvas
require "ffmpeg"
video = Video::File.new("./test.mp4")
video.each_frame do |frame|
puts "key frame? #{frame.key_frame?}"
frame # => FFmpeg::Frame
frame.to_canvas # => StumpyCore::Canvas
end
also supports UDP streams (unicast or multicast)
require "ffmpeg"
video = Video::UDP.new("udp://239.0.0.2:1234")
video.each_frame do |frame|
frame # => FFmpeg::Frame
frame.to_canvas # => StumpyCore::Canvas
end
You can also scale the frames
require "ffmpeg"
video = Video::File.new("./test.mp4")
# the scaler context
scaler = uninitialized FFmpeg::SWScale
# scaled frame we'll use for storing the scaling output
scaled_frame = uninitialized FFmpeg::Frame
video.on_codec do |codec|
# scale by 50%
width = codec.width // 2
height = codec.height // 2
scaler = FFmpeg::SWScale.new(codec, width, height, codec.pixel_format)
scaled_frame = FFmpeg::Frame.new width, height, codec.pixel_format
end
video.each_frame do |frame|
scaler.scale frame, scaled_frame
# do something with the scaled frame
scaled_frame.to_canvas
end
See the specs for more detailed usage
- Fork it (https://github.com/spider-gazelle/ffmpeg/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Stephen von Takach - creator and maintainer