根据时间段自动调节pulseaudio音量

本脚本根据当前时间自动调整PulseAudio音量,适用于服务器环境,无需手动干预。通过定时任务或启动时执行,可在不同时间段设定不同的音量级别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据本地时间 自动调节pulseaudio音量
例如 防止夜间音量过大 0点-8点前 音量设置为 25% 8点-23点 音量设置为50%
适用于服务器环境,无需专门登录到服务器手动操作

#!/bin/sh

#脚本路径 /usr/local/bin/pulse-autovolume

#set -x

PATH=/bin:/usr/bin:/sbin:/usr/sbin

[  -x `which pulseaudio` ] || exit 1
[ -x `which pactl` ] || exit 1


# 手动开关标记文件,用于定时脚本执行
# /tmp/pulseaudio-autovolume
# 为0 表示 取消这种特性 
# 不等于0 表示开启

AUTOFILE=/tmp/pulseaudio-autovolume

if [ ! -f $AUTOFILE ];then 
touch $AUTOFILE
echo "1" | tee $AUTOFILE
fi

chmod 777 $AUTOFILE

ISRUN=`head -1 $AUTOFILE`
if [ ${ISRUN} -eq 0 ]; then
exit 1
fi


#初始音量  25% 指定时间段 50%
VOL="25%"
HOUROFNOW=`date +"%H"|awk '{print int($0)}'`
#上午8点以后 音量调高  之前调低 , 到午夜0点小时数为0

if [ $HOUROFNOW -ge 8 ]; then
VOL="50%"
fi

#指定pulseaudio 要设置音量的 
#默认情况使用pulseaudio 默认的sink 

PULSESINK=`LC_ALL=C pactl info|grep 'Default Sink'|awk -F ":" '{print $2}'|sed 's/^[ ]*//g'`
if [ $PULSESINK ]; then
pactl  set-sink-volume ${PULSESINK} ${VOL}
fi

脚本路径 /usr/local/bin/pulse-autovolume

1.可放在systemctl cat pulseaudio
这样每次启动pulseaudio后可根据当前时间调节音量
添加
ExecStartPost=/usr/bin/sh /usr/local/bin/pulse-autovolume

2.可放在/etc/crontab 定时执行
注意pulseaudio的运行用户 系统级别的还是用户级别的
添加 在每天0点 8点 16点执行一次
1 0,8,16 * * * root sh /usr/local/bin/pulse-autovolume

3 编辑 /tmp/pulseaudio-autovolume 设置为0 关闭脚本功能 为非0的值则表示开启该功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值