-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathscene-cut
executable file
·117 lines (96 loc) · 3.6 KB
/
scene-cut
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
#!/bin/sh
#===============================================================================
# scene-cut
# ffmpeg scene cut
#===============================================================================
# dependencies:
# ffmpeg
#===============================================================================
# script usage
#===============================================================================
usage () {
# if argument passed to function echo it
[ -z "${1}" ] || echo "! ${1}"
# display help
echo "\
$(basename "$0") -i input -c cutfile
-i input.(mp4|mov|mkv|m4v)
-c cutfile"
exit 2
}
#===============================================================================
# error messages
#===============================================================================
INVALID_OPT_ERR='Invalid option:'
REQ_ARG_ERR='requires an argument'
WRONG_ARGS_ERR='wrong number of arguments passed to script'
#===============================================================================
# check the number of arguments passed to the script
#===============================================================================
[ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}"
#===============================================================================
# getopts check the options passed to the script
#===============================================================================
while getopts ':i:c:h' opt
do
case ${opt} in
i) input="${OPTARG}";;
c) cutfile="${OPTARG}";;
h) usage;;
\?) usage "${INVALID_OPT_ERR} ${OPTARG}" 1>&2;;
:) usage "${INVALID_OPT_ERR} ${OPTARG} ${REQ_ARG_ERR}" 1>&2;;
esac
done
shift $((OPTIND-1))
#===============================================================================
# variables
#===============================================================================
# get the input file name
input_nopath="${input##*/}"
input_name="${input_nopath%.*}"
#===============================================================================
# ffmpeg create clips - nostdin needed to avoid clash with read command
#===============================================================================
trim_video () {
# awk convert start and end time from sexagesimal time to seconds
# add the start and end time to get the endpoint and convert from seconds back to sexagesimal time
endtime=$(printf "%s %s\n" "${start}" "${duration}" \
| awk '
{
start = $1
end = $2
if (start ~ /:/) {
split(start, t, ":")
sseconds = (t[1] * 3600) + (t[2] * 60) + t[3]
}
if (end ~ /:/) {
split(end, t, ":")
eseconds = (t[1] * 3600) + (t[2] * 60) + t[3]
}
duration = eseconds + sseconds
printf("%s\n"), duration
}' \
| awk -F. 'NF==1 { printf("%02d:%02d:%02d\n"), ($1 / 3600), ($1 % 3600 / 60), ($1 % 60) }\
NF==2 { printf("%02d:%02d:%02d.%s\n"), ($1 / 3600), ($1 % 3600 / 60), ($1 % 60), ($2) }'
)
output="${input_name}-[${start}-${endtime}].mp4"
ffmpeg \
-nostdin \
-hide_banner \
-stats -v panic \
-ss "${start}" \
-i "${input}" \
-t "${duration}" \
-c:a aac \
-c:v libx264 -profile:v high \
-pix_fmt yuv420p -movflags +faststart \
-f mp4 \
"${output}"
}
#===============================================================================
# read file and set IFS=, read = input before , duration = input after ,
#===============================================================================
count=1
while IFS=, read -r start duration; do
trim_video
done < "${cutfile}"