# 等待直到结束时间或ffmpeg自然结束 whiletrue; do current_hour=$(date +%H) current_min=$(date +%M) if [ $current_hour -eq $end_hour ] && [ $current_min -eq $end_min ]; then # 检查ffmpeg是否还在运行,并杀死它 ifkill -0 $ffmpeg_pid 2>/dev/null; then echo"Killing ffmpeg process with PID $ffmpeg_pid" kill$ffmpeg_pid else # 如果运行多份脚本,下行的提示可以忽略。 echo"ffmpeg process with PID $ffmpeg_pid does not exist or has already been killed" fi break fi
echo"Waiting ffmpeg to stop: $(date)" sleep 60 done
echo"ffmpeg has been stopped or reached the end time."
# 遍历时间段列表 for slot in"${time_slots[@]}"; do # 分割开始时间和结束时间 start_time=${slot%-*} end_time=${slot#*-} # 转换为小时和分钟 start_hour=$((10#${start_time:0:2})) start_min=$((10#${start_time:2:2})) end_hour=$((10#${end_time:0:2})) end_min=$((10#${end_time:2:2}))
# 等待直到开始时间 whiletrue; do current_hour=$(date +%H) current_min=$(date +%M) if [ $current_hour -eq $start_hour ] && [ $current_min -eq $start_min ]; then break fi echo"Waiting for ${start_hour}:${start_min} to start ffmpeg: $(date)" sleep 60 done
# 启动ffmpeg并保存PID echo"Starting ffmpeg for ${start_hour}:${start_min} - ${end_hour}:${end_min}..." # 如果运行多份脚本并且启动录制的时间相同,请确保 output_filename 不会重名。 file_prefix=$(date +%Y%m%d) output_filename="${file_prefix}_${start_time}_${end_time}.aac" ffmpeg -i http://ngcdn001.cnr.cn/live/zgzs/index.m3u8 -vn -c:a copy "$output_filename" & ffmpeg_pid=$! # 等待直到结束时间或ffmpeg自然结束 whiletrue; do current_hour=$(date +%H) current_min=$(date +%M) if [ $current_hour -eq $end_hour ] && [ $current_min -eq $end_min ]; then # 检查ffmpeg是否还在运行,并杀死它 ifkill -0 $ffmpeg_pid 2>/dev/null; then echo"Killing ffmpeg process with PID $ffmpeg_pid" kill$ffmpeg_pid else # 如果运行多份脚本,下行的提示可以忽略。 echo"ffmpeg process with PID $ffmpeg_pid does not exist or has already been killed" fi break fi echo"Waiting for ffmpeg to stop or ${end_hour}:${end_min}: $(date)" sleep 60 done done
echo"All ffmpeg sessions have been stopped or reached their end times."