Sino的研究基地

自动清理EMBY转码目录的脚本(Linux)

在之前使用Emby观看直播时,我发现到transcoding-temp会一直保留直播放开始以来的所有视频切片,这常常导致服务器磁盘使用率达到100%。为了避免因为磁盘空间不足引发一些严重问题的情况,我创建了RAM磁盘用来存储这些临时文件,并且减少磁盘的读写

尽管这并不能从根本上解决问题,当时我很少看直播,因此没怎么重视。直到最近,由于我开始长时间看直播,然后我的内存磁盘满了….

我决定这次认真处理这个问题。(已经过去了5年,Emby团队仍然没有解决这个问题)

首先~直接放脚本~

#!/bin/bash

target_directory="/tmp/tmpfs/transcoding-temp"
max_size=24000  # MB


get_directory_size() {
    du -sm "$1" | awk '{print $1}'
}

while [ "$(get_directory_size "$target_directory")" -gt $max_size ]; do
    old_file=$(find "$target_directory" -type f -name "*.ts" -size +1 -printf '%T+ %p\n' | sort | head -n 1 | awk '{print $2}')

    if [ -z "$old_file" ]; then
        break
    fi

    echo "" > "$old_file"
    echo "clean: $old_file"
done

echo "Operation Completed"

大概解释一下:

如何使用呢?

第一步首先上传脚本到服务器(假设在/opt/clean-transcoding-temp.sh)

第二步给予脚本执行权限~

chmod 711 /opt/clean-transcoding-temp.sh

第三步创建任务计划~

crontab -e

添加一行任务~

*/5 * * * *  /opt/clean-transcoding-temp.sh
#上面的*/5 * * * *  /opt/clean-transcoding-temp.sh 代表着每5分钟运行/opt/clean-transcoding-temp.sh
*/5    *    *    *    *   /opt/clean-transcoding-temp.sh
-      -    -    -    -   -
|      |    |    |    |   |
|      |    |    |    |   +----- 启动的命令或脚本
|      |    |    |    +----- 一周的某一天 (0 - 7) (星期天=0 或者 7) 或者 sun,mon,tue,wed,thu,fri,sat
|      |    |    +---------- 月 (1 - 12) 或者 jan,feb,mar,apr ...
|      |    +--------------- 1个月的某一天 (1 - 31)
|      +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)

最后欢迎评论点赞哦(o^_^o)