python+ffmpeg视频并发直播压力测试

 更新时间:2018年03月06日 15:16:08   作者:LZT-LHZ  
这篇文章主要为大家详细介绍了python+ffmpeg视频并发直播压力测试,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

通过python与ffmpeg结合使用,可生成进行视频点播、直播的压力测试脚本。可支持不同类型的视频流,比如rtmp或者hls形式。
通过如下方式执行脚本:python multiRealPlay.py [rtmp|http] [thread counts] [interval Time]
[rtmp | http]:视频播放的不同形式
[thread counts]:并发线程数
[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()


#需要手动配置的参数

#启动序号
SLEEP_TIME = 0

#直播地址
FULL_ADDR = {}

#需要手动配置的参数
RTMP_ADDR = 'rtmp://192.168.1.208:1935/live/'
HTTP_ADDR = 'http://192.168.1.208:80/live'
liveID = '100002750'  #来自于万视无忧中创建的直播
urlKey = 'a1e5c680f7bfc85851de8ab2e63b0a33'  #来自于万视无忧安全设置模块
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地址,用来产生区别于其他机器的数据
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 "进程退出"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • python常见的占位符总结及用法

    python常见的占位符总结及用法

    在本篇文章里小编给大家整理的是一篇关于python常见的占位符总结及用法,有兴趣的朋友们可以跟着学习参考下。
    2021-07-07
  • Python AutoCAD 系统设置的实现方法

    Python AutoCAD 系统设置的实现方法

    这篇文章主要介绍了Python AutoCAD 系统设置的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • python多进程重复加载的解决方式

    python多进程重复加载的解决方式

    今天小编就为大家分享一篇python多进程重复加载的解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)

    Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版

    这篇文章主要介绍了Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Python基础之数据类型知识汇总

    Python基础之数据类型知识汇总

    今天带大家复习一下Python基础知识,文中对数据类型相关知识做了详细的汇总,对刚入门python的小伙伴很有帮助哟,需要的朋友可以参考下
    2021-05-05
  • 分享一个可以生成各种进制格式IP的小工具实例代码

    分享一个可以生成各种进制格式IP的小工具实例代码

    这篇文章主要给大家分享了一个可以生成各种进制格式IP的小工具,利用的语言是python实现的一个小工具,这个小工具对大家的日常使用与开发具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。
    2017-07-07
  • Python获取linux主机ip的简单实现方法

    Python获取linux主机ip的简单实现方法

    这篇文章主要介绍了Python获取linux主机ip的简单实现方法,涉及Python使用socket模块调用shell命令的相关技巧,需要的朋友可以参考下
    2016-04-04
  • 对Python中class和instance以及self的用法详解

    对Python中class和instance以及self的用法详解

    今天小编就为大家分享一篇对Python中class和instance以及self的用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • 总结Pyinstaller的坑及终极解决方法(小结)

    总结Pyinstaller的坑及终极解决方法(小结)

    这篇文章主要介绍了总结Pyinstaller的坑及终极解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • python将字符串转换成json的方法小结

    python将字符串转换成json的方法小结

    这篇文章主要介绍了python将字符串转换成json的方法小结,通过实例代码给大家介绍将字符串型的数据转换成dict类型遇到的问题,需要的朋友可以参考下
    2019-07-07

最新评论