-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomkv
executable file
·193 lines (170 loc) · 4.33 KB
/
tomkv
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
#!/bin/sh
#
# tomkv
# - convert all arguments to mkv files
# resulting mkv files are stored in $PWD/"mkv"
#
# name of directory in PWD where we will put mkv files
mkvdir=mkv
# name of output file, tracked in case of interrupt
out=""
# how many files have been completed
completedCount=0
# how many bytes have been processed
completedSize=0
# how many files have been processed in this run
processedCount=0
# how many bytes have been processed in this run
processedSize=0
# total number of files we are working on
total=$#
# total number of bytes left to process
totalSize=0
# start time in Julian seconds
start=$(date +%s)
# name of script
prog=$(basename "$0")
# length of date string
tmp=$(date +"%H:%M:%S")
dateLen=${#tmp}
# print final report
finish() {
echo "`date` --- processed ${processedCount} files."
}
setup() {
# no arguments = process all mp4 files in $PWD
extlist=(mp4 mov)
found=1
if [ $# = 0 ]; then
found=0
for ext in ${extlist[@]}; do
set *.$ext
if [ "x$*" != 'x*.'"$ext" ]; then
found=1
break
fi
done
fi
if [ $found = 0 ]; then
echo "${prog}: No files specified and no files found with extension: ${extlist[@]}"
exit 2
fi
if [ ! -d "${mkvdir}" ]; then
echo "$0: directory ${mkvdir} missing. Creating it."
mkdir -p "${mkvdir}"
fi
}
cleanup() {
if [ "${out}xx" != xx ]; then
echo "### ${prog}: User interrupt; deleting $out"
rm "${out}"
else
echo "### ${prog}: User interrupt"
fi
finish
exit 1
}
# report printed as each files starts processing
report() {
mp4="$1"
now=$(date +%s)
delta=$((now-start));
etaMessagePrefix="ETA "
etaMessageLength=$((dateLen+${#etaMessagePrefix}))
remainingCount=$((total-completedCount))
etaMessage=$(printf "%-${etaMessageLength}s" "no ETA")
averageMin="00"
averageSec="00"
if [ ${processedCount} -gt 0 ]; then
# size-based guess of remaining time (using factor of 1000 since bash has no floating point)
K=100000000000
processingRateK=$((delta*K / processedSize))
remainingSizeInBytes=$((totalSize - completedSize))
remainingTimeInSeconds=$(( (processingRateK * remainingSizeInBytes) / K ))
average=$((delta/completedCount))
averageMin=$(printf "%02.2d" $((average/60)) )
averageSec=$(printf "%02.2d" $((average%60)) )
# eta="$((now+average*remainingCount))"
eta=$((now+remainingTimeInSeconds))
if [ $remainingCount = 0 ]; then
etaMessage=$(printf "%-${etaMessageLength}s" "ETA: now")
else
etaMessage="${etaMessagePrefix}$(date -r $eta +"%H:%M:%S")"
fi
fi
# show completedCount+1 as the # of the file we are *currently* working on
displayCount=$(printf "%03.3d" $((completedCount+1)) )
echo "`date` -- #${displayCount} / $total; ${averageMin}m${averageSec}s average, ${etaMessage} --- processing ${mp4}"
}
calculateSizes() {
# iterate over all arguments and calculate remaining size
completedCount=0
completedSize=0
totalSize=0
total=$#
for mp4; do
out=${mkvdir}/"${mp4%.*}".mkv
mp4Size=$(stat -f %z "${mp4}")
if [ -r "${out}" ]; then
((completedCount++))
((completedSize += mp4Size))
echo "# ${out} already exists - skipping"
continue
fi
((totalSize += mp4Size))
done
}
convert() {
# iterate over all arguments and convert
processedCount=0
processedSize=0
start=$(date +%s)
mkdir -p "${mkvdir}" || exit 1
for videoFile; do
out=${mkvdir}/"${videoFile%.*}".mkv
if [ -r "${out}" ]; then
continue
fi
report "${videoFile}"
# use `nice` to be .. nice
nice ffmpeg -i "${videoFile}" "${out}" > /dev/null 2>&1
# ensure timestamp is the same
touch -r "${videoFile}" "${out}"
((completedCount++))
size=$(stat -f %z "${videoFile}")
((completedSize += size))
((processedSize += size))
((processedCount++))
done
}
# ^C cleans up and exits
trap 'cleanup' INT
# check the first argument
if [ $# -gt 0 -a -d "${1}" ]; then
for dir; do
echo "### ${prog}: processing directory $dir"
cd "$dir"
vlist=()
for video in *.[Mm][Pp]4 *.[Mm][Oo][Vv]; do
if [ "x${video}" = = "x*.[Mm][Pp]4x" -o "x${video}x" = "x*.[Mm][Oo][Vv]x" ]; then
continue;
fi
vlist=("${vlist[@]}" "$video")
done
if [ ${#vlist[@]} = 0 ]; then
echo "### ${prog}: no video files"
else
echo "### converting ${#vlist[@]} files."
calculateSizes "${vlist[@]}"
convert "${vlist[@]}"
echo "### ${prog}: finished directory $dir"
fi
echo ""
cd ..
done
else
setup "$@"
calculateSizes "$@"
convert "$@"
fi
finish