python+ffmpeg視頻并發(fā)直播壓力測試
通過python與ffmpeg結(jié)合使用,可生成進行視頻點播、直播的壓力測試腳本??芍С植煌愋偷囊曨l流,比如rtmp或者hls形式。
通過如下方式執(zhí)行腳本:python multiRealPlay.py [rtmp|http] [thread counts] [interval Time]
[rtmp | http]:視頻播放的不同形式
[thread counts]:并發(fā)線程數(shù)
[interval Time]:啟動每個線程的間隔時間
代碼:
#!/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()
#需要手動配置的參數(shù)
#啟動序號
SLEEP_TIME = 0
#直播地址
FULL_ADDR = {}
#需要手動配置的參數(shù)
RTMP_ADDR = 'rtmp://192.168.1.208:1935/live/'
HTTP_ADDR = 'http://192.168.1.208:80/live'
liveID = '100002750' #來自于萬視無憂中創(chuàng)建的直播
urlKey = 'a1e5c680f7bfc85851de8ab2e63b0a33' #來自于萬視無憂安全設(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
#獲取本機ip地址,用來產(chǎn)生區(qū)別于其他機器的數(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 個 %s 進程開始運行........" %(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 "進程退出"
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch張量和numpy數(shù)組相互轉(zhuǎn)換
在使用pytorch作為深度學(xué)習(xí)的框架時,經(jīng)常會遇到張量tensor和矩陣numpy的類型的相互轉(zhuǎn)化的問題,本文主要介紹了pytorch張量和numpy數(shù)組相互轉(zhuǎn)換,感興趣的可以了解一下2024-02-02
Python機器學(xué)習(xí)之預(yù)測黃金價格
這篇文章主要介紹了如何使用機器學(xué)習(xí)方法來預(yù)測最重要的貴金屬之一黃金的價格,文中的示例代碼講解詳細,感興趣的小伙伴可以試一試2022-01-01
python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式
Python內(nèi)置了CSV模塊,可直接通過該模塊實現(xiàn)csv文件的讀寫操作,在web應(yīng)用中導(dǎo)出數(shù)據(jù)是比較常見操作,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式的相關(guān)資料,需要的朋友可以參考下2022-06-06
pycharm 主題theme設(shè)置調(diào)整仿sublime的方法
今天小編就為大家分享一篇pycharm 主題theme設(shè)置調(diào)整仿sublime的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python人工智能tensorflow函數(shù)tf.nn.dropout使用方法
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.nn.dropout使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
python+selenium實現(xiàn)自動化百度搜索關(guān)鍵詞
在本篇文章里我們給大家分享了一篇關(guān)于python+selenium實現(xiàn)自動化百度搜索關(guān)鍵詞的實例文章,需要的朋友們可以跟著操作下。2019-06-06
Python企業(yè)編碼生成系統(tǒng)之系統(tǒng)主要函數(shù)設(shè)計詳解
這篇文章主要介紹了Python企業(yè)編碼生成系統(tǒng)之系統(tǒng)主要函數(shù)設(shè)計,涉及目錄操作、文件讀寫、驗證判斷、編碼輸出等功能實現(xiàn)技巧,需要的朋友可以參考下2019-07-07

