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采用getopt解析命令行输入参数实例
Sep 30 Python
Python中list列表的一些进阶使用方法介绍
Aug 15 Python
Python优先队列实现方法示例
Sep 21 Python
python spyder中读取txt为图片的方法
Apr 27 Python
python将excel转换为csv的代码方法总结
Jul 03 Python
pyqt5 QScrollArea设置在自定义侧(任何位置)
Sep 25 Python
Python3实现监控新型冠状病毒肺炎疫情的示例代码
Feb 13 Python
PyTorch之nn.ReLU与F.ReLU的区别介绍
Jun 27 Python
python 5个顶级异步框架推荐
Sep 09 Python
Python实现哲学家就餐问题实例代码
Nov 09 Python
神经网络训练采用gpu设置的方式
Mar 03 Python
Python序列化模块JSON与Pickle
Jun 05 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
Terran兵种对照表
2020/03/14 星际争霸
PHP用strstr()函数阻止垃圾评论(通过判断a标记)
2013/09/28 PHP
不使用php api函数实现数组的交换排序示例
2014/04/13 PHP
最常用的8款PHP调试工具
2014/07/06 PHP
php 中的closure用法详解
2017/06/12 PHP
PHP-FPM 设置多pool及配置文件重写操作示例
2019/10/02 PHP
完美解决JS中汉字显示乱码问题(已解决)
2006/12/27 Javascript
用JS判别浏览器种类以及IE版本的几种方法小结
2011/08/02 Javascript
在js中判断checkboxlist(.net控件客户端id)是否有选中
2013/04/11 Javascript
jQuery实现类似滑动门切换效果的层切换
2013/09/23 Javascript
使用原生js实现页面蒙灰(mask)效果示例代码
2014/06/20 Javascript
jquery移动节点实例
2015/01/14 Javascript
javascript倒计时效果实现
2015/11/12 Javascript
原生JS查找元素的方法(推荐)
2016/11/22 Javascript
对称加密与非对称加密优缺点详解
2017/02/06 Javascript
JS实现课堂随机点名和顺序点名
2017/03/09 Javascript
vue学习笔记之指令v-text &amp;&amp; v-html &amp;&amp; v-bind详解
2017/05/12 Javascript
基于nodejs 的多页面爬虫实例代码
2017/05/31 NodeJs
Vue ElementUI之Form表单验证遇到的问题
2017/08/21 Javascript
Node爬取大批量文件的方法示例
2019/06/28 Javascript
node实现爬虫的几种简易方式
2019/08/22 Javascript
Vue el-autocomplete远程搜索下拉框并实现自动填充功能(推荐)
2019/10/25 Javascript
Vite和Vue CLI的优劣
2021/01/30 Vue.js
JavaScript实现点击出现子菜单效果
2021/02/08 Javascript
[03:22]DOTA2超级联赛专访单车:找到属于自己的英雄
2013/06/08 DOTA
python实现将html表格转换成CSV文件的方法
2015/06/28 Python
Python实现在线音乐播放器
2017/03/03 Python
Python中使用pypdf2合并、分割、加密pdf文件的代码详解
2019/05/21 Python
Ubuntu下Anaconda和Pycharm配置方法详解
2019/06/14 Python
Python动态强类型解释型语言原理解析
2020/03/25 Python
Python3.8.2安装包及安装教程图文详解(附安装包)
2020/11/28 Python
戴尔加拿大官网:Dell加拿大
2016/09/17 全球购物
主管职责范文
2013/11/09 职场文书
园艺师求职信
2014/03/10 职场文书
中国式结婚:司仪主持词(范文)
2019/07/25 职场文书
用javascript制作qq注册动态页面
2021/04/14 Javascript