我是一名媒体收藏爱好者,最近启动了一个项目,使用我拥有的媒体作为自托管媒体服务器。我已经手动完成所有工作有一段时间了,但开始使用 shell 脚本来自动执行大量输入。在我用于压缩剧集季节的脚本中,我无法让脚本检测文件名上的空格和连字符。
这是我的脚本:
echo "Warning! Please ensure before running this script
that the files you'd like to convert are in the following format:
'series_name - SXXEYY' and mind that by running this script
you're aware that the uncompressed files are goint to be
automatically deleted. If you change your mind press 'Ctrl + C'
to cancel or press [ENTER] to continue:"
read
echo "Enter destination of the input folder:"
read input
echo "Enter series name(how it's written inside the folder):"
read series_name
echo "Enter season number:"
read season_no
echo "Enter number of episodes in the season:"
read ep_no
echo "Enter destination of the output folder:"
read output
for i in $(seq -f "%02g" 1 $ep_no); do
input_file="$input/"$series_name - S${season_no}E${i}.mkv""
output_file="$output/"$series_name - S${season_no}E${i}.mkv""
ffmpeg -i "$input_file" -c:v libx264 -crf 22 -c:a copy -c:s copy -map 0 "$output_file"
done
rm -rf "$input"
clear
echo "Done :)"
这是我保存文件的方法:
nesco@nesker:~/uncompressed/Rome$ ls
'Rome - S01E01.mkv' 'Rome - S01E02.mkv'
我目前正在再次提取原始文件,因为上次我运行脚本时它没有检测到并删除了文件夹
你为什么费心去询问季数和集数?您通常只想在删除所有节目之前压缩某些节目吗?为什么不直接将“输入”目录中的所有文件处理到“输出”目录中:
#!/bin/bash
echo "Warning! Please ensure before running this script
that the files you'd like to convert are in the following format:
'series_name - SXXEYY' and mind that by running this script
you're aware that the uncompressed files are going to be
automatically deleted. If you change your mind press 'Ctrl + C'
to cancel or press [ENTER] to continue:"
read
read -p "Enter destination of the input folder: " input
read -p "Enter destination of the output folder: " output
if [ ! -d "$input" ]; then
echo "Error: $input is not a directory."
exit 1
fi
if [ ! -d "$output" ]; then
echo "Error: $output is not a directory."
exit 1
fi
set -e # force the shell to exit if commands fail.
for input_file in "${input}"/*; do
output_file="$output"/$(basename "$input_file")
ffmpeg -i "$input_file" -c:v libx264 -crf 22 -c:a copy -c:s copy -map 0 "$output_file"
done
rm -rf "$input"
# clear # why do this?
echo "Done :)"
请注意,我进行了一些错误检查,这非常重要,因为您要删除数据。