python調(diào)用系統(tǒng)ffmpeg實(shí)現(xiàn)視頻截圖、http發(fā)送
python 調(diào)用系統(tǒng)ffmpeg進(jìn)行視頻截圖,并進(jìn)行圖片http發(fā)送ffmpeg ,視頻、圖片的各種處理。
最近在做視頻、圖片的版權(quán)等深度學(xué)習(xí)識別,用到了ffmpeg部分功能,功能如下:
調(diào)用ffmpeg 對不同目錄視頻進(jìn)行截圖,通過http發(fā)送到后臺進(jìn)行算法識別。
每5分鐘掃描最近的視頻,生成圖片,發(fā)送完畢圖片刪除。
代碼如下:
#!/usr/bin/env python # -*- coding: utf-8 -*- """'定時任務(wù)每五分鐘發(fā)送上一個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' #時間差5分鐘執(zhí)行一次 subtime=300 #生成審核中截圖 def create_auditing_image(auditing_dir): #掃描視頻目錄生成截圖 for parent, dirnames, filenames in os.walk(auditing_dir): # 三個參數(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) #獲取修改時間 localtime=time.time() #獲取當(dāng)前系統(tǒng)時間 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): # 三個參數(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) #獲取修改時間 localtime=time.time() #獲取當(dāng)前系統(tǒng)時間 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): # 三個參數(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) #獲取修改時間 localtime=time.time() #獲取當(dāng)前系統(tǒng)時間 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): # 三個參數(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) #獲取修改時間 localtime=time.time() #獲取當(dāng)前系統(tǒng)時間 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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python八大常見排序算法定義、實(shí)現(xiàn)及時間消耗效率分析
這篇文章主要介紹了Python八大常見排序算法定義、實(shí)現(xiàn)及時間消耗效率分析,結(jié)合具體實(shí)例形式對比分析了冒泡排序、直接插入排序、選擇排序、歸并排序、希爾排序、桶排序、堆排序等排序算法的使用與執(zhí)行效率,需要的朋友可以參考下2018-04-04使用Python去除字符串中某個字符的多種實(shí)現(xiàn)方式比較
python中字符串是不可變的,所以無法直接刪除字符串之間的特定字符,下面這篇文章主要給大家介紹了關(guān)于使用Python去除字符串中某個字符的多種實(shí)現(xiàn)方式比較的相關(guān)資料,需要的朋友可以參考下2022-06-06Pytorch神經(jīng)網(wǎng)絡(luò)參數(shù)管理方法詳細(xì)講解
這篇文章主要介紹了Pytorch神經(jīng)網(wǎng)絡(luò)參數(shù)管理方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-05-05Python minidom模塊用法示例【DOM寫入和解析XML】
這篇文章主要介紹了Python minidom模塊用法,結(jié)合實(shí)例形式分析了Python DOM創(chuàng)建、寫入和解析XML文件相關(guān)操作技巧,需要的朋友可以參考下2019-03-03python3應(yīng)用windows api對后臺程序窗口及桌面截圖并保存的方法
今天小編就為大家分享一篇python3應(yīng)用windows api對后臺程序窗口及桌面截圖并保存的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08