python+ffmpeg視頻并發(fā)直播壓力測(cè)試
通過(guò)python與ffmpeg結(jié)合使用,可生成進(jìn)行視頻點(diǎn)播、直播的壓力測(cè)試腳本??芍С植煌愋偷囊曨l流,比如rtmp或者h(yuǎn)ls形式。
通過(guò)如下方式執(zhí)行腳本:python multiRealPlay.py [rtmp|http] [thread counts] [interval Time]
[rtmp | http]:視頻播放的不同形式
[thread counts]:并發(fā)線程數(shù)
[interval Time]:?jiǎn)?dòng)每個(gè)線程的間隔時(shí)間
代碼:
#!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2015年7月22日 @author: LiBiao ''' import datetime,time import threading import subprocess import os, base64 import sys import Queue queue = Queue.Queue() #需要手動(dòng)配置的參數(shù) #啟動(dòng)序號(hào) SLEEP_TIME = 0 #直播地址 FULL_ADDR = {} #需要手動(dòng)配置的參數(shù) RTMP_ADDR = 'rtmp://192.168.1.208:1935/live/' HTTP_ADDR = 'http://192.168.1.208:80/live' liveID = '100002750' #來(lái)自于萬(wàn)視無(wú)憂中創(chuàng)建的直播 urlKey = 'a1e5c680f7bfc85851de8ab2e63b0a33' #來(lái)自于萬(wàn)視無(wú)憂安全設(shè)置模塊 liveResCode = '71ac6c06d3' #直播源碼 #生成MD5值 def getMD5_Value(inputdata): try: import hashlib hash = hashlib.md5(inputdata.encode('utf-8')) except ImportError: #for python << 2.5 import md5 hash = md5.new() return hash.hexdigest() #直播地址組裝 def build_live_addr(): t = time.strftime('%Y%m%d%H%M%S',time.localtime())[2:] data = '%s#%s#%s' %(liveID, t, urlKey) secret = getMD5_Value(data) rtmp_addr = '%s%s?liveID=%s&time=%s&secret=%s' %(RTMP_ADDR, liveResCode, liveID, t, secret) http_addr = '%s/%s/playlist.m3u8?liveID=%s&time=%s&secret=%s' %(HTTP_ADDR, liveResCode, liveID, t, secret) FULL_ADDR['rtmp'] = rtmp_addr FULL_ADDR['http'] = http_addr return FULL_ADDR #獲取本機(jī)ip地址,用來(lái)產(chǎn)生區(qū)別于其他機(jī)器的數(shù)據(jù) def get_local_ip(): try: ip = os.popen("ifconfig | grep 'inet addr' | awk '{print $2}'").read() ip = ip[ip.find(':') + 1:ip.find('\n')] except Exception,e: print e return ip class Video_To_Live(threading.Thread): def __init__(self,queue): threading.Thread.__init__(self) self.queue = queue def run(self): liveAddr = self.queue.get() #print liveAddr try: print liveAddr subprocess.call('./ffmpeg -i \"%s\" -c:v copy -c:a copy -bsf:a aac_adtstoasc -y -f flv -timeout 4000 /dev/null 2>/dev/null' %liveAddr,stdout=subprocess.PIPE,shell=True) except Exception as e: wiriteLog('ERROR',str(e)) self.queue.task_done() if __name__ == "__main__": time.sleep(SLEEP_TIME) parser = argparse.ArgumentParser(description = "Live Play") parser.add_argument('--liveType',action = "store",dest = "liveType",required = False) parser.add_argument('--pnum',action = "store",dest = "pnum",type = int,required = False) parser.add_argument('--itime',action = "store",dest = "itime",required = False) given_args = parser.parse_args() liveType = given_args.liveType threadNum = given_args.pnum intervalTime = given_args.itime print "%d 個(gè) %s 進(jìn)程開始運(yùn)行........" %(threadNum, Video_To_Live) for i in xrange(threadNum): videotolive = Video_To_Live(queue) videotolive.setDaemon(True) videotolive.start() for i in xrange(threadNum): if liveType in ["http","rtmp"]: addr = build_live_addr() liveaddr = addr[liveType] queue.put(liveaddr) time.sleep(intervalTime) queue.join() print "進(jìn)程退出"
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python調(diào)用系統(tǒng)ffmpeg實(shí)現(xiàn)視頻截圖、http發(fā)送
- Python使用ffmpeg合成視頻、音頻的實(shí)現(xiàn)方法
- Python 如何利用ffmpeg 處理視頻素材
- python調(diào)用ffmpeg命令行工具便捷操作視頻示例實(shí)現(xiàn)過(guò)程
- ffmpeg+Python實(shí)現(xiàn)B站MP4格式音頻與視頻的合并示例代碼
- Python3 ffmpeg視頻轉(zhuǎn)換工具使用方法解析
- python ffmpeg任意提取視頻幀的方法
- 如何使用 Python和 FFmpeg 批量截圖視頻到各自文件夾中
相關(guān)文章
使用并行處理提升python?for循環(huán)速度的過(guò)程
Python?是一門功能強(qiáng)大的編程語(yǔ)言,但在處理大規(guī)模數(shù)據(jù)或復(fù)雜計(jì)算任務(wù)時(shí),性能可能成為一個(gè)瓶頸,這篇文章主要介紹了使用并行處理提升python?for循環(huán)速度,需要的朋友可以參考下2023-06-06解決python 在for循環(huán)并且pop數(shù)組的時(shí)候會(huì)跳過(guò)某些元素的問(wèn)題
這篇文章主要介紹了解決python 在for循環(huán)并且pop數(shù)組的時(shí)候會(huì)跳過(guò)某些元素的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12python調(diào)用Matplotlib繪制分布點(diǎn)圖
這篇文章主要為大家詳細(xì)介紹了python調(diào)用Matplotlib繪制分布點(diǎn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10對(duì)Python中一維向量和一維向量轉(zhuǎn)置相乘的方法詳解
今天小編就為大家分享一篇對(duì)Python中一維向量和一維向量轉(zhuǎn)置相乘的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python實(shí)戰(zhàn)之手寫一個(gè)搜索引擎
這篇文章主要介紹了Python實(shí)戰(zhàn)之手寫一個(gè)搜索引擎,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04多線程爬蟲批量下載pcgame圖片url 保存為xml的實(shí)現(xiàn)代碼
用Python寫的多線程爬蟲批量下載pcgame的圖片url并保存為xml格式,主要是邏輯代碼,喜歡的朋友可以測(cè)試下2013-01-01