python视频处理框架_python调用ffmpeg处理视频-提取图片

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值