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

yipeiwu_com5年前Python基础

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 xlwt设置excel单元格字体及格式

Python xlwt设置excel单元格字体及格式

本文根据自己初学经验编写的使用xlwt模块设置单元格的一些基本样式,如设置单元格的背景颜色,下框线,字体,字体的颜色,设置列宽行高,插入简单的图片,详细程序如下: #!/usr/bi...

Python 经典面试题 21 道【不可错过】

到底什么是Python? •Python是一种解释性语言。Python代码在运行之前不需要编译。其它解释性语言还包括PHP和Ruby。 •Python是...

pytorch中的自定义反向传播,求导实例

pytorch中自定义backward()函数。在图像处理过程中,我们有时候会使用自己定义的算法处理图像,这些算法多是基于numpy或者scipy等包。 那么如何将自定义算法的梯度加入到...

Python中dictionary items()系列函数的用法实例

本文实例讲述了Python中dictionary items()系列函数的用法,对Python程序设计有很好的参考借鉴价值。具体分析如下: 先来看一个示例: import html...

Python 装饰器使用详解

装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象.   经常用于有切面需求的场景,比如:插入日志、性能测试、...