注意
单击此处下载完整的示例代码
StreamWriter 高级用法¶
作者: Moto Hira
注意
本教程使用硬件设备,因此无法跨 不同的操作系统。
本教程在 MacBook Pro(M1,2020)上编写和测试。
注意
本教程需要 FFmpeg 库。 请参考 FFmpeg 依赖 细节。
警告
TorchAudio 动态加载兼容的 FFmpeg 库 已安装在系统上。 支持的格式类型(媒体格式、编码器、编码器 options 等)取决于库。
要检查可用的设备、多路复用器和编码器,您可以使用 以下命令
ffmpeg -muxers
ffmpeg -encoders
ffmpeg -devices
ffmpeg -protocols
制备¶
import torch
import torchaudio
print(torch.__version__)
print(torchaudio.__version__)
from torchaudio.io import StreamWriter
from torchaudio.utils import download_asset
AUDIO_PATH = download_asset("tutorial-assets/Lab41-SRI-VOiCES-src-sp0307-ch127535-sg0042.wav")
VIDEO_PATH = download_asset(
"tutorial-assets/stream-api/NASAs_Most_Scientifically_Complex_Space_Observatory_Requires_Precision-MP4_small.mp4"
)
设备可用性¶
StreamWriter
利用 FFmpeg 的 IO 抽象和
将数据写入媒体设备,例如扬声器和 GUI。
要写入设备,请为构造函数提供 option
之。format
StreamWriter
不同的操作系统将具有不同的设备选项及其可用性 取决于 FFmpeg 的实际安装。
要检查哪个设备可用,您可以使用 ffmpeg -devices 命令。
“audiotoolbox”(扬声器)和 “sdl”(视频 GUI) 可用。
$ ffmpeg -devices
...
Devices:
D. = Demuxing supported
.E = Muxing supported
--
E audiotoolbox AudioToolbox output device
D avfoundation AVFoundation input device
D lavfi Libavfilter virtual input device
E opengl OpenGL output
E sdl,sdl2 SDL2 output device
有关哪些设备在哪些操作系统上可用的详细信息,请查看 官方 FFmpeg 文档。https://ffmpeg.org/ffmpeg-devices.html
播放音频¶
通过提供 option,StreamWriter 写入
数据到扬声器设备。format="audiotoolbox"
# Prepare sample audio
waveform, sample_rate = torchaudio.load(AUDIO_PATH, channels_first=False, normalize=False)
num_frames, num_channels = waveform.shape
# Configure StreamWriter to write to speaker device
s = StreamWriter(dst="-", format="audiotoolbox")
s.add_audio_stream(sample_rate, num_channels, format="s16")
# Write audio to the device
with s.open():
for i in range(0, num_frames, 256):
s.write_audio_chunk(0, waveform[i : i + 256])
播放视频¶
要播放视频,您可以使用 或 。
同样,您需要一个具有相应集成的 FFmpeg 版本
启用。可用设备可以使用 进行检查。format="sdl"
format="opengl"
ffmpeg -devices
在这里,我们使用 SDL 设备 (https://ffmpeg.org/ffmpeg-devices.html#sdl)。
# note:
# SDL device does not support specifying frame rate, and it has to
# match the refresh rate of display.
frame_rate = 120
width, height = 640, 360
为此,我们定义了一个辅助函数,该函数将视频加载委托给 一个后台线程并给出 chunks
running = True
def video_streamer(path, frames_per_chunk):
import queue
import threading
from torchaudio.io import StreamReader
q = queue.Queue()
# Streaming process that runs in background thread
def _streamer():
streamer = StreamReader(path)
streamer.add_basic_video_stream(
frames_per_chunk, format="rgb24", frame_rate=frame_rate, width=width, height=height
)
for (chunk_,) in streamer.stream():
q.put(chunk_)
if not running:
break
# Start the background thread and fetch chunks
t = threading.Thread(target=_streamer)
t.start()
while running:
try:
yield q.get()
except queue.Empty:
break
t.join()
现在我们开始流式传输。按“Q”将停止视频。
注意
write_video_chunk 对 SDL 设备块进行调用,直到 SDL 完成 播放视频。
# Set output device to SDL
s = StreamWriter("-", format="sdl")
# Configure video stream (RGB24)
s.add_video_stream(frame_rate, width, height, format="rgb24", encoder_format="rgb24")
# Play the video
with s.open():
for chunk in video_streamer(VIDEO_PATH, frames_per_chunk=256):
try:
s.write_video_chunk(0, chunk)
except RuntimeError:
running = False
break
[法典]
流式视频¶
到目前为止,我们了解了如何写入硬件设备。有一些 视频流的替代方法。
RTMP(实时消息收发协议)¶
使用 RMTP,您可以将媒体(视频和/或音频)流式传输到单个客户端。 这不需要硬件设备,但需要单独的播放器。
要使用 RMTP,请在
StreamWriter 构造函数,然后在打开
目的地。dst
{"listen": "1"}
StreamWriter 将侦听端口并等待客户端请求视频。
在收到请求之前,将阻止对 的调用。open
s = StreamWriter(dst="rtmp://localhost:1935/live/app", format="flv")
s.add_audio_stream(sample_rate=sample_rate, num_channels=num_channels, encoder="aac")
s.add_video_stream(frame_rate=frame_rate, width=width, height=height)
with s.open(option={"listen": "1"}):
for video_chunk, audio_chunk in generator():
s.write_audio_chunk(0, audio_chunk)
s.write_video_chunk(1, video_chunk)
[法典]
UDP(用户数据报协议)¶
使用 UDP,您可以将媒体(视频和/或音频)流式传输到套接字。 这不需要硬件设备,但需要单独的播放器。
与 RTMP 不同,流式处理和客户端进程是断开连接的。 流式处理进程不知道客户端进程。
s = StreamWriter(dst="udp://localhost:48550", format="mpegts")
s.add_audio_stream(sample_rate=sample_rate, num_channels=num_channels, encoder="aac")
s.add_video_stream(frame_rate=frame_rate, width=width, height=height)
with s.open():
for video_chunk, audio_chunk in generator():
s.write_audio_chunk(0, audio_chunk)
s.write_video_chunk(1, video_chunk)
[法典]
脚本总运行时间:(0 分 0.000 秒)