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 self,cls,decorator的理解
Jul 13 Python
python判断字符串是否包含子字符串的方法
Mar 24 Python
python基础入门学习笔记(Python环境搭建)
Jan 13 Python
利用Python实现Windows定时关机功能
Mar 21 Python
Python动刷新抢12306火车票的代码(附源码)
Jan 24 Python
Python 实现Windows开机运行某软件的方法
Oct 14 Python
Python生成rsa密钥对操作示例
Apr 26 Python
详解python中__name__的意义以及作用
Aug 07 Python
基于Pytorch SSD模型分析
Feb 18 Python
PyTorch中model.zero_grad()和optimizer.zero_grad()用法
Jun 24 Python
超级实用的8个Python列表技巧
Aug 24 Python
python 检测图片是否有马赛克
Dec 01 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(6) 面向对象
2010/02/16 PHP
解析在PHP中使用mysqli扩展库对mysql的操作
2013/07/03 PHP
PHP之APC缓存详细介绍 apc模块安装
2014/01/13 PHP
CodeIgniter实现更改view文件夹路径的方法
2014/07/04 PHP
PHP设置进度条的方法
2015/07/08 PHP
Symfony生成二维码的方法
2016/02/04 PHP
WAF的正确bypass
2017/01/05 PHP
PHP实现数组转JSon和JSon转数组的方法示例
2018/06/14 PHP
JavaScript入门教程(3) js面向对象
2009/01/31 Javascript
jquery复选框全选/取消示例
2013/12/30 Javascript
jQuery实现行文字链接提示效果的方法
2015/03/10 Javascript
BootStrap使用popover插件实现鼠标经过显示并保持显示框
2016/06/23 Javascript
angularjs 源码解析之injector
2016/08/22 Javascript
JS新包管理工具yarn和npm的对比与使用入门
2016/12/09 Javascript
JS验证不重复验证码
2017/02/10 Javascript
jQuery实现移动端Tab选项卡效果
2017/03/15 Javascript
vue-router 路由基础的详解
2017/10/17 Javascript
JavaScript原生实现观察者模式的示例
2017/12/15 Javascript
jQuery实现表单动态添加数据并提交的方法
2018/07/19 jQuery
vue自定v-model实现表单数据双向绑定问题
2018/09/03 Javascript
vux-scroller实现移动端上拉加载功能过程解析
2019/10/08 Javascript
[58:23]LGD vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
python 2.7.14安装图文教程
2018/04/08 Python
Python3 assert断言实现原理解析
2020/03/02 Python
python中remove函数的踩坑记录
2021/01/04 Python
用Python实现定时备份Mongodb数据并上传到FTP服务器
2021/01/27 Python
澳大利亚UGG工厂直销:Australian Ugg Boots
2017/10/14 全球购物
意大利在线药房:shop-farmacia.it
2019/03/12 全球购物
eDreams葡萄牙:全球最大的在线旅行社之一
2019/04/15 全球购物
2014普法依法治理工作总结
2014/12/18 职场文书
计划生育汇报材料
2014/12/26 职场文书
罚站检讨书
2015/01/29 职场文书
安全员岗位职责范本
2015/04/11 职场文书
中国汉字听写大会观后感
2015/06/02 职场文书
本地通过nginx配置反向代理的全过程记录
2021/03/31 Servers
Netty结合Protobuf进行编解码的方法
2021/06/26 Java/Android