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

如何在12点强制中断Liquidsoap流并播放指定文件(Icecast2环境)

解决Liquidsoap到点强制切换播放内容的问题

问题说明

已成功搭建Liquidsoap与Icecast2环境且运行正常,希望在12点整播放包含静默时段和国歌的单个文件。但当前配置下,若12点时有歌曲正在播放,Icecast会等待歌曲结束后才切换播放该文件,需实现12点整强制中断当前直播流并立即播放目标文件。

原配置代码

#!/root/.opam/default/bin/liquidsoap
set("init.allow_root",true)
out = output.icecast(host = "127.0.0.1",port = 8082,user = "source",password = "passforsource",name = "Streamcast",genre = "Various",url = "https://stackoverflow.com",encoding = "UTF-8")
set("server.telnet.bind_addr","127.0.0.1")
set("server.telnet",true)
dir = "/var/www/html/public/storage/radio"

music_dir = "#{dir}/music"
jingles_dir = "#{dir}/jingles"
set("log.file.path","#{dir}/radio.log")
set("log.level", 3)
jingles = playlist(reload=86400,"#{jingles_dir}")
music = playlist(reload=86400,"#{music_dir}")

national_anthem = single("/var/www/html/public/storage/radio/special-playlists/silence_then_anthem.ogg")

radio = switch([(predicate.activates({ 12h-12h3m }), national_anthem), ({ true }, rotate(weights = [15, 1],[music,jingles]))])

radio = mksafe(radio)
radio = crossfade(smart=true,duration=2.00,fade_out=1.25,fade_in=1.00,radio)
out(
 %vorbis.abr(samplerate = 44100, channels = 2, bitrate = 128, max_bitrate = 192, min_bitrate = 96),
 description = "Average vorbis 96-128-192 Kbps",
 mount = "stream",
 mksafe(radio)
)

解决方案

核心问题是默认的switch采用惰性切换逻辑,会等待当前播放的轨道自然结束才触发切换。要实现强制中断,需修改两个关键部分:

  1. 给switch添加fallback=false参数,触发条件时立即中断当前源并切换。
  2. 调整crossfade的作用范围,避免交叉淡入淡出干扰国歌的强制播放。

修改后的完整配置

#!/root/.opam/default/bin/liquidsoap
set("init.allow_root",true)
out = output.icecast(host = "127.0.0.1",port = 8082,user = "source",password = "passforsource",name = "Streamcast",genre = "Various",url = "https://stackoverflow.com",encoding = "UTF-8")
set("server.telnet.bind_addr","127.0.0.1")
set("server.telnet",true)
dir = "/var/www/html/public/storage/radio"

music_dir = "#{dir}/music"
jingles_dir = "#{dir}/jingles"
set("log.file.path","#{dir}/radio.log")
set("log.level", 3)
jingles = playlist(reload=86400,"#{jingles_dir}")
music = playlist(reload=86400,"#{music_dir}")

national_anthem = single("/var/www/html/public/storage/radio/special-playlists/silence_then_anthem.ogg")

# 核心修改:添加fallback=false实现强制切换
default_source = crossfade(smart=true,duration=2.00,fade_out=1.25,fade_in=1.00,rotate(weights = [15, 1],[music,jingles]))
radio = switch(fallback=false, [
  (predicate.activates({ 12h-12h3m }), national_anthem),
  ({ true }, default_source)
])

radio = mksafe(radio)
out(
 %vorbis.abr(samplerate = 44100, channels = 2, bitrate = 128, max_bitrate = 192, min_bitrate = 96),
 description = "Average vorbis 96-128-192 Kbps",
 mount = "stream",
 radio
)

关键修改说明

  • fallback=false:这是实现强制切换的核心参数,告诉Liquidsoap当切换条件满足时,不需要等待当前播放的内容结束,立即中断并切换到目标源。
  • 调整crossfade位置:将交叉淡入淡出逻辑移到默认播放分支中,这样国歌播放时不会被淡入淡出处理,保证内容能直接、完整地播放。
  • 简化mksafe调用:只在最终的radio流上调用一次mksafe,避免重复处理导致的异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 16:25:30