Scikit-video is a Python video processing library that makes it easy to call a variety of video processing algorithms

Read and write video

Skvideo. IO uses FFmpeg/LibAV as the back-end video read and write module. Using the available backend, it parses the video metadata with the appropriate probe tools (FFProbe, AvProbe, or even Mediainfo).

1. Read the video

Use skVideo.io. Vread to load any video data into memory as a single NDARry (here is Bigbuckbunny video). Note that this function assumes you have plenty of memory, so try to use small videos.

import skvideo.io
import skvideo.datasets
videodata = skvideo.io.vread(skvideo.datasets.bigbuckbunny())
print(videodata.shape)
Copy the code

Running this code prints:

(132, 720, 1280, 3)
Copy the code

You can load any video frame by frame using skVideo.io. Vreader (here is Bigbuckbunny video). This function can be used to load large files, and it is usually faster than loading the video as a separate Ndarry. But it needs to process each frame at load time. The following code snippet:

import skvideo.io
import skvideo.datasets
videogen = skvideo.io.vreader(skvideo.datasets.bigbuckbunny())
for frame in videogen:
        print(frame.shape)
Copy the code

Its output is:

(720, 1280, 3) (720, 1280, 3)... . . (720, 1280, 3)Copy the code

Sometimes specific use cases need to fine tune the read parameters of FFmpeg. You can use skVideo.io.FFmpegReader

import skvideo.io
import skvideo.datasets

# here you can set keys and values for parameters in ffmpeg
inputparameters = {}
outputparameters = {}
reader = skvideo.io.FFmpegReader(skvideo.datasets.bigbuckbunny(),
                inputdict=inputparameters,
                outputdict=outputparameters)

# iterate through the frames
accumulation = 0
for frame in reader.nextFrame():
        # do something with the ndarray frame
        accumulation += np.sum(frame)
Copy the code

For example, FFmpegReader returns RGB by default, but sometimes if you want to use other color Spaces, you can set appropriate key/values values in outputParameters. Since the FFmpeg output is imported into STdin, you can use all the FFmpeg instructions there. Inputparameters are useful for raw video signals with no headers.

2. Write a video

To write an ndarry as a video file you can use skVideo.io.write

import skvideo.io
import numpy as np

outputdata = np.random.random(size=(5, 480, 680, 3)) * 255
outputdata = outputdata.astype(np.uint8)

skvideo.io.vwrite("outputvideo.mp4", outputdata)
Copy the code

Usually writing video requires fine-tuning of FFmpeg write parameters to select encoder, frame rate, bit rate, etc. You can do this using skVideo.io.FFmpegWriter.

import skvideo.io
import numpy as np

outputdata = np.random.random(size=(5, 480, 680, 3)) * 255
outputdata = outputdata.astype(np.uint8)

writer = skvideo.io.FFmpegWriter("outputvideo.mp4")
for i in xrange(5):
        writer.writeFrame(outputdata[i, :, :, :])
writer.close()
Copy the code

3. Read the video meta information

Use skVideo.io. Ffprobe to read video meta information. As follows:

import skvideo.io
import skvideo.datasets
import json
metadata = skvideo.io.ffprobe(skvideo.datasets.bigbuckbunny())
print(metadata.keys())
print(json.dumps(metadata["video"], indent=4))
Copy the code

Skvideo.io. Ffprobe returns a dictionary that can be passed to json.dumps for better display. The output is as follows:

[u'audio', u'video'] { "@index": "0", "@codec_name": "h264", "@codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10", "@profile": "Main", "@codec_type": "video", "@codec_time_base": "1/50", "@codec_tag_string": "avc1", "@codec_tag": "0x31637661", "@width": "1280", "@height": "720", "@coded_width": "1280", "@coded_height": "720", "@has_b_frames": "0", "@sample_aspect_ratio": "1:1", "@display_aspect_ratio": "16:9", "@pix_fmt": "yuv420p", "@level": "31", "@chroma_location": "left", "@refs": "1", "@is_avc": "1", "@nal_length_size": "4", "@r_frame_rate": "25/1", "@avg_frame_rate": "25/1", "@time_base": "1/12800", "@ start_pts" : "0", "@ start_time" : "0.000000", "@ duration_ts" : "67584", "@ duration" : "5.280000", "@ bit_rate" : "1205959", "@bits_per_raw_sample": "8", "@nb_frames": "132", "disposition": { "@default": "1", "@dub": "0", "@original": "0", "@comment": "0", "@lyrics": "0", "@karaoke": "0", "@forced": "0", "@hearing_impaired": "0", "@visual_impaired": "0", "@clean_effects": "0", "@attached_pic": "0" }, "tag": [ { "@key": "creation_time", "@value": "1970-01-01 00:00:00" }, { "@key": "language", "@value": "und" }, { "@key": "handler_name", "@value": "VideoHandler" } ] }Copy the code