1、sounddevice通过ffmpeg读取rstp远程声音
*** samples维度是samples_per_read指定 ***
##用全路径,调用系统ffmpeg,直接conda里运行不然容易出错
C:/Users/loong/.conda/envs/nlp/python.exe D:\sound\ffmpeg_test.py
ffmpeg_test.py:
import sounddevice as sd
import numpy as np
import ffmpeg
# 音频设置
sample_rate = 44100
channels = 1
samples_per_read = 1024
# 打开RTSP流并连接到音频部分
# rtsp_url = 'rtsp://username:password@ip_address:port/stream_path'
rtsp_url = "rtsp://a******nnels/2?tcp"
input_stream = ffmpeg.input(rtsp_url).audio
print("##"*18)
print("input_stream:",input_stream)
print("##"*18)
# 配置音频处理管道和音频流
ffmpeg_process = (
input_stream.output('pipe:', format='wav')
.run_async(pipe_stdout=True)
.stdout
)
# ffmpeg_process = ffmpeg.input(
# rtsp_url
# ).output(
# 'pipe:',
# format='f32le',
# acodec='pcm_f32le',
# ac=channels,
# ar=sample_rate,
# loglevel='quiet',
# ).run_async(pipe_stdout=True)
# 创建音频流
with sd.InputStream(channels=channels, dtype='float32', samplerate=sample_rate) as stream:
while True:
# 从FFmpeg进程中读取音频数据
data = ffmpeg_process.read(samples_per_read * channels * 4) # 每个样本32位浮点数,乘以4
if not data:
break
# 将音频数据转换为浮点数数组
samples = np.frombuffer(data, dtype=np.float32)
print(samples.shape, samples)
# 根据需要进行音频处理
# ...
# 在此处可以使用处理后的音频数据
# ...
# 读取下一批音频数据
samples, _ = stream.read(samples_per_read) # 阻塞式读取
# 停止FFmpeg进程,释放资源
ffmpeg_process.wait()
ffmpeg_process.terminate()
2、 ffmpegzai conda环境里执行不了,在系统可以运行
import ffmpeg
stream = ffmpeg.input(r'D:\sound\222.mp4')
stream = ffmpeg.filter(stream, 'fps', fps=25, round='up')
stream = ffmpeg.output(stream, r'D:\sound\dummy2.mp4')
ffmpeg.run(stream)
会报错:
File “C:\Users\loong.conda\envs\nlp\lib\subprocess.py”, line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
解决方法,用全部绝对路径执行可以正常运行
C:/Users/loong/.conda/envs/nlp/python.exe D:\sound\ffmpeg_sounddevice.py