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 相关文章推荐
Python函数中的函数(闭包)用法实例
Mar 15 Python
Python reduce()函数的用法小结
Nov 15 Python
django manage.py扩展自定义命令方法
May 27 Python
python使用tcp实现局域网内文件传输
Mar 20 Python
python 通过类中一个方法获取另一个方法变量的实例
Jan 22 Python
Python supervisor强大的进程管理工具的使用
Apr 24 Python
Django 实现Admin自动填充当前用户的示例代码
Nov 18 Python
python 函数嵌套及多函数共同运行知识点讲解
Mar 03 Python
django 数据库返回queryset实现封装为字典
May 19 Python
django中related_name的用法说明
May 20 Python
Python如何实现的二分查找算法
May 27 Python
Selenium python时间控件输入问题解决方案
Jul 22 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实现GIF图片验证码
2015/11/04 PHP
JQuery将文本转化成JSON对象需要注意的问题
2011/05/09 Javascript
js 连接数据库如何操作数据库中的数据
2012/11/23 Javascript
Angular中的Promise对象($q介绍)
2015/03/03 Javascript
JS选项卡动态替换banner图片路径的方法
2015/05/11 Javascript
jQuery+CSS3折叠卡片式下拉列表框实现效果
2015/11/02 Javascript
jQuery+css3实现转动的正方形效果(附demo源码下载)
2016/01/27 Javascript
js仿支付宝填写支付密码效果实现多方框输入密码
2016/03/09 Javascript
对Js OOP编程 创建对象的一些全面理解
2016/07/26 Javascript
jQuery中的insertBefore(),insertAfter(),after(),before()区别介绍
2016/09/01 Javascript
vue-cli监听组件加载完成的方法
2018/09/07 Javascript
html5以及jQuery实现本地图片上传前的预览代码实例讲解
2021/03/01 jQuery
Python  __getattr__与__setattr__使用方法
2008/09/06 Python
使用Python脚本对Linux服务器进行监控的教程
2015/04/02 Python
利用Python读取文件的四种不同方法比对
2017/05/18 Python
python Opencv将图片转为字符画
2021/02/19 Python
Python之列表实现栈的工作功能
2019/01/28 Python
Python元组知识点总结
2019/02/18 Python
PyQt5 QTable插入图片并动态更新的实例
2019/06/18 Python
Python实现图片批量加入水印代码实例
2019/11/30 Python
解决Python图形界面中设置尺寸的问题
2020/03/05 Python
python实现引用其他路径包里面的模块
2020/03/09 Python
Canon佳能美国官方商店:购买数码相机、数码单反相机、镜头和打印机
2016/11/15 全球购物
ALDI奥乐齐官方海外旗舰店:德国百年超市
2017/12/27 全球购物
美国在线健康和美容市场:Pharmapacks
2018/12/05 全球购物
JSF面试题:Jsf中的核心类用那些?有什么作用?LiftCycle六大生命周期是什么?
2014/07/17 面试题
百货商场楼层班组长竞聘书
2014/03/31 职场文书
给学校的建议书范文
2014/05/15 职场文书
个人安全承诺书
2014/05/22 职场文书
幼儿教师求职信
2014/05/24 职场文书
篮球社团活动总结
2014/06/27 职场文书
社区班子对照检查材料
2014/08/27 职场文书
民事诉讼答辩状范文
2015/05/21 职场文书
php 解析非标准json、非规范json
2021/04/01 PHP
Python中npy和mat文件的保存与读取
2022/04/24 Python
关于的python五子棋的算法
2022/05/02 Python