-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_video.sh
177 lines (133 loc) · 5.15 KB
/
make_video.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
#!/bin/bash
#
# This script is licensed under NOLICENSE please don't take the piss
#
# project github is at https://github.com/d834256/ZIGGAZIGAH
#
# if you set this you'll have subtitles on your final video
SUBTITLES="1"
# tolerance, ok, this is going to take some explaining.
#
# by default you end up with a clip that is exactly the length of one
# subtitle entry. The thing is these clips ary in length depending on
# the pace of the video content. Dramas may be drawn out with slow
# dialogue, action based stuff will be quicker. So if you're not careful
# you're going to have a collection of quickly flashing videos and you'll
# be on the floor having some kind of seizure. So to make this a bit easier
# on the eyes you can add a tolerance which basically adds as many extra
# subtitle entries as you like to make the clips longer.
#
TOLERANCE="0"
#
# change this if you want your db to live somewhere else
#
database="./worddb.sqlite"
# we increment this in a pattern
counter="0"
# pull in our shared functions
. $(dirname "$0")/functions.sh
filename_prefix=$(date +%Y%m%d%H%M%S)
if [ ! "${1}" ]; then
usage_warning="1"
fi
if [ "${1}" != "random" ] && [ "${1}" != "keywords" ]; then
usage_warning="1"
fi
if ([ "${1}" == "random" ] || [ "${1}" == "keywords" ]) && [ ! "${2}" ]; then
usage_warning="1"
fi
if [ "${1}" == "random" ]; then
mode="random"
sublines="${2}"
fi
if [ "${1}" == "keywords" ]; then
mode="keywords"
words="${2}"
fi
if [ "${usage_warning}" == "1" ]; then
scriptname=$(basename -- "${0}")
echo ""
echo "Usage : ${scriptname} keywords [comma separated keywords]"
echo ""
echo "Usage : ${scriptname} random [number of subtitle lines]"
echo ""
exit 1
fi
if [ ! -d "./tmp" ]; then
echo "Creating ./tmp"
mkdir tmp
fi
# make a note of our start time
startdate=$(date +%s)
if [ "${mode}" == "random" ]; then
echo "Mode: ${mode} Using ${sublines} lines of random subtitles."
sql="SELECT id from words order by RANDOM() LIMIT ${sublines};"
results=$(sqlite3 ${database} "${sql}")
echo ${result}
for result in ${results}; do
id="${result}"
padded=$(printf "%05d\n" ${counter})
extract_clip "${filename_prefix}_${padded}" "${id}" "${SUBTITLES}" "${TOLERANCE}"
echo "file '${filename_prefix}_${padded}.mp4'" >> ./tmp/${filename_prefix}.txt
counter=$((counter+1))
done
echo "Combining all the clips to ./tmp/${filename_prefix}_final.mp4"
ffmpeg -loglevel 0 -f concat -safe 0 -i ./tmp/${filename_prefix}.txt -codec copy ./tmp/${filename_prefix}_final.mp4
testdur=$(ffprobe -v error -show_format -show_streams -i "./tmp/${filename_prefix}_final.mp4"|grep duration=|grep -v "N/A"|uniq|awk -F= '{print $2}')
testdur=$(echo ${testdur}|awk -F. '{print $1}')
#echo "Output video file is ${testdur} seconds long."
fi
if [ "${mode}" == "keywords" ]; then
echo "Mode: ${mode} Using keywords: ${words}"
for word in ${words}; do
result=$(search_for_word "${word}")
if [[ "${result}" = *[[:space:]]* ]]; then
resultarr=( $result )
resultcount=${#resultarr[@]}
random=$(( ( RANDOM % ${resultcount} ) ))
id=${resultarr[${random}]}
padded=$(printf "%05d\n" ${counter})
extract_clip "${filename_prefix}_${padded}" "${id}" "${SUBTITLES}" "${TOLERANCE}"
echo "file '${filename_prefix}_${padded}.mp4'" >> ./tmp/${filename_prefix}.txt
counter=$((counter+1))
elif [ "${result}" != "0" ] || [ "${result}" != "" ]; then
id=${result}
padded=$(printf "%05d\n" ${counter})
extract_clip "${filename_prefix}_${padded}" "${id}" "${SUBTITLES}" "${TOLERANCE}"
echo "file '${filename_prefix}_${padded}.mp4'" >> ./tmp/${filename_prefix}.txt
counter=$((counter+1))
else
echo "WARNING: word ${word} wasn't found in the database."
fi
done
echo "Combining all the clips to ./tmp/${filename_prefix}_final.mp4"
ffmpeg -loglevel 0 -f concat -safe 0 -i ./tmp/${filename_prefix}.txt -codec copy ./tmp/${filename_prefix}_final.mp4
errorcode="${?}"
if [ "${errorcode}" == "1" ]; then
echo ""
echo "ERROR: ffmpeg combined failed."
echo ""
echo "ffmpeg command line was :"
echo ""
echo "ffmpeg -loglevel 0 -f concat -safe 0 -i ./tmp/${filename_prefix}.txt -codec copy ./tmp/${filename_prefix}_final.mp4"
echo ""
exit 1
fi
testdur=$(ffprobe -v error -show_format -show_streams -i "./tmp/${filename_prefix}_final.mp4"|grep duration=|grep -v "N/A"|uniq|awk -F= '{print $2}')
testdur=$(echo ${testdur}|awk -F. '{print $1}')
#echo "Output video file is ${testdur} seconds long."
fi
echo "Output video file is ${testdur} seconds long."
# make a note of our end time
enddate=$(date +%s)
#
# https://stackoverflow.com/questions/42149301/how-to-translate-seconds-to-minutes-seconds-in-bash
#
total_time=$((enddate-startdate))
minutes=$((total_time / 60))
seconds=$((total_time % 60))
echo "Script took $minutes minutes and $seconds seconds to complete."
#
# TODO -
#
# something to prevent duplicate clips