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爬虫之使用Scrapy框架编写爬虫
Nov 07 Python
Python实现基于HTTP文件传输实例
Nov 08 Python
Python抓取手机号归属地信息示例代码
Nov 28 Python
Python正则表达式教程之二:捕获篇
Mar 02 Python
使用python编写简单的小程序编译成exe跑在win10上
Jan 15 Python
python 接口测试response返回数据对比的方法
Feb 11 Python
numpy中实现二维数组按照某列、某行排序的方法
Apr 04 Python
深入解析Python小白学习【操作列表】
Mar 23 Python
python rsa实现数据加密和解密、签名加密和验签功能
Sep 18 Python
在keras 中获取张量 tensor 的维度大小实例
Jun 10 Python
python 使用建议与技巧分享(四)
Aug 18 Python
python 6行代码制作月历生成器
Sep 18 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 !function_exists(&quot;T7FC56270E7A70FA81A5935B72EACBE29&quot;))代码解密
2011/01/07 PHP
PHP 循环删除无限分类子节点的实现代码
2013/06/21 PHP
在WordPress中实现发送http请求的相关函数解析
2015/12/29 PHP
利用switch语句进行多选一判断的实例代码
2016/11/14 PHP
解决laravel 5.1报错:No supported encrypter found的办法
2017/06/07 PHP
php实现页面纯静态的实例代码
2017/06/21 PHP
Laravel框架文件上传功能实现方法示例
2019/04/16 PHP
js 加密压缩出现bug解决方案
2014/11/25 Javascript
jQuery中[attribute]选择器用法实例
2014/12/31 Javascript
JS版微信6.0分享接口用法分析
2016/10/13 Javascript
浅谈键盘上回车按钮的js触发事件
2017/02/13 Javascript
浅谈JavaScript正则表达式-非捕获性分组
2017/03/08 Javascript
React从react-router路由上做登陆验证控制的方法
2018/05/10 Javascript
vue router 源码概览案例分析
2018/10/09 Javascript
使用ESLint禁止项目导入特定模块的方法步骤
2019/03/04 Javascript
vue实现记事本功能
2019/06/26 Javascript
Echarts地图添加引导线效果(labelLine)
2019/09/30 Javascript
使用vscode快速建立vue模板过程详解
2019/10/10 Javascript
layui写后台表格思路和赋值用法详解
2019/11/14 Javascript
Python实现栈的方法
2015/05/26 Python
使用Python编写基于DHT协议的BT资源爬虫
2016/03/19 Python
python 3.5实现检测路由器流量并写入txt的方法实例
2017/12/17 Python
python爬虫-模拟微博登录功能
2019/09/12 Python
Python3 shelve对象持久存储原理详解
2020/03/23 Python
python图片指定区域替换img.paste函数的使用
2020/04/09 Python
今天学到的CSS最新技术(与图片背景相关)
2012/12/24 HTML / CSS
理肤泉加拿大官网:La Roche-Posay加拿大
2018/07/06 全球购物
英国排名第一的宠物店:PetPlanet
2020/02/02 全球购物
财务会计实习报告体会
2013/12/20 职场文书
致跳远、跳高运动员广播稿
2014/01/09 职场文书
人事部岗位职责范本
2014/03/05 职场文书
2014年党支部学习材料
2014/05/19 职场文书
2015年高校辅导员工作总结
2015/04/20 职场文书
交通事故协议书范本
2016/03/19 职场文书
解决Git推送错误non-fast-forward的方法
2022/06/25 Servers
浅谈css清除浮动(clearfix和clear)的用法
2023/05/21 HTML / CSS