Video processing
In this demo, let's see what you can do with a video using Engee. To do this, let's work with a video clip taken from the Internet. The video is presented below.
Pkg.add(["VideoIO", "StackViews", "ImageFiltering", "ImageShow"])
Pkg.add("VideoIO") # Video Stream Processing Library
using VideoIO # Video Processing Library
using ImageShow # Image Rendering Library
using StackViews # Set Processing Library
using ImageFiltering # Image Filtering Library
Next, using the VideoIO.load function, we load the file name into memory as a vector of image arrays. And using StackView, we will perform vector transformations into a multidimensional matrix.
path = @__DIR__ # Returns the path to the folder opened in the file manager
path_video = "$path/input.mp4"
vid = VideoIO.load( path_video )
Stack = StackView(vid)# Assembling an image vector into a matrix
simshow(Stack[:,:,1])# Rendering the first frame of the video
From the resulting matrix, we can calculate the frame rate, knowing the time of the video and the number of frames in the video.
The get_time_duration function returns the start date and time, as well as the duration of the video file. Please note: if the date and time are missing, the function returns 00:00 on January 1, 1970.
We can also find out the dimensions of the frames and their number using the size function for the image stack.
get_time = VideoIO.get_time_duration(path_video)
print("All size video: " * string(size(Stack)))
print("FPS: " * string(size(Stack,3)/floor(get_time[2])))
Let's crop the video, leaving only the central part of it.
new_video = Stack[1:360,1:640,:]
for i in 1:size(Stack,3)
new_video[:,:,i] = Stack[180:180+359,320:320+639,i]
end
simshow(new_video[:,:,1])# Rendering the first frame of the video
Now, for the first 100 frames of the cropped video, add filtering to create a blur effect.
for i in 1:100
new_video[:,:,i] = imfilter(new_video[:,:,i], Kernel.gaussian(3))
end
simshow(new_video[:,:,1])# Rendering the first frame of the video
If we want to save our data in a video, then there is an option to directly save the stack of images in the form of a video file. At the same time, we can independently adjust the encoder parameters and frame rate. The list of available settings is shown in the table below.
video = [zeros(eltype(new_video), size(new_video, 1), size(new_video, 2)) for _ in axes(new_video, 3)]
@inbounds for k in axes(new_video, 3)
video[k] = new_video[:,:,k]
end
| Goal | encoder_options |
|---|---|
| Perceptual compression, default is h264. The best solution for most cases | (crf=23, preset="medium") |
| Lossless compression. Fastest, largest file size | (crf=0, preset="ultrafast") |
| Lossless compression. Slowest, minimum file size | (crf=0, preset="veryslow") |
| Direct control of bitrate and frame rate | (bit_rate = 400000, gop_size = 10, max_b_frames = 1) |
encoder_options = (crf=23, preset="medium")
VideoIO.save("video.mp4", video, framerate=30, encoder_options=encoder_options)
Conclusion
In this demo, we looked at the possibilities of video processing in Engee and used illustrative examples to make sure that the environment works successfully with video. The technique is the same as for image processing.


