Skip to main content

A SDK for sending RTP and RTMP streams

Project description

description

功能:将音视频数据转换成rtp和rtmp包

usage examples

安装av库

pip uninstall av && pip install av --no-binary av

具体使用方式请参考下面的示例:

from streamers.rtp_sender import RTPSender
from streamers.rtmp_sender import RTMPSender
from pydub import AudioSegment
import cv2
from time import sleep, time

if __name__ == '__main__':
    ip_address = "127.0.0.1"
    rtmp_url = 'your_rtmp_url'
    port = 7777
    image_file = "/Users/a58/Code/python/rtp/images/frame_0.png"
    image_files = ["/Users/a58/Code/python/rtp/images/frame_%d.png" % i for i in range(5)]
    audio_file = "/Users/a58/Code/python/rtp/audios/bgroup.wav"
    audio_16k_file = "/Users/a58/Code/python/rtp/audios/bgroup16k.wav"
    audio_48k_file = "/Users/a58/Code/python/rtp/audios/bgroup48k.wav"

    resolution = (1080, 1920) # (width, height)

    rtpSender = RTPSender(ip_address, port, resolution, hard_encode=False, open_log=True, days=7)
    rtpSender.stop()
    
    rtpSender = RTPSender(ip_address, port, resolution, hard_encode=False, open_log=True, days=7)

    rtmpSender = RTMPSender(ip_address, port, resolution, rtmp_url=rtmp_url, hard_encode=True, open_log=True, days=7, stdout=False, bit_rate=600000)

    audio = AudioSegment.from_file(audio_48k_file, format="wav")
    audio_data = audio.raw_data
    i = 0
    cnt = 0
    t1 = time()

    init_cnt = 2

    imgs = [cv2.imread(image_file) for image_file in image_files]

    is_16k = False
    frame_size = 640 if is_16k else 1920

    frame_cnt = 0

    while True:
        for img in imgs:
            if i >= len(audio_data) - frame_size:
                i = 0
            for j in range(25):
                rtpSender.send_video_rtp_from_img(img)
                rtmpSender.send_video_rtmp_from_img(img)
                frame_cnt += 1
                rtpSender.send_audio_rtp_from_bytes(audio_data[i:i+frame_size], True)
                rtmpSender.send_audio_rtmp_from_bytes(audio_data[i:i+frame_size], is_16k=is_16k)
                i += frame_size
                rtpSender.send_audio_rtp_from_bytes(audio_data[i:i+frame_size], True)
                rtmpSender.send_audio_rtmp_from_bytes(audio_data[i:i+frame_size], is_16k=is_16k)
                cnt += 1
                i += frame_size
                t2 = time()
                t = t1 + cnt*0.04 - t2
                if t > 0:
                    sleep(t)
        if init_cnt < 20:
            rtpSender.stop()
            rtpSender = RTPSender(ip_address, port, resolution, hard_encode=False, open_log=True, days=7)
            init_cnt += 1
            print("reinit rtpSender: ", init_cnt)

Releases

Major Release Version Release Date Updates
v0.0.4 2024-10-16 增加音频采样率转换
v0.0.2 2024-10-15 增加rtmp推流异常处理, 并格式化日志
v0.0.1 2024-10-15 调整目录结构, 同时支持RTPSender和RTMPSender

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamsender-0.0.4.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

StreamSender-0.0.4-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file streamsender-0.0.4.tar.gz.

File metadata

  • Download URL: streamsender-0.0.4.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for streamsender-0.0.4.tar.gz
Algorithm Hash digest
SHA256 beac3921159370db0da2b609415e7a6327316c5677807ccef45b482a8e8b5d38
MD5 63748477581c86cc7fd1a7a8e98a6d1a
BLAKE2b-256 d04aae98cce27cb737dfb4ba0d2ba67c9326ad9da1390533278934b76e7af529

See more details on using hashes here.

File details

Details for the file StreamSender-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: StreamSender-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for StreamSender-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 12585dee9e7a13f679ab5af900471bc29653c30ac547eb269269e2cc5f0a10f6
MD5 d074cf243c856dbf3b36886025b0c349
BLAKE2b-256 cf0f6b5be1772b85f7d40be90de23988b49390ba8d163c897092b13d27379c38

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page