python调用ffmpeg处理视频-提取图片
2019-03-12 本文已影响0人
洗洗睡吧i
ffmpeg 使用说明
# usage:
ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
# options:
-ss: set the start time offset
-f: force format
-t: record or transcode "duration" seconds of audio/video
-r set frame rate (Hz value, fraction or abbreviation)
-y: overwrite output files
遍历文件夹中的所有视频文件, 提取特定时间点的图片
import os
# 提取特定时间点(第12s)的图片
# ffmpeg -ss 0:0:12 -i xxx.mp4 -t 0.01 -f image2 yyy.jpg -y
str = 'c:/users/xxxx/ffmpeg/ffmpeg.exe ' +
'-ss 0:0:12 -i {} -t 0.01 -f image2 d:/images/{}.jpg -y'
for parent, dirnames, filenames in os.walk('d:/videos/'):
for filename in filenames:
video_dir = os.path.join(parent, filename)
str_cmd = str.format(video_dir, filename)
print(str_cmd)
os.popen(str_cmd)
定时提取视频中的图片
import os
# 第12s之后每隔1s钟,定时提取视频中的图片
# ffmpeg -ss 0:0:12 -i xxx.mp4 -r 1 -f image2 yyy_%03d.jpg -y
str = 'c:/users/xxxx/ffmpeg/ffmpeg.exe ' +
'-ss 0:0:12 -i xxx.mp4 -r 1 -f image2 d:/images/{}_%03d.jpg -y'
str_cmd = str
print(str_cmd)
os.popen(str_cmd)
定时提取视频中的图片,并截取需要的区域
import os
import time
import cv2
import matplotlib.pyplot as plt
ss = 'c:/users/xxxx/ffmpeg/ffmpeg.exe ' +
'-ss {} -i xxx.mp4 -t 0.01 -f image2 d:/images/{}.jpg -y'
for sec in range(60):
cmd = ss.format(str(sec), str(sec))
print(cmd)
os.popen(cmd)
time.sleep(0.5)
for i in range(60):
img = cv2.imread('d:/images/{}.jpg'.format(str(i)), 0)
cut = img[803:835, 533:595]
# 二值化
# _, cut1 = cv2.threshold(cut, 50, 220, cv2.THRESH_BINARY)
cv2.imwrite('d:/cuts/{}.jpg'.format(str(i)), cut)
plt.subplot(6, 10, i+1)
plt.imshow(cut)
# plt.title(i)
plt.xticks([]), plt.yticks([])
# plt.savefig('yyy.png', dpi=300)
plt.show()