最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)實(shí)時(shí)視頻流播放代碼實(shí)例

 更新時(shí)間:2020年01月11日 11:03:34   作者:前方、有光  
這篇文章主要介紹了python實(shí)現(xiàn)實(shí)時(shí)視頻流播放代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了python實(shí)現(xiàn)實(shí)時(shí)視頻流播放代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

@action(methods=['GET'], detail=True)
  def video(self, request, pk=None):
    """
    獲取設(shè)備實(shí)時(shí)視頻流
    :param request:
    :param pk:
    :return:
    """
    device_obj = self.get_object()

    # if device_obj.status == 0:
    #   return Response({'error': '設(shè)備離線(xiàn)'})

    if not device_obj.rtsp_address:
      return Response({'error': '缺少rtsp地址'})

    cache_id = '_video_stream_{}'.format(device_obj.hash)
    cache_status = cache.get(cache_id, None)
    if cache_status is None: # 任務(wù)初始化,設(shè)置初始時(shí)間
      cache.set(cache_id, time.time(), timeout=60)

    elif isinstance(cache_status, float) and time.time() - cache_status > 30: # 任務(wù)已超時(shí), 返回錯(cuò)誤信息, 一段時(shí)間內(nèi)不再入隊(duì)
      return Response({'error': '連接數(shù)目超過(guò)限制, 請(qǐng)稍后再試'})

    ret = job_queue.enqueue_video(rtsp_address=device_obj.rtsp_address, device_hash=device_obj.hash)

    logger.info('fetch device %s video job status: %s', pk, ret._status)

    if ret._status == b'started' or 'started': # 視頻流正常推送中, 刷新播放時(shí)間, 返回視頻ID
      cache.set(cache_id, 'continue', timeout=30)
      return Response({'video': ''.join([settings.FFMPEG_VIDEO, device_obj.hash])})

    elif ret._status == b'queued' or 'queued': # 視頻任務(wù)等待中
      return Response({'status': '等待建立視頻連接'})

    else: # 建立視頻任務(wù)失敗
      return Response({'error': '打開(kāi)視頻失敗'})
class JobQueue:
  """實(shí)時(shí)視頻播放"""
  def __init__(self):
    self.video_queue = django_rq.get_queue('video') # 視頻推流消息隊(duì)列

  def enqueue_video(self, rtsp_address, device_hash):
    """視頻流隊(duì)列"""
    job_id = 'video_{}'.format(device_hash)
    job = self.video_queue.fetch_job(job_id)

    if not job:
      job = self.video_queue.enqueue_call(
        func='utils.ffmpeg.ffmpeg_play',
        args=(rtsp_address, device_hash),
        timeout=-1,
        ttl=30, # 最多等待30秒
        result_ttl=0,
        job_id=job_id
      )

    return job
# -*- coding: utf-8 -*-

import subprocess
import threading
import time
import logging

from django.core.cache import cache


logger = logging.getLogger('server.default')


def ffmpeg_play(stream, name):

  play = True
  cache_id = '_video_stream_{}'.format(name)
  cache.set(cache_id, 'continue', timeout=30)
  process = None

  def upstream():
    cmd = "ffmpeg -i '{}' -c:v h264 -f flv -r 25 -an 'rtmp://127.0.0.1:1935/live/{}'".format(stream, name)
    process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stderr=subprocess.DEVNULL)
    try:
      logger.info('device: {} stream thread start: {}'.format(name, stream))
      while play:
        time.sleep(1)

    except Exception as e:
      logger.info('device: {} stream thread error {}'.format(name, e))

    finally:
      logger.info('device: {} stream thread stop'.format(name))
      process.communicate(b'q')

  thr = threading.Thread(target=upstream)
  thr.start()

  try:
    while True:
      play = cache.get(cache_id, '')
      if play != 'continue':
        logger.info('stop device {} video stream'.format(name))
        play = False
        break
      time.sleep(1)

  except Exception as e:
    logger.info('device: {} play stream error {}'.format(name, e))
    process.communicate(b'q')

  logger.info('wait device {} video thread stop'.format(name))
  thr.join()
  logger.info('device {} video job stop'.format(name))
