You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让mpv的--stream-record功能使用icecast的icy-title作为输出文件名

关于mpv是否支持按icy-title拆分录制Icecast流的解答

原生功能限制

mpv内置的--record-file、--stream-record、--dump-cache三个录制相关参数,均仅支持指定固定文件名输出,无法监听流的icy-title元数据变化自动拆分文件,原生功能无法直接满足需求。

可行实现方案

方案1:使用mpv Lua扩展脚本实现

mpv支持Lua脚本扩展,可以通过监听元数据更新事件实现自动拆分录制,具体实现逻辑如下:

  1. 监听metadata-update事件,检测icy-title字段的变化
  2. 每次icy-title更新时,停止当前录制任务,用新的标题生成合法文件名后启动新的录制任务

示例脚本参考:

local current_recording_path = nil

-- 处理文件名非法字符
local function sanitize_filename(title)
    return title:gsub("[<>:\"/\\|?*%c]", "_"):gsub("%s+", " ")
end

-- 监听元数据变化
mp.observe_property("metadata", "native", function(_, metadata)
    if not metadata or not metadata["icy-title"] then return end
    local icy_title = metadata["icy-title"]
    -- 过滤重复的元数据通知
    if current_recording_path and current_recording_path:find(icy_title, 1, true) then return end

    local new_filename = sanitize_filename(icy_title) .. ".mp3"
    -- 停止上一段录制
    if current_recording_path then
        mp.commandv("record", "stop")
    end
    -- 启动新一段录制
    mp.commandv("record", "start", new_filename)
    current_recording_path = new_filename
end)

使用方法:将上述脚本保存为icecast_split.lua,启动mpv时附加脚本即可:
mpv --script=./icecast_split.lua <Icecast流地址>

方案2:使用专用流录制工具

可以直接使用专门针对Icecast流设计的拆分录制工具,这类工具原生支持按icy-title自动拆分文件,无需自行开发脚本,使用更稳定:

  • 直接运行命令即可自动拆分保存:streamripper <Icecast流地址> -d ./歌曲保存目录

注意事项

  • 部分Icecast流的元数据更新存在1-3秒的延迟,可在脚本中启停录制前添加对应延迟,避免切歌时前后段内容重叠或缺失
  • 生成文件名时必须过滤系统不允许的特殊字符,避免文件写入失败

内容的提问来源于stack exchange,提问作者ccpizza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 18:30:05