Reading Videos

Video Reading

Note: Reading of audio streams is not yet implemented

Reading Video Files

VideoIO contains a simple high-level interface which allows reading of video frames from a supported video file (or from a camera device, shown later).

using VideoIO

#io = VideoIO.open(video_file)
io = VideoIO.testvideo("annie_oakley") # for testing purposes

f = VideoIO.openvideo(io)

img = read(f)

while !eof(f)
    read!(f, img)
    # Do something with frames
end
close(f)

Seeking through the video can be achieved via seek(f, seconds::Float64) and seekstart(f) to return to the start.

Base.seekFunction.
seek(s::VideoReader, seconds::AbstractFloat, seconds_min::AbstractFloat=-1.0,  seconds_max::AbstractFloat=-1.0, video_stream::Integer=1, forward::Bool=false)

Seek through VideoReader object.

source
seek(s::VideoReader, seconds::AbstractFloat, seconds_min::AbstractFloat=-1.0,  seconds_max::AbstractFloat=-1.0, video_stream::Integer=1, forward::Bool=false)

Seek through AVInput object.

source
Base.seekstartFunction.
seekstart(s::VideoReader, video_stream=1)

Seek to start of VideoReader object.

source
seekstart(avin::AVInput{T}, video_stream=1) where T <: AbstractString

Seek to start of AVInput object.

source

Changing the target pixel format for reading

It can be helpful to be explicit in which pixel format you wish to read frames as. Here a grayscale video is read and parsed into a Vector(Array{UInt8}}

f = VideoIO.openvideo(filename, target_format=VideoIO.AV_PIX_FMT_GRAY8)

while !eof(f)
    img = reinterpret(UInt8, read(f))
end
close(f)

Video Playback

A trivial video player interface exists (no audio) through Makie.jl. Note: Makie must be imported first to enable playback functionality.

using Makie
using VideoIO

f = VideoIO.testvideo("annie_oakley")  # downloaded if not available
VideoIO.playvideo(f)  # no sound

Customization of playback can be achieved by looking at the basic expanded version of this function:

import Makie
import VideoIO

#io = VideoIO.open(video_file)
io = VideoIO.testvideo("annie_oakley") # for testing purposes
f = VideoIO.openvideo(io)

img = read(f)
scene = Makie.Scene(resolution = reverse(size(img)))
makieimg = Makie.image!(scene, img, show_axis = false, scale_plot = true)[end]
Makie.rotate!(scene, -0.5pi)
display(scene)

while !eof(f)
    read!(f, img)
    makieimg[1] = img
    sleep(1/f.framerate)
end

This code is essentially the code in playvideo, and will read and (without the sleep) play a movie file as fast as possible.

Reading Camera Output

Frames can be read iteratively

using VideoIO
cam = VideoIO.opencamera()
for i in 1:100
    img = read(cam)
    sleep(1/framerate)
end

Webcam playback

The default system webcam can be viewed directly

using Makie
using VideoIO
VideoIO.viewcam()

An expanded version of this approach:

import Makie
import VideoIO

io = VideoIO.open(video_file)
f = VideoIO.openvideo(io)

img = read(f)
scene = Makie.Scene(resolution = size(img))
makieimg = Makie.image!(scene, img, show_axis = false, scale_plot = false)[end]
Makie.rotate!(scene, -0.5pi)
display(scene)

while !eof(f)
    read!(f, img)
    makieimg[1] = img
    sleep(1/f.framerate)
end

Video Properties & Metadata

get_start_time(file::String) -> DateTime

Return the starting date & time of the video file. Note that if the starting date & time are missing, this function will return the Unix epoch (00:00 1st January 1970).

source
get_time_duration(file::String) -> (DateTime, Microsecond)

Return the starting date & time as well as the duration of the video file. Note that if the starting date & time are missing, this function will return the Unix epoch (00:00 1st January 1970).

source
VideoIO.get_durationFunction.
get_duration(file::String) -> Float64

Return the duration of the video file in seconds (float).

source