# 實(shí)時(shí)視頻流播放
RQ_QUEUES = {
  'video': {
    'USE_REDIS_CACHE': 'video',
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python黑魔法庫(kù)安裝及操作字典示例詳解

    Python黑魔法庫(kù)安裝及操作字典示例詳解

    這篇文章主要為大家介紹了Python中黑魔法庫(kù)的安裝及操作字典的示例詳解,有需要的 朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10
  • python實(shí)現(xiàn)斗地主分牌洗牌

    python實(shí)現(xiàn)斗地主分牌洗牌

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)斗地主分牌洗牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • 利用Python實(shí)現(xiàn)一個(gè)可定制風(fēng)格的繪圖系統(tǒng)

    利用Python實(shí)現(xiàn)一個(gè)可定制風(fēng)格的繪圖系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)一個(gè)可定制風(fēng)格的繪圖系統(tǒng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解下
    2023-09-09
  • python設(shè)置環(huán)境變量的幾種方法總結(jié)

    python設(shè)置環(huán)境變量的幾種方法總結(jié)

    這篇文章主要介紹了在Python中設(shè)置環(huán)境變量可以通過(guò)多種方式實(shí)現(xiàn),包括使用os.environ、os.putenv、setuptools以及在操作系統(tǒng)級(jí)別設(shè)置,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-01-01
  • Ubuntu 下 vim 搭建python 環(huán)境 配置

    Ubuntu 下 vim 搭建python 環(huán)境 配置

    這篇文章主要介紹了Ubuntu 下 vim 搭建python環(huán)境配置,需要的朋友可以參考下
    2017-06-06
  • Python寫(xiě)的Discuz7.2版faq.php注入漏洞工具

    Python寫(xiě)的Discuz7.2版faq.php注入漏洞工具

    這篇文章主要介紹了Python寫(xiě)的Discuz7.2版faq.php注入漏洞工具,全自動(dòng)的一款注入工具,針對(duì)Discuz7.2版,需要的朋友可以參考下
    2014-08-08
  • 為什么python比較流行

    為什么python比較流行

    在本篇文章里小編給大家分析了關(guān)于python比較流行的原因以及優(yōu)勢(shì)等,需要的朋友們可以參考下。
    2020-06-06
  • 什么是Python中的匿名函數(shù)

    什么是Python中的匿名函數(shù)

    在本篇文章里小編給大家整理的是關(guān)于Python匿名函數(shù)知識(shí)點(diǎn)總結(jié),需要的朋友們可以學(xué)習(xí)參考下。
    2020-06-06
  • Python 畫(huà)出來(lái)六維圖

    Python 畫(huà)出來(lái)六維圖

    這篇文章主要介紹了Python 畫(huà)出來(lái)六維圖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • pip安裝提示Twisted錯(cuò)誤問(wèn)題(Python3.6.4安裝Twisted錯(cuò)誤)

    pip安裝提示Twisted錯(cuò)誤問(wèn)題(Python3.6.4安裝Twisted錯(cuò)誤)

    這篇文章主要介紹了pip安裝提示Twisted錯(cuò)誤問(wèn)題(Python3.6.4安裝Twisted錯(cuò)誤),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-05-05

最新評(píng)論

富蕴县| 阳原县| 江北区| 金塔县| 岐山县| 汾西县| 泗水县| 军事| 砚山县| 泗水县| 岳普湖县| 旬阳县| 麻城市| 泸定县| 永吉县| 盱眙县| 汶川县| 温泉县| 汉沽区| 图木舒克市| 吉水县| 宜都市| 广东省| 年辖:市辖区| 梅河口市| 安顺市| 阿克陶县| 姜堰市| 奇台县| 富平县| 四川省| 四川省| 濮阳县| 肇东市| 安陆市| 福贡县| 同德县| 开远市| 黎川县| 太谷县| 宁陕县|