同步操作将从 mktime/python-learn 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import pyaudioimport subprocessimport sysimport noisereduce as nrimport numpy as npfrom datetime import datetime, timedeltaimport hashlib# 设置参数FORMAT = pyaudio.paInt16CHANNELS = 1RATE = 44100CHUNK = 1024RTMP_URL = 'rtmp://127.0.0.1/live/stream' # 替换为你的 RTMP 服务器地址DEVICE_INDEX = 0'''rtmp服务器搭建指南:1.编译安装nginx和rtmp module,下载源码.```https://nginx.org/download/nginx-1.27.2.tar.gzhttps://github.com/arut/nginx-rtmp-module.git```生成makefile```./configure --prefix=/Users/linus/nginx \--with-cc-opt=-Wno-unused-but-set-variable \--add-module=/Users/linus/nginx-rtmp-module;```2.配置nginx,在nginx.conf末尾追加```rtmp {server {listen 1935; # RTMP 端口chunk_size 4096;allow publish 127.0.0.1;deny publish all;application live {live on; # 开启直播record off; # 关闭录制(如果不需要)}}}```3.启动nginx4.运行本代码python audio-rtmp.py5.打开vlc,选择网络播放,地址rtmp://127.0.0.1/live/stream'''def start_rtmp_server():# 初始化 PyAudioaudio = pyaudio.PyAudio()# 打开音频流stream = audio.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK)# 启动 FFmpeg 进程ffmpeg_cmd = ['ffmpeg','-f', 's16le', # 输入格式'-ar', str(RATE), # 采样率'-ac', str(CHANNELS), # 声道数'-i', '-', # 输入来自标准输入'-c:a', 'aac', # 音频编码器'-f', 'flv', # 输出格式RTMP_URL]process = subprocess.Popen(ffmpeg_cmd, stdin=subprocess.PIPE)print(f"开始录音并推送到 RTMP 服务器{RTMP_URL}")try:while True:try:# 从音频流中读取数据data = stream.read(CHUNK)# 将字节数据转换为 NumPy 数组audio_data = np.frombuffer(data, dtype=np.int16)# 进行降噪处理reduced_noise = nr.reduce_noise(y=audio_data, sr=RATE)# 将处理后的数据转换回字节格式output_data = reduced_noise.astype(np.int16).tobytes()# 将处理后的数据写入 FFmpeg 进程process.stdin.write(output_data)except BrokenPipeError:print('[warn] pipe write.')#except Exception:# print('[warn] opps.')except KeyboardInterrupt:print("停止录音.")# 清理资源stream.stop_stream()stream.close()audio.terminate()process.stdin.close()process.wait()def choose_device():# 初始化 PyAudioaudio = pyaudio.PyAudio()# 列出所有音频输入设备for i in range(audio.get_device_count()):device_info = audio.get_device_info_by_index(i)if device_info['maxInputChannels'] > 0:print(f"Device {i}: {device_info['name']}")audio.terminate()print(40 * '*')print('>>请输入麦克风编号:', end='', flush=True)DEVICE_INDEX = sys.stdin.readline().strip()def create_rtmp_url():'''如果使用腾讯云直播服务,创建推流地址'''domain = 'push.tlivecloud.com'app_name = 'live'stream_name = 'stream'key = 'key123'current_date = datetime.now()next_month = current_date + timedelta(days=30)_time = int(next_month.timestamp())hex_time = str(hex(_time))[2:].upper()_txSecret = key + stream_name + hex_timetxSecret = hashlib.md5(_txSecret.encode('utf-8')).hexdigest()txTime = hex_timertmp_url = f'rtmp://{domain}/{app_name}/{stream_name}?txSecret={txSecret}&txTime={txTime}'print('rtmp_url:', rtmp_url)return rtmp_urlif __name__ == '__main__':#choose_device()start_rtmp_server()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。