Skip to main content

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 # 耗时约10mins

安装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) - 50 * 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) - 50 * 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.1.0 2024-11-29 修复日志重复打印的问题
v0.0.8 2024-11-19 增加音频采样率转换条件
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

Metadata

Release files for StreamSender 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for StreamSender 0.1.0
File Size Uploaded
streamsender-0.1.0.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for StreamSender 0.1.0
File Interpreter ABI Platform
StreamSender-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.1 kB

Release files / streamsender-0.1.0.tar.gz

Download URL streamsender-0.1.0.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8eea11ae260be68a34c71dd26948c18561d64ad88a2d6e16e8a577d25093b4a7
BLAKE2b-256 checksum
How to use checksums
56a7784558ece1fb0b738be0932053e3fc59b7064c7a6ff9930df265fafc0093
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release files / StreamSender-0.1.0-py3-none-any.whl

Download URL StreamSender-0.1.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c93832b88b58d234ae173617ea6b7e3bfdb9787ff200bf8194cea91f88662bb
BLAKE2b-256 checksum
How to use checksums
f3f17b5d9a0f52bedaf49ffa25699c98bf6526dacecd13de03b8d75a4cd4f6ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.19

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page