-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt.sh
executable file
·208 lines (177 loc) · 4.93 KB
/
pt.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
#!/usr/bin/env bash
FFMPEG=/usr/bin/ffmpeg
FFPROBE=/usr/bin/ffprobe
JHEAD=/usr/bin/jhead
IMG_NAME_PATTERN="img_%y%m%d_%H%M%S"
VIDEOEXT="mkv"
FFMPEG_PARAMS="-c:v libx265 -crf 22 -preset fast"
DELIMITER="=========="
MODIFICATOR=$1
function renamePhoto() {
local ISTEST=$1
for file in "${@:2}"; do
if [ ${ISTEST} -eq 1 ]
then
echo "${JHEAD} -n${IMG_NAME_PATTERN} -autorot ${file}"
else
${JHEAD} -n${IMG_NAME_PATTERN} -autorot "${file}"
fi
done
}
function showVideoInfo() {
for file in "${@:1}"; do
echo "${DELIMITER} ${file} ${DELIMITER}"
${FFPROBE} "${file}"
done
}
function showVideoInfoDate() {
for file in "${@:1}"; do
local DATE=$(getVideoDate "${file}")
echo "${file} creation date: ${DATE}"
done
}
function getVideoDate() {
local INFO=`${FFPROBE} "$1" 2>&1`
local DATETIME=`echo "${INFO}" \
| grep -iE -m 1 "(creation_time|creationdate)"`
if [ ${#DATETIME} -eq 0 ]
then
echo ""
else
echo "${DATETIME}" \
| egrep -o -- "[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}(T[[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2})?" \
| sed -e "s/-//g;s/:https://g;s/T/_/g"
fi
}
function createFileName() {
local NAME=$1
local EXT=$2
local COUNTER=$3
if [ -z "$COUNTER" ]
then
echo "VID${NAME}.${EXT}"
else
echo "VID${NAME}-${COUNTER}.${EXT}"
fi
}
function checkFileExists() {
local DIR=$1
local NAME=$2
local EXT=$3
local COUNTER=0
local FULL_NAME="${DIR}/$(createFileName ${NAME} ${EXT})"
while [ -f "${FULL_NAME}" ]; do
COUNTER=$((COUNTER+1))
FULL_NAME="${DIR}/$(createFileName ${NAME} ${EXT} ${COUNTER})"
done
echo "$FULL_NAME"
}
function renameVideo() {
local ISTEST=$1
for file in "${@:2}"; do
local DATE=$(getVideoDate "${file}")
local DIR=`dirname "${file}"`
local FNAME=$(basename "$file")
local EXT="${FNAME##*.}"
if [ -z "$DATE" ]
then
local FULL_NAME="$DIR/$FNAME"
else
local FULL_NAME=$(checkFileExists "$DIR" "$DATE" "$EXT")
fi
if [ ${ISTEST} -eq 1 ]
then
echo "mv ${file} ${FULL_NAME}"
else
mv "${file}" "${FULL_NAME}"
fi
done
}
function encodeVideo() {
local ISTEST=$1
for file in "${@:2}"; do
local DATE=$(getVideoDate "${file}")
local DIR=`dirname "${file}"`
if [ -z "$DATE" ]
then
local FNAME=$(basename "$file")
local FILENAME="${FNAME%.*}"
if [ "${FILENAME:0:3}" = VID ]
then
local FULL_NAME=$(checkFileExists "$DIR" "${FILENAME:4}" "$VIDEOEXT")
else
local FULL_NAME=$(checkFileExists "$DIR" "$FILENAME" "$VIDEOEXT")
fi
else
local FULL_NAME=$(checkFileExists "$DIR" "$DATE" "$VIDEOEXT")
fi
if [ ${ISTEST} -eq 1 ]
then
echo "${FFMPEG} -i ${file} ${FFMPEG_PARAMS} ${FULL_NAME}"
else
echo "${DELIMITER} ${file} -> ${FULL_NAME} ${DELIMITER}"
${FFMPEG} -i "${file}" ${FFMPEG_PARAMS} "${FULL_NAME}"
fi
done
}
function concatVideo() {
local ISTEST=$1
local FILES=("${@:2}")
local FNAME=$(basename "${FILES[0]}")
local DIR=$(dirname "${FILES[0]}")
local FILENAME="${FNAME%.*}"
local CONCAT="${DIR}/${FILENAME}-list.txt"
local OUTPUT="${DIR}/${FILENAME}-full.${VIDEOEXT}"
for file in "${FILES[@]}"; do
if [ ${ISTEST} -eq 1 ]
then
echo "echo \"file '${file}'\" >> ${CONCAT}"
else
echo "file '$file'" >> "${CONCAT}"
fi
done
if [ ${ISTEST} -eq 1 ]
then
echo "${FFMPEG} -f concat -safe 0 -i ${CONCAT} -c copy ${OUTPUT}"
echo "rm -f ${CONCAT}"
else
${FFMPEG} -f concat -safe 0 -i "${CONCAT}" -c copy "${OUTPUT}"
rm -f "${CONCAT}"
fi
local COUNTER=0
for file in "${FILES[@]}"; do
local FNAME=$(basename "${file}")
local DIR=$(dirname "${file}")
local FILENAME="${FNAME%.*}"
local EXT="${FNAME##*.}"
COUNTER=$((COUNTER+1))
if [ ${ISTEST} -eq 1 ]
then
echo "mv "${file}" "${DIR}/${FILENAME}-concat-${COUNTER}.${EXT}""
else
mv "${file}" "${DIR}/${FILENAME}-concat-${COUNTER}.${EXT}"
fi
done
}
case ${MODIFICATOR} in
'photo-rename')
renamePhoto 0 "${@:2}" ;;
'photo-rename-test')
renamePhoto 1 "${@:2}" ;;
'video-encode')
encodeVideo 0 "${@:2}" ;;
'video-encode-test')
encodeVideo 1 "${@:2}" ;;
'video-info')
showVideoInfo "${@:2}" ;;
'video-info-date')
showVideoInfoDate "${@:2}" ;;
'video-rename')
renameVideo 0 "${@:2}" ;;
'video-rename-test')
renameVideo 1 "${@:2}" ;;
'video-concat')
concatVideo 0 "${@:2}" ;;
'video-concat-test')
concatVideo 1 "${@:2}" ;;
esac