Python视频处理库:scikit-video

Python视频处理库:scikit-video

 

安装

$ sudo pip install sk-video

视频的读写

读视频

import skvideo.io
import skvideo.datasets
import numpy as np 

# read video as a single ndarray
videodata = skvideo.io.vread(skvideo.datasets.bigbuckbunny())
print(videodata.shape)

# read video frame by frame
videogen = skvideo.io.vreader(skvideo.datasets.bigbuckbunny())
for frame in videogen:
    print(frame.shape)

# 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)

写视频

import skvideo.io
import skvideo.datasets
import numpy as np 

# write an ndarray to a video file
outputdata = np.random.random(size=(30,480,680,3)) * 255
outputdata = outputdata.astype(np.uint8)
skvideo.io.vwrite("outputvideo.mp4", outputdata)

# FFmpeg writer (报错)
outputdats = np.random.random(size=(5,480,680,3)) * 255
outputdata = outputdata.astype(np.uint8)
writer = skvideo.io.FFmpegWriter("outputvideoplus.mp4",(5,480,640,3))
for i in xrange(5):
    writer.writeFrame(outputdata[i,:,:,:])
writer.close()

ffmpeg写报错,writer = skvideo.io.FFmpegWriter("outputvideoplus.mp4",(5,480,640,3)),TypeError: 'tuple' object does not support item assignment

读视频元数据(metadata)

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))

运动

import skvideo.io 
import skvideo.motion
import skvideo.datasets

videodata = skvideo.io.vread(skvideo.datasets.bigbuckbunny(),num_frames=5)


motion = skvideo.motion.blockMotion(videodata)

print(videodata.shape)
print(motion.shape)

# compensate the video
compensate = skvideo.motion.blockComp(videodata, motion)

# write
skvideo.io.vwrite("compensate.mp4", compensate)

测量

import skvideo.io 
import skvideo.motion
import skvideo.datasets
import skvideo.measure

# compute vectors from bigbuckbunny
videodata = skvideo.io.vread(skvideo.datasets.bigbuckbunny(),num_frames=5,as_grey=True)
# videodata = skvideo.io.vread(skvideo.datasets.bigbuckbunny())
# skvideo.io.vwrite("origin.mp4", videodata)

motion = skvideo.motion.blockMotion(videodata)

print(videodata.shape)
print(motion.shape)

# compensate the video
compensate = skvideo.motion.blockComp(videodata, motion)

# write
skvideo.io.vwrite("compensate.mp4", compensate)

print(skvideo.measure.ssim(videodata,compensate))
print(skvideo.measure.psnr(videodata,compensate))
print(skvideo.measure.mse(videodata,compensate))

读写图片

import skvideo.io 

# a frame from the bigbuckbunny sequence
vid = skvideo.io.vread('/home/shuai/python_test/output.png')
T, M, N, C = vid.shape

print("Number of frames: %d" % (T,))
print("Number of rows: %d" % (M,))
print("Number of cols: %d" % (N,))
print("Number of channels: %d" % (C,))

# upsacle by a factor of 2
vid = skvideo.io.vread('/home/shuai/python_test/output.png',
                        outputdict={
                            "-sws_flags":"bilinear",
                            "-s":"2560x1440"
                        })
T, M, N, C = vid.shape

print("Number of frames: %d" % (T,))
print("Number of rows: %d" % (M,))
print("Number of cols: %d" % (N,))
print("Number of channels: %d" % (C,))

skvideo.io.vwrite("outputplus.png", vid)

你可能感兴趣的:(python)