-
Notifications
You must be signed in to change notification settings - Fork 1
/
videoaip
executable file
·269 lines (242 loc) · 10.8 KB
/
videoaip
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
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
config_file="$HOME/.$(basename "${0}").conf"
touch "${config_file}"
source "${config_file}"
usage(){
echo "Usage: videoaip [options] [inputfile1] [inputfile2] ... , help mode: videoaip -h, crop mode: videoaip -c, configuration: videoaip -e, add logs with -l file1.log -l file2.log etc OR -l auto to add to AIP all .log and .framemd5 files in same directory as input[s]." && exit
}
_cancel_option(){
clear && echo "Goodbye" && exit 0
}
_set_up_trim(){
if [ -n "${EndTrimLength}" ] ; then
FILELENGTH=$(ffprobe "${1}" 2>&1 | grep Duration | tr -d ' ' | cut -d',' -f1 | cut -d':' -f2-)
FILELENGTH_NORMALIZED=$(echo "${FILELENGTH}" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
ENDTRIM=$(echo "${FILELENGTH_NORMALIZED}" - "${EndTrimLength}" | bc)
if [ -n "${StartTrimLength}" ] && [ -n "${EndTrimLength}" ] ; then
ENDTRIM=$(echo "${FILELENGTH_NORMALIZED}" - "${EndTrimLength}" - "${StartTrimLength}" | bc)
fi
ENDTRIMOPT="-t ${ENDTRIM}"
fi
if [ -n "${StartTrimLength}" ] ; then
STARTTRIMOPT="-ss ${StartTrimLength}"
fi
}
_preview_trim(){
mpv --no-terminal --keep-open=yes --title="Preview for Trimming" "${file_input}" -lavfi-complex [vid1]crop="${cropdetect}"[vo]
}
_get_crop(){
if [ -z "${crop_message}" ] ; then
crop_message="Input number of lines to crop from bottom. Enter Y to view file again. Leave blank to not crop bottom. Press Cancel to exit script."
fi
crop_choice=$(dialog --title "Crop Bottom of Video" --inputbox "${crop_message}" 8 50 3>&1 1>&2 2>&3 3>&-) || _cancel_option
if [ -n "${crop_choice}" ] && ! [[ "${crop_choice}" =~ ^[0-9]+$ ]] && ! [[ "${crop_choice}" = [yY] ]] ; then
crop_message="Please use integers only."
_get_crop
fi
}
_get_start_trim(){
if [ -z "${start_trim_message}" ] ; then
start_trim_message="Enter number of seconds to trim from start of file. Enter Y to view file again. Leave blank to not trim. Press Cancel to exit script."
fi
StartTrimLength=$(dialog --title "Start Trim Length" --inputbox "${start_trim_message}" 8 50 3>&1 1>&2 2>&3 3>&-) || _cancel_option
if [ -n "${StartTrimLength}" ] && ! [[ "${StartTrimLength}" =~ ^[0-9]+$ ]] && ! [[ "${StartTrimLength}" = [yY] ]] ; then
start_trim_message="Please use integers only."
_get_start_trim
elif [[ "${StartTrimLength}" = [yY] ]] ; then
_preview_trim
_get_start_trim
fi
}
_get_end_trim(){
if [ -z "${end_trim_message}" ] ; then
end_trim_message="Enter number of seconds to trim from end of file. Enter Y to view file again. Leave blank to not trim. Press Cancel to exit script."
fi
EndTrimLength=$(dialog --title "End Trim Length" --inputbox "${end_trim_message}" 8 50 3>&1 1>&2 2>&3 3>&-) || _cancel_option
if [ -n "${EndTrimLength}" ] && ! [[ "${EndTrimLength}" =~ ^[0-9]+$ ]] && ! [[ "${EndTrimLength}" = [yY] ]] ; then
end_trim_message="Please use integers only."
_get_end_trim
elif [[ "${EndTrimLength}" = [yY] ]] ; then
_preview_trim
_get_end_trim
fi
}
_set_up_crop(){
mpv --no-terminal --keep-open=yes --title="Preview for Cropping" "${file_input}" -lavfi-complex [vid1]split=2[aa][b],[aa]crop=iw:20:0:ih-20[aa1],[aa1]scale=iw:ih*20:flags=neighbor,setdar=4/3[aa2],[aa2]drawgrid=width=iw:height=ih/20:t=1:c=lightgreen[bb2],[bb2]scale=300:-1[aa3],[aa3]drawtext="/Library/Fonts/Andale\ Mono.ttf: \
text="20": fontcolor=lightgreen: fontsize=12: x=10: y=0"[t],[t]drawtext="/Library/Fonts/Andale\ Mono.ttf: \
text="15": fontcolor=lightgreen: fontsize=12: x=10: y=(h-text_h)*.26"[t2],[t2]drawtext="/Library/Fonts/Andale\ Mono.ttf: \
text="10": fontcolor=lightgreen: fontsize=12: x=10: y=(h-text_h)*.52"[t3],[t3]drawtext="/Library/Fonts/Andale\ Mono.ttf: \
text="5": fontcolor=lightgreen: fontsize=12: x=10: y=(h-text_h)*.79"[t4],[b]crop="${cropdetect}"[b1],[b1][t4]overlay=10:10[vo]
_get_crop
if [[ "${crop_choice}" = [yY] ]] ; then
_set_up_crop
fi
}
_error_exit(){
echo -e "\033[1;32m"$1"\033[0m" 1>&2
}
OPTIND=1
while getopts "hecxl:" opt ; do
case "${opt}" in
h) usage ;;
e) runtype="edit";;
c) runtype="crop";;
x) hiddenFiles="preserve";;
l) logfiles+=("${OPTARG}");;
*)
esac
done
if [ "${*}" = "" ] ; then
usage
fi
shift $((OPTIND -1))
if [[ "${runtype}" = "edit" ]] ; then
{
echo "#INSERT YOUR VALUES BETWEEN THE QUOTES AND SAVE FILE"
echo "#To control file sync change 'sync_choice' to either Y or N"
echo "#Then change destination to the file path of the desired destination"
echo "#Enter destinations (with no spaces in names) between the parentheses and separated them with spaces"
echo "sync_choice=\"${sync_choice}\""
echo "destination=("${destination[@]}")"
echo ""
echo "#To remove original AIP after syncing change to Y"
echo "remove_aip=\"${remove_aip}\""
echo ""
echo "#If you want to sync an additional access file to an extra location choose Y or N and enter file path of destination below"
echo "derivative_choice=\"${derivative_choice}\""
echo "derivative_destination=\"${derivative_destination}\""
} > "${config_file}"
mate "$config_file" || nano "$config_file"
exit
fi
#Check if input exists
while [ "${*}" != "" ] ; do
file_input="${1}"
shift
if [ ! -f "${file_input}" ]; then
_error_exit "Input: ${file_input} could not be found. Please enter valid input."
continue
fi
#set variables
orig_base=$(basename "$file_input")
mp4_name="${file_input%.*}.mp4"
mediaid="${orig_base%.*}"
targetdirectory=$(dirname "${file_input}")
if [ -d "${targetdirectory}/${mediaid}" ] ; then
_error_exit "Directory already exists. Exiting..."
continue
fi
#crop mode
if [[ "${runtype}" = "crop" ]] ; then
echo -e "\033[1;32mPlease wait. Gathering crop information.\033[0m"
cropdetect=$(ffmpeg -ss 15 -i "${file_input}" -vf cropdetect -hide_banner -an -t 10 -f null - 2>&1 | grep crop= | cut -d' ' -f 14- | sort | uniq -c | sort | tail -n1 | cut -d'=' -f2-)
_set_up_crop
crop_w=$(echo "${cropdetect}" | cut -d':' -f1)
crop_h=$(echo "${cropdetect}" | cut -d':' -f2)
crop_frame=$(echo "${cropdetect}" | cut -d':' -f3-)
if [ -n "${crop_choice}" ] ; then
crop_h_modified=$((crop_h-crop_choice))
if ! [ $(($crop_h_modified % 2)) -eq 0 ] ; then
crop_h_modified=$((crop_h_modified-1))
fi
cropdetect="${crop_w}:${crop_h_modified}:${crop_frame}"
fi
cropoptions=",crop=${cropdetect}"
_preview_trim
_get_start_trim
_get_end_trim
_set_up_trim "${file_input}"
fi
#create directory structure
mkdir "${targetdirectory}/${mediaid}" || ( _error_exit "Directory already exists. Exiting..." && continue )
mkdir "${targetdirectory}/${mediaid}/metadata"
mkdir "${targetdirectory}/${mediaid}/logs"
mkdir "${targetdirectory}/${mediaid}/logs/fileMeta"
mkdir "${targetdirectory}/${mediaid}/objects"
mkdir "${targetdirectory}/${mediaid}/objects/access"
#create metadata and derivative files
mediainfo --output=XML "$file_input" > "${targetdirectory}/${mediaid}/logs/fileMeta/${mediaid}.xml"
mediainfo -f "$file_input" > "${targetdirectory}/${mediaid}/logs/fileMeta/${mediaid}.txt"
if ! [ -e "$mp4_name" ] ; then
ffmpeg ${STARTTRIMOPT} -i "${file_input}" -c:v libx264 -pix_fmt yuv420p -movflags +faststart -preset fast -crf 18 -c:a aac -ar 48k -b:a 128k -vf yadif=deint=1"${cropoptions}" ${ENDTRIMOPT} "$mp4_name" 2>&1 | tee "${targetdirectory}/${mediaid}/logs/${mediaid}_mp4.log"
if [ "${?}" -ne 0 ] ; then
echo -e "\033[1;31mError in Conversion! Cleaning up and Exiting.\033[0m"
rm "$mp4_name"
rm -r "${targetdirectory}/${mediaid}"
exit 1
fi
fi
#sync access mp4 if selected
if [[ "${derivative_choice}" = [yY] ]] ; then
echo -e "\033[1;32mSyncing access copy to "${derivative_destination}" \033[0m"
if [ -d "${derivative_destination}" ] ; then
rsync -Pa "$mp4_name" "${derivative_destination}"
else
echo -e "\033[1;31mError: Derivative Sync to "${derivative_destination}" Failed\033[0m"
fi
fi
#move files
echo -e "\033[1;32mMoving Files into package\033[0m"
rsync -av "$file_input" "${targetdirectory}/${mediaid}/objects"
rsync -av --remove-source-files "$mp4_name" "${targetdirectory}/${mediaid}/objects/access"
# either find all log files in same directory as input (auto) or add specified logfiles
if [ "${logfiles[0]}" = 'auto' ] ; then
unset logfiles
logfiles+=($(find "${targetdirectory}" -maxdepth 1 -iname "*.log"))
frame_md5=($(find "${targetdirectory}" -maxdepth 1 -iname "*.framemd5"))
for i in "${logfiles[@]}" ; do
rsync -av "${i}" "${targetdirectory}/${mediaid}/logs/"
done
for i in "${frame_md5[@]}" ; do
rsync -av "${i}" "${targetdirectory}/${mediaid}/metadata/"
done
unset logfiles
unset framemd5
logfiles+=('auto')
else
for i in "${logfiles[@]}" ; do
if [ ${i##*.} == 'framemd5' ] ; then
rsync -av "${i}" "${targetdirectory}/${mediaid}/metadata/"
else
rsync -av "${i}" "${targetdirectory}/${mediaid}/logs/"
fi
done
fi
package="${targetdirectory}/${mediaid}"
#create checksum
echo -e "\033[1;32mGenerating Checksums\033[0m"
md5deep -bcr "$package" > "$package/metadata/${mediaid}.md5"
#Run Bagit
echo -e "\033[1;32mCreating Bag\033[0m"
if [ "${hiddenFiles}" = 'preserve' ] ; then
bagit.py --md5 "$package"
else
find "$package" -type f -iname ".*" -exec rm -f {} \;
bagit.py --md5 "$package"
fi
#sync AIP to destination
if [[ "${sync_choice}" = [yY] ]] ; then
for i in "${destination[@]}" ; do
if [ -d "${i}" ] ; then
echo -e "\033[1;32mSyncing Files to "${i}" \033[0m"
if [ "${hiddenFiles}" = 'preserve' ] ; then
rsync -Pa "$package" "${i}"
else
rsync -Pa --exclude ".*" "$package" "${i}"
fi
if ! [ "${?}" = "0" ] ; then
SYNC_PROBLEM="1"
fi
else
echo -e "\033[1;31mError: Sync to ${i} Failed\033[0m"
SYNC_PROBLEM="1"
fi
done
if [[ "${remove_aip}" = [yY] ]] && [ -z "${SYNC_PROBLEM}" ]; then
echo "Removing source AIP"
rm -r "${package}"
else
echo "Problem Detected with sync - leaving original AIP in place"
fi
fi
done