-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoTools.sh
217 lines (197 loc) · 5.3 KB
/
videoTools.sh
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/bash
#
# Video Tools
#
# Dependency:
# annie: get video's info & download videos
# https://github.com/iawia002/annie
# ffprobe: get video's resolution
# part of ffmpeg
# danmaku2ass: convert xml danmaku to ass
# https://github.com/m13253/danmaku2ass
# Usage:
# see `usage()` or `${basename $0} help`
#
ORIGIN_COOKIES="${HOME}/Downloads/cookies.txt"
COOKIES="./bilibili.cookies"
DOWNLOAD_LIST="./.download.list"
INFO_LIST="./download.info"
INFO_LIST_TMP="./.download.info.tmp"
MOVE_DIR="${HOME}/Videos"
echoWarning() {
echo -ne "\033[33;1mWARNING:\033[0m "; echo $*
}
echoError() {
echo -ne "\033[31;1mERROR:\033[0m "; echo $*
}
makeBilibiliCookies() {
if [[ -f "${ORIGIN_COOKIES}" ]]; then
echo -n "Generating Bilibili cookies ... "
grep 'bilibili.*\(DedeUserID\(__ckMd5\)\?\|SESSDATA\|sid\)' \
"${ORIGIN_COOKIES}" \
> "${COOKIES}"
sed -i 's/^#//' "${COOKIES}"
if [[ $? -eq 0 ]]; then
echo "DONE!"
fi
rm "${ORIGIN_COOKIES}"
else
echoWarning "Downloaded cookies not found!"
fi
}
updateInfo() {
echo -n "Processing download list ... "
sed -E 's/^#([0-9a-z])/\1/' ./download.list | grep -Eo '^[^#]+' > "${INFO_LIST_TMP}"
if [[ $? -eq 0 ]]; then
echo "DONE!"
fi
echo -n "Generating infomation list ... "
annie -p -j -c "${COOKIES}" -F "${INFO_LIST_TMP}" | \
grep -E '("title"|"url":.*www\.bilibili\.com)' \
> "${INFO_LIST}"
rm "${INFO_LIST_TMP}"
sed -i -E 's/^ +//' "${INFO_LIST}"
sed -i -E 's/("url": ".*) *#.*"/\1"/' "${INFO_LIST}"
echo "DONE!"
}
listDownload() {
catDownloadList() {
cat "${DOWNLOAD_LIST}"
}
makeDownloadList() {
if [[ ! -f "./download.list" ]]; then
echoError "Download list is not found in ./download.list"
exit 1
fi
echo -n "Processing download list ... "
sed -E 's/ *#.*$//' ./download.list | grep '.\+' > "${DOWNLOAD_LIST}"
if [[ $? -eq 0 ]]; then
echo "DONE!"
fi
}
case "$1" in
"make"|"generate" )
makeDownloadList
;;
* )
catDownloadList
;;
esac
}
download() {
if [[ ! -f "${DOWNLOAD_LIST}" ]]; then
listDownload make
fi
annie -p -C -c "${COOKIES}" -F "${DOWNLOAD_LIST}" && rm "${DOWNLOAD_LIST}"
}
xml2ass() {
getVideoRes() {
videoFile="$1"
ffprobe -v error \
-select_streams v:0 \
-show_entries stream=width,height \
-of csv=s=x:p=0 \
"${videoFile}"
}
xmlFile="$1"
xmlFileName=${xmlFile%.xml}
videoFile="${xmlFileName}.flv"
if [[ -f "${xmlFileName}.mp4" ]]; then
videoFile="${xmlFileName}.mp4"
fi
if [[ -f "${videoFile}" ]]; then
optRes=$(getVideoRes "${videoFile}")
height=${optRes#*x}
optFs=$((${height}*50/1080))
optDm=12
optAlpha=.7
echo -n "Generating ${xmlFileName}.ass ... "
danmaku2ass \
-s ${optRes} \
-fs ${optFs} \
-dm ${optDm} \
-ds ${optDm} \
-a ${optAlpha} \
-o "${xmlFileName}.ass" "${xmlFile}"
if [[ $? -eq 0 ]]; then
echo "DONE!"
fi
else
echoWarning "Video ${xmlFileName##*/} is not found!"
fi
}
makeAss() {
for i in "${1-}"*.xml; do
xml2ass "$i"
done
}
statCount() {
ls "$1"* \
| sed -E 's/(.+第([0-9]+(\.[0-9]+)?).*[^]])(\[[0-9]+])?\.[^.]{3}(\.download)?$/\2\t\1/' \
| sort -g | uniq -c
}
videoMove() {
if [[ ! -n "$1" ]]; then
echoError -n "Moving videos needs a param of PREFIX! "
echo "Use $(basename $0) help for more help."
exit 1
fi
targetDir="${MOVE_DIR}/$1"
if [[ ! -d "${targetDir}" ]]; then
echo -n "Making folder: ${targetDir} ... "
mkdir -p "${targetDir}"
echo "DONE!"
fi
echo "Moving $(ls "$1"* | wc -l) files to ${targetDir}"
rsync -aP --remove-source-files "$1"* "${targetDir}"
}
usage() {
NAME=$(basename $0)
cat << EOF
${NAME} is a video download tool
usage: ${NAME} SUBCOMMAND [OPTION]...
SUBCOMMAND:
cookie - make bilibili cookies
list [make] - list or generate download list
download - download videos and generate ass
stat [PREFIX] - show file list & counts with PREFIX of filename
ass [PREFIX] - generate ass from xml with PREFIX of filename
info - generate video infomations
move PREFIX - move files with PREFIX of filename to ${MOVE_DIR}/PREFIX
help - show this help text
EOF
}
main() {
SUBCMD="$1"
shift
case "$SUBCMD" in
"download"|"d" )
download
makeAss
;;
"list"|"ls" )
listDownload $*
;;
"cookie" )
makeBilibiliCookies
;;
"ass" )
makeAss $*
;;
"stats"|"stat" )
statCount $*
;;
"info" )
updateInfo
;;
"move" )
videoMove $*
;;
"help"|""|"-h"|"--help" )
usage
;;
* )
echoError "Vaild SUBCOMMAND, use $(basename $0) help for help."
esac
}
main $*