python调用系统ffmpeg实现视频截图、http发送


Posted in Python onMarch 06, 2018

python 调用系统ffmpeg进行视频截图,并进行图片http发送ffmpeg ,视频、图片的各种处理。 

最近在做视频、图片的版权等深度学习识别,用到了ffmpeg部分功能,功能如下: 
调用ffmpeg 对不同目录视频进行截图,通过http发送到后台进行算法识别。 
每5分钟扫描最近的视频,生成图片,发送完毕图片删除。 

代码如下:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
"""'定时任务每五分钟发送上一个5分钟视频 
 目标视频:10.1.1.25 /usr/local/checkVideo  audited、auditing、black、white 
 find 
 """ 
import linecache 
import os 
import os.path 
import requests 
import time 
import datetime 
import sys 
reload(sys) 
sys.setdefaultencoding('utf8') 
 
#openAPI现网配置 
url='http://***/nudityRecog' 
app_key = '***' 
access_token = '***' 
imagedir='/opt/tomcat_api/video_sendto_api/image/' 
 
audited_dir='/usr/local/checkVideo/audited' 
auditing_dir='/usr/local/checkVideo/auditing' 
black_dir='/usr/local/checkVideo/black' 
white_dir='/usr/local/checkVideo/white' 
 
#时间差5分钟执行一次 
subtime=300 
 
