python 調(diào)用系統(tǒng)ffmpeg進(jìn)行視頻截圖,并進(jìn)行圖片http發(fā)送ffmpeg ,視頻、圖片的各種處理。
最近在做視頻、圖片的版權(quán)等深度學(xué)習(xí)識(shí)別,用到了ffmpeg部分功能,功能如下:
調(diào)用ffmpeg 對(duì)不同目錄視頻進(jìn)行截圖,通過(guò)http發(fā)送到后臺(tái)進(jìn)行算法識(shí)別。
每5分鐘掃描最近的視頻,生成圖片,發(fā)送完畢圖片刪除。
代碼如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- """'定時(shí)任務(wù)每五分鐘發(fā)送上一個(gè)5分鐘視頻 目標(biāo)視頻: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現(xiàn)網(wǎng)配置 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' #時(shí)間差5分鐘執(zhí)行一次 subtime=300 #生成審核中截圖 def create_auditing_image(auditing_dir): #掃描視頻目錄生成截圖 for parent, dirnames, filenames in os.walk(auditing_dir): # 三個(gè)參數(shù):分別返回1.父目錄 2.所有文件夾名字(不含路徑) 3.所有文件名字 for filename in filenames: # 輸出文件信息 video_path = os.path.join(parent, filename) # 輸出文件路徑信息 filePath = unicode(video_path, 'utf8') #中文編碼 filetime= os.path.getmtime(filePath) #獲取修改時(shí)間 localtime=time.time() #獲取當(dāng)前系統(tǒng)時(shí)間 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) # 調(diào)用命令行生成截圖 print str_video #生成審核完截圖 def create_audited_image(audited_dir): #掃描視頻目錄生成截圖 for parent, dirnames, filenames in os.walk(audited_dir): # 三個(gè)參數(shù):分別返回1.父目錄 2.所有文件夾名字(不含路徑) 3.所有文件名字 for filename in filenames: # 輸出文件信息 video_path = os.path.join(parent, filename) # 輸出文件路徑信息 filePath = unicode(video_path, 'utf8') #中文編碼 filetime= os.path.getmtime(filePath) #獲取修改時(shí)間 localtime=time.time() #獲取當(dāng)前系統(tǒng)時(shí)間 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) # 調(diào)用命令行生成截圖 print str_video #生成黑名單截圖 def create_black_image(black_dir): #掃描視頻目錄生成截圖 for parent, dirnames, filenames in os.walk(black_dir): # 三個(gè)參數(shù):分別返回1.父目錄 2.所有文件夾名字(不含路徑) 3.所有文件名字 for filename in filenames: # 輸出文件信息 video_path = os.path.join(parent, filename) # 輸出文件路徑信息 filePath = unicode(video_path, 'utf8') #中文編碼 filetime= os.path.getmtime(filePath) #獲取修改時(shí)間 localtime=time.time() #獲取當(dāng)前系統(tǒng)時(shí)間 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) # 調(diào)用命令行生成截圖 print str_video #生成白名單截圖 def create_white_image(white_dir): #掃描視頻目錄生成截圖 for parent, dirnames, filenames in os.walk(white_dir): # 三個(gè)參數(shù):分別返回1.父目錄 2.所有文件夾名字(不含路徑) 3.所有文件名字 for filename in filenames: # 輸出文件信息 video_path = os.path.join(parent, filename) # 輸出文件路徑信息 filePath = unicode(video_path, 'utf8') #中文編碼 filetime= os.path.getmtime(filePath) #獲取修改時(shí)間 localtime=time.time() #獲取當(dāng)前系統(tǒng)時(shí)間 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) # 調(diào)用命令行生成截圖 print str_video #發(fā)送圖片進(jìn)程 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 #準(zhǔn)備發(fā)送圖片 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 #刪除發(fā)送的圖片 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)
新聞熱點(diǎn)
疑難解答
圖片精選