录制、连接和同步Twitch实时流
tdh-twitch-utils的Python项目详细描述
用于抽搐的python实用程序
pip3 install tdh-twitch-utils
本模块由三个不同的脚本组成:
concat
此脚本使用MPEG-TS时间戳连接多个 无需重新编码即可将视频片段转换为单个文件。它是 最适用于组合部分流记录以防 流下载期间的中断或错误。重叠 使用ffmpeg的concat demuxer将精确地删除部分。
示例
# download two overlapping segments (60 seconds each)
VOD="YOUR VOD ID"
streamlink -o 1.ts --hls-duration 60 "twitch.tv/videos/$VOD" best
streamlink -o 2.ts --hls-start-offset 30 --hls-duration 60 "twitch.tv/videos/$VOD" best
# concatenate two segments into one video
twitch_utils concat 1.ts 2.ts -o result.mp4
记录
此脚本可用于录制实时流而无需等待 让他们结束。它立即开始录制直播, 然后下载视频点播并将其连接到全流录制。
显然,这个脚本要求频道有公共的视频点播和流。
算法:
- 检查信道是否存在,当前流的VOD已经存在;
- 从Twitch API获取VOD ID(使用Twitch Chat Downloader库的一部分);
- 开始将实时流下载到文件
VOD.end.ts
; - 等待10分钟,开始将VOD下载到文件
VOD.start.ts
; - 等待两次下载完成;
- 通过
concat
脚本连接两个部分(见上文)。
带宽限制是通过streamlink的管道输出实现的
通过pv
实用程序,因此需要安装此程序
使用-b
标志。
示例
# Record live stream of channel 'blackufa' using 2 threads and
# limiting bandwidth to 2 MiB/s
twitch_utils record blackufa -j 2 -b 2M
偏移量
此脚本执行两个音频文件的相互关联以查找
它们之间的偏移量。第一个参数被裁剪并用作模板。
第二个参数可以有任何持续时间--它将被分为
分开块以减少内存使用(否则
可以使用非常大的文件)。两个参数都可以是
视频或音频文件-音频曲目将被提取和转换。
如果ffprobe
能够正确地确定
第二个参数的持续时间。
示例
# Cut small segment from big video file (offset: 123 seconds)
ffmpeg -ss 123 -i YOUR_FILE.mp4 -t 60 -c copy template.mp4
# Find offset of template.mp4 within YOUR_FILE.mp4
twitch_utils offset template.mp4 YOUR_FILE.mp4
# ... returns 122.99997732426303
# Same command, but result will be rounded to nearest integer
twitch_utils offset template.mp4 YOUR_FILE.mp4 --round
# ... returns 123