#生成审核中截图 
def create_auditing_image(auditing_dir): 
 #扫描视频目录生成截图 
 for parent, dirnames, filenames in os.walk(auditing_dir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 
  for filename in filenames: # 输出文件信息 
   video_path = os.path.join(parent, filename) # 输出文件路径信息 
   filePath = unicode(video_path, 'utf8') #中文编码 
   filetime= os.path.getmtime(filePath) #获取修改时间 
   localtime=time.time() #获取当前系统时间 
   t=localtime-filetime #两者差值 
   #判断差值是否小于300s 
   if t<=subtime: 
    print t,filePath 
    filename=unicode(filename, 'utf8') #下载视频名称 
    #生成视频md5 
    str_md5= 'md5sum'+' ' + filePath +' '+ "| awk '{print $1}'" 
    video_md5 = os.popen(str_md5).readline(32) 
    print filePath,video_md5 
    #拼接截图命令行, 
    str_video= "ffmpeg -ss 0:1:00 -i "+" " +filePath + " "+"-r 0.01 -f image2 "+imagedir+video_md5+"-image-%5d.jpeg" 
    images = os.popen(str_video) # 调用命令行生成截图 
    print str_video 
 
#生成审核完截图 
def create_audited_image(audited_dir): 
 #扫描视频目录生成截图 
 for parent, dirnames, filenames in os.walk(audited_dir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 
  for filename in filenames: # 输出文件信息 
   video_path = os.path.join(parent, filename) # 输出文件路径信息 
   filePath = unicode(video_path, 'utf8') #中文编码 
   filetime= os.path.getmtime(filePath) #获取修改时间 
   localtime=time.time() #获取当前系统时间 
   t=localtime-filetime #两者差值 
   #判断差值是否小于300s 
   if t<=subtime: 
    print t,filePath 
    filename=unicode(filename, 'utf8') #下载视频名称 
    #生成视频md5 
    str_md5= 'md5sum'+' ' + filePath +' '+ "| awk '{print $1}'" 
    video_md5 = os.popen(str_md5).readline(32) 
    #拼接命令行, 
    str_video= "ffmpeg -ss 0:1:00 -i "+" " +filePath + " "+"-r 0.01 -f image2 "+imagedir+video_md5+"-image-%5d.jpeg" 
    images = os.popen(str_video) # 调用命令行生成截图 
    print str_video 
 
#生成黑名单截图 
def create_black_image(black_dir): 
 #扫描视频目录生成截图 
 for parent, dirnames, filenames in os.walk(black_dir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 
  for filename in filenames: # 输出文件信息 
   video_path = os.path.join(parent, filename) # 输出文件路径信息 
   filePath = unicode(video_path, 'utf8') #中文编码 
   filetime= os.path.getmtime(filePath) #获取修改时间 
   localtime=time.time() #获取当前系统时间 
   t=localtime-filetime #两者差值 
   #判断差值是否小于300s 
   if t<=subtime: 
    print t,filePath 
    filename=unicode(filename, 'utf8') #下载视频名称 
    #生成视频md5 
    str_md5= 'md5sum'+' ' + filePath +' '+ "| awk '{print $1}'" 
    video_md5 = os.popen(str_md5).readline(32) 
    #拼接命令行, 
    str_video= "ffmpeg -ss 0:1:00 -i "+" " +filePath + " "+"-r 0.01 -f image2 "+imagedir+video_md5+"-image-%5d.jpeg" 
    images = os.popen(str_video) # 调用命令行生成截图 
    print str_video 
 
 
#生成白名单截图 
def create_white_image(white_dir): 
 #扫描视频目录生成截图 
 for parent, dirnames, filenames in os.walk(white_dir): # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 
  for filename in filenames: # 输出文件信息 
   video_path = os.path.join(parent, filename) # 输出文件路径信息 
   filePath = unicode(video_path, 'utf8') #中文编码 
   filetime= os.path.getmtime(filePath) #获取修改时间 
   localtime=time.time() #获取当前系统时间 
   t=localtime-filetime #两者差值 
   #判断差值是否小于300s 
   if t<=subtime: 
    print t,filePath 
    filename=unicode(filename, 'utf8') #下载视频名称 
    #生成视频md5 
    str_md5= 'md5sum'+' ' + filePath +' '+ "| awk '{print $1}'" 
    video_md5 = os.popen(str_md5).readline(32) 
    #拼接命令行, 
    str_video= "ffmpeg -ss 0:1:00 -i "+" " +filePath + " "+"-r 0.01 -f image2 "+imagedir+video_md5+"-image-%5d.jpeg" 
    images = os.popen(str_video) # 调用命令行生成截图 
    print str_video 
#发送图片进程 
def send_image(imagedir): 
 #扫描图片路径 
 for img_parent, img_dir_names, img_names in os.walk(imagedir): 
  for img_name in img_names: 
   image = os.path.join(img_parent, img_name) #拼接图片完整路径 
   print time.strftime("%Y-%m-%d %X"), image 
   #准备发送图片 
   file = dict(file=open(image, 'rb')) 
   post_data = {'mark': 'room-201', 'timestamp': 1846123456, 'random': 123} 
   headers = {'app_key': app_key, 'access_token': access_token} 
   result = requests.post(url, files=file, data=post_data, headers=headers, verify=False) 
   print result.content 
   #删除发送的图片 
   str_img = "rm -f " + " " + image 
   del_img = os.popen(str_img).readline() 
   print del_img 
 
if __name__ == "__main__": 
 #create_auditing_image(auditing_dir) 
 #create_audited_image(audited_dir) 
 #create_black_image(black_dir) 
 #create_white_image(white_dir) 
 send_image(imagedir)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
web.py在SAE中的Session问题解决方法(使用mysql存储)
Jun 24 Python
Python选课系统开发程序
Sep 02 Python
Python使用filetype精确判断文件类型
Jul 02 Python
Python3一行代码实现图片文字识别的示例
Jan 15 Python
python基于itchat模块实现微信防撤回
Apr 29 Python
解决python flask中config配置管理的问题
Jul 26 Python
python web框架中实现原生分页
Sep 08 Python
python 内置函数汇总详解
Sep 16 Python
python入门之基础语法学习笔记
Feb 08 Python
深入浅析python的第三方库pandas
Feb 13 Python
Python用5行代码实现批量抠图的示例代码
Apr 14 Python
mac系统下安装pycharm、永久激活、中文汉化详细教程
Nov 24 Python
Python从零开始创建区块链
Mar 06 #Python
Django 实现下载文件功能的示例
Mar 06 #Python
python入门前的第一课 python怎样入门
Mar 06 #Python
详解Python判定IP地址合法性的三种方法
Mar 06 #Python
Python中enumerate()函数编写更Pythonic的循环
Mar 06 #Python
python距离测量的方法
Mar 06 #Python
Python入门之后再看点什么好?
Mar 05 #Python
You might like
PHP的autoload自动加载机制使用说明
2010/12/28 PHP
php实现斐波那契数列的简单写法
2014/07/19 PHP
PHP三种方式实现链式操作详解
2017/01/21 PHP
个人总结的一些关于String、Function、Array的属性和用法
2007/01/10 Javascript
js 操作符实例代码
2009/10/24 Javascript
js获取当前select 元素值的代码
2010/04/19 Javascript
javascript游戏开发之《三国志曹操传》零部件开发(三)情景对话中仿打字机输出文字
2013/01/23 Javascript
html5的自定义data-*属性和jquery的data()方法的使用示例
2013/08/21 Javascript
javascript自启动函数的问题探讨
2013/10/05 Javascript
node.js中的events.emitter.removeAllListeners方法使用说明
2014/12/10 Javascript
JavaScript实现更改网页背景与字体颜色的方法
2015/02/02 Javascript
bootstrap flask登录页面编写实例
2016/11/01 Javascript
jquery实现表单获取短信验证码代码
2017/03/13 Javascript
nodejs开发——express路由与中间件
2017/03/24 NodeJs
vue路由对不同界面进行传参及跳转的总结
2019/04/20 Javascript
Vue CLI3创建项目部署到Tomcat 使用ngrok映射到外网
2019/05/16 Javascript
layui 富文本赋值,取值,取纯文本值的实例
2019/09/18 Javascript
微信小程序制作扭蛋机代码实例
2019/09/24 Javascript
vue elementui tree 任意级别拖拽功能代码
2020/08/31 Javascript
Python数组条件过滤filter函数使用示例
2014/07/22 Python
跟老齐学Python之编写类之三子类
2014/10/11 Python
Python set常用操作函数集锦
2017/11/15 Python
关于Tensorflow中的tf.train.batch函数的使用
2018/04/24 Python
完美解决Python 2.7不能正常使用pip install的问题
2018/06/12 Python
Django 中使用流响应处理视频的方法
2018/07/20 Python
一百行python代码将图片转成字符画
2021/02/19 Python
Python 加密与解密小结
2018/12/06 Python
如何用Matplotlib 画三维图的示例代码
2020/07/28 Python
python的数学算法函数及公式用法
2020/11/18 Python
python爬取抖音视频的实例分析
2021/01/19 Python
python中HTMLParser模块知识点总结
2021/01/25 Python
SpringBoot首页设置解析(推荐)
2021/02/11 Python
html5使用html2canvas实现浏览器截图的示例
2017/08/31 HTML / CSS
Raleigh兰令自行车美国官网:英国凤头牌自行车
2018/01/08 全球购物
台湾东南旅游社网站:东南旅游
2019/02/11 全球购物
Python 使用dict实现switch的操作
2021/04/07 Python