几个 shell 脚本

记录几个 shell 脚本,高效。

批量使用下划线替换路径中的空格

1
2
3
4
5
6
7
8
9
10
#!/bin/bash 

find . -name "* *"|
while read name;do

na=$(echo $name | tr ' ' '_')

mv "$name" $na

done

参考文章

Imagemagic 批量缩小图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash 

if [ ! -d re_size ];then
mkdir -p re_size
fi

filelist=$(find -iname "*.jpg")
FOLDER=$(cd `dirname $0`; pwd)


for filename in $filelist

do
tempname=${filename#*.}
newfilename=${FOLDER}${tempname}


if [ ! -d re_size${tempname%/*} ];then
mkdir -p re_size${tempname%/*}
fi

convert $newfilename -resize 800 -quality 60 ${FOLDER}/re_size${tempname}

done

参考文章1参考2

批量压制视频

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash 
# author: [email protected]
#------settings--------#

myfolder=finshed
# use sd or ipad
compatibility=sd

#-------maincode-------#

if [ $compatibility == "sd" ]
then
cut="854x480"
videorate="500k"
audiorate="64k"
speed="fast"
profile="-x264-params \"profile=high:level=4.0\""
out="480p"

elif [ $compatibility == "ipad" ]
then
cut="1280x720"
videorate="2200k"
audiorate="128k"
speed="slow"
profile="-x264-params \"profile=high:level=4.2\""
out="720p"
else
exit

fi


if [ ! -d $myfolder ];then
mkdir -p $myfolder
fi

filelist=$(find \( -iname "*.mp4" -o -iname "*.mkv" -o -iname "*.ts" -o -iname "*.avi" -o -iname "*.wmv" \) -a ! -name "*_$out.mp4")
FOLDER=$(cd `dirname $0`; pwd)

for filename in $filelist

do
tempname=${filename#*/}
newfilename=${FOLDER}/${tempname}
ffmpegcode="ffmpeg -i $newfilename -s $cut -c:v libx264 $profile -preset $speed -b:v $videorate"
isempty=${filename#*.}
isempty=${isempty%/*}

if [ ! -d $myfolder$isempty ];then

mkdir -p $myfolder$isempty
fi

nohup $ffmpegcode -pass 1 -an -f mp4 -y /dev/null && $ffmpegcode -pass 2 -c:a aac -b:a $audiorate -strict -2 ${FOLDER}/$myfolder/${tempname%.*}_$out.mp4 >/dev/null 2>&1

done

rm -f ffmpeg2pass*
echo "finished!"
exit
---------本文结束感谢阅读---------
Title - Artist
0:00