批量处理视频时,经常需要事先判断视频是否损坏,并且将视频帧存储为图片。
这里使用matlab做一个简单的判断。
主要用到VideoReader
和VideoWriter
两个方法。
1 准备工作
matlab如果遇到下面这个错误:
Could not read file due to an unexpected error. Reason: Unable to initialize the video obtain properties
只需要运行下面命令安装相关依赖项即可解决(对Ubuntu 14有效,其他版本效果未知):
wget http://ppa.launchpad.net/mc3man/gstffmpeg-keep/ubuntu/pool/main/g/gstreamer0.10-ffmpeg/gstreamer0.10-ffmpeg_0.10.13-5ubuntu1~trusty2.1_amd64.deb
sudo dpkg -i gstreamer0.10-ffmpeg_0.10.13-5ubuntu1~trusty2.1_amd64.deb
2 主要代码
try
obj = VideoReader('filename'); %% 读取视频帧
numFrames = obj.NumberOfFrames;
writerObj=VideoWriter('output_video_filename'); %%打开一个视频,准备写入
open(writerObj);
for k = 1: numFrames % 遍历所有帧
frame = read(obj,k);
if(mod(k,10)==1) %%隔10帧存一次
imwrite(frame,'output_frame_filename','png');% 保存帧
end
writeVideo(writerObj,frame); % 把图像存入视频文件中
end
close(writerObj);
catch
display(’got an error‘);
end
3 实现更多
基本的代码逻辑如上,至于两个函数更加详细的用法,还要借助matlab的help