-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-videos
256 lines (204 loc) · 6.98 KB
/
convert-videos
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/bin/bash
scriptdir="$(dirname "$0")"
. "$scriptdir/functions"
echo "###########################"
echo " Use bash"
echo "###########################"
function convertfile
{
echo "Output to $2"
if [ "$simulate" != "y" ] ;then
nice -n 19 ffmpeg -v $verbosity -y -i "$1" -vcodec libx264 -crf $qualityfactor -vf "transpose=dir=$rot" -vf $res -acodec $acodec -ab 192k "$2" </dev/null
# nice -n 19 ffmpeg -v $verbosity -y -i "$1" -vcodec libx264 -crf $qualityfactor -vf "transpose=dir=$rot" -vf $res -acodec aac -strict experimental -ab 128k "$2" </dev/null
# nice -n 19 ffmpeg -v 2 -y -i "$1" -vf "transpose=dir=$rot" -vf $res -b "$b"k -acodec aac -strict experimental -ab 128k "$2" </dev/null
else
echo "nice -n 19 ffmpeg -v $verbosity -y -i $1 -vcodec libx264 -crf $qualityfactor -vf transpose=dir=$rot -vf $res -acodec $acodec -strict experimental -ab 128k $2 </dev/null "
fi
}
from=""
ending="mp4"
ending_target="mp4"
count=1000
b=2000
# frames=20
# res=800x450
# -vf "scale=640:-1" preserves the aspect ratio
# res=1280x738
res="scale=-1:-1" # reduce noise with "dctdnoiz=4.5"
rot=-1
qualityfactor=20
verbosity=fatal
post="red"
out=""
acodec=copy
mydate=`date "+%Y-%m-%d-%H-%M-%S"`
while getopts "f:t:e:E:a:q:f:r:p:s:m:v:h" optname
do
case "$optname" in
"f")
echo "Option $optname is $OPTARG"
from=$OPTARG
;;
"t")
echo "Option $optname is $OPTARG"
out=$OPTARG
;;
# "n")
# echo "Option $optname is $OPTARG"
# count=$OPTARG
# ;;
"e")
echo "Option $optname is $OPTARG"
ending=$OPTARG
;;
"E")
echo "Option $optname is $OPTARG"
ending_target=$OPTARG
;;
"q")
echo "Option $optname is $OPTARG"
qualityfactor=$OPTARG
;;
"a")
echo "Option $optname is $OPTARG"
acodec=$OPTARG
;;
# "b")
# echo "Option $optname is $OPTARG"
# b=$OPTARG
# ;;
"m")
echo "Option $optname is $OPTARG"
rot=$OPTARG
;;
"f")
echo "Option $optname is $OPTARG"
frames=$OPTARG
;;
"r")
echo "Option $optname is $OPTARG"
res=$OPTARG
;;
"p")
echo "Option $optname is $OPTARG"
pre=$OPTARG
;;
"s")
echo "Simulate"
simulate="y"
;;
"v")
echo "Option $optname is $OPTARG"
verbosity=$OPTARG
;;
"h")
echo ""
echo " Help will always be given to those who ask for it"
echo ""
echo ""
echo " -f From IS REQUIRED Please with a / at the end"
echo " -t output folder Please with a / at the end (default is the source directory $out)"
# echo " -n Count files (default is $count)"
echo " -e Extension (default is $ending)"
echo " -T Target Extension (default is $ending_target)"
echo " -a Audio codec (default is $acodec)"
echo " -q qualityfactor (default is $qualityfactor, 18...24 lower is higher quality)"
# echo " -b bitrate (default is $b)"
echo " -m Transformation (default is no transformation $rot) Available values are clock, cclock, clock_flip, cclock_flip"
# echo " -f Frames per sencond (default = $frames)"
echo " -r resolution (default is \"$res\")"
echo " -p Post Output Name (default is $post)"
echo " -v Verbosioty (default is $verbosity)"
echo " -s Simulate"
echo " The Input Path just give as the last argument"
echo " Example:"
echo " bash ~/Skripte/convert-videos -r \"$res\" -s -n 1 -e "$ending" -f /media/mount/Filme/"
exit
;;
esac
done
#cd "${!#}"
mkdir -p "$out"
if [ "$from" = "" ] ;then
echo "Cant do anything. Give an -f input folder/file"
exit
fi
bool_screen=false
Ask "Create screenshots comparison of the original and the converted file?" "no" "yes"
if [ "$result" == "yes" ]; then
bool_screen=true
fi
ifile=0
if [ -d "$from" ]; then
cd "$from"
echo "it's a directory "
#count files
countfiles=$(find . -name \*.$ending | wc -l)
echo "Applying on $countfiles files"
# randomly sorts them
# if you don't wnat if randomly just remove |sort -R
find . -name \*.$ending | sort | while read file; do
# find . -name \*.$ending |sort -R |tail -n $count |while read file; do
ifile=$(echo "$ifile +1" | bc -l)
ipercent=$(echo "scale=2; $ifile/$countfiles*100/1" | bc -l)
echo ""
echo ""
echo ""
echo ""
echo "====================================================================================================="
echo "== File $ifile / $countfiles = $ipercent % ======="
#echo "File ""$file"
name=`basename "$file"`
echo "Filename ""$name"
outname="$out$file-$post-$mydate.$ending_target"
# replace the ./ in the outname
outname=${outname/.\//}
# create necessary folders
mkdir -p "$(dirname "${outname}")"
convertfile "$file" "$outname"
sizeorg=$(wc -c <"$file")
sizeout=$(wc -c <"$outname")
comparison=$(echo "scale=2; $sizeout/$sizeorg/1" | bc -l)
if (( $(echo "$comparison >= 1" | bc -l) )); then
echo "#! Attention. Converted video is LARGER by $comparison as the original!"
echo "#! Attention. Converted video is LARGER by $comparison as the original!" >> "$mydate.log"
else
echo "#Reduced size to $comparison of original size"
fi
#echo $bool_screen
if $bool_screen; then
ffmpeg -v $verbosity -ss 00:00:20 -i "$file" -vframes 1 -q:v 2 "$file-screenshot.jpg" </dev/null
ffmpeg -v $verbosity -ss 00:00:20 -i "$outname" -vframes 1 -q:v 2 "$outname-screenshot.jpg" </dev/null
fi
echo "trash \"$file\" ### reduced to $comparison > $outname" >> "$mydate.log"
done
echo "============================================================================================================================="
echo "Converted files: "
echo "============================================================================================================================="
cat "$mydate.log"
Ask "Delete these videos to trash?" "no" "yes"
if [ "$result" == "yes" ]; then
bash "$mydate.log"
fi
else
echo "it's a file"
verbosity=info
file=$from
#name=`basename "$from"`
#DIR=$(dirname "${from}")
outname="$out$from-$post-$mydate.$ending_target"
convertfile "$from" "$outname"
sizeorg=$(wc -c <"$file")
sizeout=$(wc -c <"$outname")
comparison=$(echo "$sizeout/$sizeorg" | bc -l)
if (( $(echo "$comparison >= 1" | bc -l) )); then
echo "! Attention. Converted video is LARGER by $comparison as the original!"
else
echo "Reduced size to $comparison of original size"
fi
#echo $bool_screen
if $bool_screen; then
ffmpeg -v $verbosity -ss 00:00:20 -i "$file" -vframes 1 -q:v 2 "$file-screenshot.jpg" </dev/null
ffmpeg -v $verbosity -ss 00:00:20 -i "$outname" -vframes 1 -q:v 2 "$outname-screenshot.jpg" </dev/null
fi
fi