Skip to main content

A SDK for sending RTP and RTMP streams

Project description

description

功能:将音视频转换成rtp和rtmp并发送

usage examples

源码编译安装ffmpeg和av

apt install -y wget unzip tar pigz rsync
wget http://prod2.wos.58dns.org/uwsRqPoNuVZ/humanvirtual/install.sh  && chmod +x install.sh && /bin/bash install.sh

安装StreamSender

pip install StreamSender

RTPSender用法示例:

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

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

    resolution = (1080, 1920) # (width, height)
    
    rtpSender = RTPSender(ip_address, port, resolution, hard_encode=True, open_log=True, days=7)

    audio = AudioSegment.from_file(audio_48k_file, format="wav")
    audio_data = audio.raw_data
    imgs = [cv2.imread(image_file) for image_file in image_files]
    
    i, cnt = 0, 0
    frame_size = 1920

    t1 = time()

    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)
                rtpSender.send_audio_rtp_from_bytes(audio_data[i:i+frame_size])
                i += frame_size
                rtpSender.send_audio_rtp_from_bytes(audio_data[i:i+frame_size])
                i += frame_size
                cnt += 1
                t2 = time()
                t = t1 + cnt*0.04 - t2
                if t > 0:
                    sleep(t)

RTMPSender用法示例:

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

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

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

    rtmpSender = RTMPSender(resolution, rtmp_url, sample_rate, 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
    imgs = [cv2.imread(image_file) for image_file in image_files]

    i, cnt = 0, 0
    frame_size = 640

    t1 = time()

    while True:
        for img in imgs:
            if i >= len(audio_data) - frame_size:
                i = 0
            for j in range(25):
                rtmpSender.send_video_rtmp_from_img(img)
                rtmpSender.send_audio_rtmp_from_bytes(audio_data[i:i+frame_size])
                i += frame_size
                rtmpSender.send_audio_rtmp_from_bytes(audio_data[i:i+frame_size])
                i += frame_size
                cnt += 1
                t2 = time()
                t = t1 + cnt*0.04 - t2
                if t > 0:
                    sleep(t)

Major Releases

Release Version Release Date Updates
v0.0.6 2024-10-31 使用PyAV推送rtmp
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.7.tar.gz (7.2 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.7-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: streamsender-0.0.7.tar.gz
  • Upload date:
  • Size: 7.2 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.7.tar.gz
Algorithm Hash digest
SHA256 e907808b2c3b4a95745539d0f7d3d346031ec37c20c823b93426742ee1ae764a
MD5 37eb0b71c84ad13b1814cd0bd34dbc40
BLAKE2b-256 58a9b01daf87c0b606219b30abb244ca5f2caa06c9bb33f0e17984618e8b15c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: StreamSender-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 9.3 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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 da7410cf57ddb4fd009db40ed787dd8b3f61a0e3e27e3209a3e440acac633d6d
MD5 f1b0521cc58d5859d7487d1dc9a80ead
BLAKE2b-256 e7fe6696f4666e1f0c22c79e2419e50ddf487d0dae466bc44c547c368aeea7a4

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