-
Notifications
You must be signed in to change notification settings - Fork 0
/
trim.sh
executable file
·202 lines (176 loc) · 5.56 KB
/
trim.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
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
#!/usr/bin/env bash
# shellcheck disable=SC2154
#
# ARG_OPTIONAL_SINGLE([output-directory],[o],[Specifys the output directory],[.])
# ARG_OPTIONAL_SINGLE([output-filename],[f],[Specifys a filename],[.])
# ARG_OPTIONAL_SINGLE([start],[s],[specify the start],[00:00:00])
# ARG_OPTIONAL_SINGLE([crf],[],[crf to use for encoding],[18])
# ARG_POSITIONAL_SINGLE([input],[path to input video])
# ARG_POSITIONAL_SINGLE([end],[target end timestemp of the video])
# ARG_HELP([ffmpeg wrapper to trim a video to a desired timestemp with recompressing])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}
begins_with_short_option()
{
local first_option all_short_options='ofsh'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# THE DEFAULTS INITIALIZATION - POSITIONALS
_positionals=()
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_output_directory="."
_arg_output_filename="."
_arg_start="00:00:00"
_arg_crf="18"
print_help()
{
printf '%s\n' "ffmpeg wrapper to trim a video to a desired timestemp with recompressing"
printf 'Usage: %s [-o|--output-directory <arg>] [-f|--output-filename <arg>] [-s|--start <arg>] [--crf <arg>] [-h|--help] <input> <end>\n' "$0"
printf '\t%s\n' "<input>: path to input video"
printf '\t%s\n' "<end>: target end timestemp of the video"
printf '\t%s\n' "-o, --output-directory: Specifys the output directory (default: '.')"
printf '\t%s\n' "-f, --output-filename: Specifys a filename (default: '.')"
printf '\t%s\n' "-s, --start: specify the start (default: '00:00:00')"
printf '\t%s\n' "--crf: crf to use for encoding (default: '18')"
printf '\t%s\n' "-h, --help: Prints help"
}
parse_commandline()
{
_positionals_count=0
while test $# -gt 0
do
_key="$1"
case "$_key" in
-o|--output-directory)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_output_directory="$2"
shift
;;
--output-directory=*)
_arg_output_directory="${_key##--output-directory=}"
;;
-o*)
_arg_output_directory="${_key##-o}"
;;
-f|--output-filename)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_output_filename="$2"
shift
;;
--output-filename=*)
_arg_output_filename="${_key##--output-filename=}"
;;
-f*)
_arg_output_filename="${_key##-f}"
;;
-s|--start)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_start="$2"
shift
;;
--start=*)
_arg_start="${_key##--start=}"
;;
-s*)
_arg_start="${_key##-s}"
;;
--crf)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_crf="$2"
shift
;;
--crf=*)
_arg_crf="${_key##--crf=}"
;;
-h|--help)
print_help
exit 0
;;
-h*)
print_help
exit 0
;;
*)
_last_positional="$1"
_positionals+=("$_last_positional")
_positionals_count=$((_positionals_count + 1))
;;
esac
shift
done
}
handle_passed_args_count()
{
local _required_args_string="'input' and 'end'"
test "${_positionals_count}" -ge 2 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 2 (namely: $_required_args_string), but got only ${_positionals_count}." 1
test "${_positionals_count}" -le 2 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 2 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1
}
assign_positional_args()
{
local _positional_name _shift_for=$1
_positional_names="_arg_input _arg_end "
shift "$_shift_for"
for _positional_name in ${_positional_names}
do
test $# -gt 0 || break
eval "$_positional_name=\${1}" || die "Error during argument parsing, possibly an Argbash bug." 1
shift
done
}
parse_commandline "$@"
handle_passed_args_count
assign_positional_args 1 "${_positionals[@]}"
# OTHER STUFF GENERATED BY Argbash
### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash
#Check if input directory exits
if [ ! -d "$_arg_output_directory" ]
then
echo "output directory must exist"
exit 1
fi
if [[ -z "$_arg_output_filename" ]]
then
#Get filename withou extension from input filename
filename="$(basename $_arg_input | sed 's/\(.*\)\..*/\1/')_trim"
else
#Use specified output filename
filename="$_arg_output_filename"
fi
#Get filename extension
extension=".${_arg_input##*.}"
#Build file output path
output="$_arg_output_directory/$filename$extension"
if [ "$_arg_start" = 00:00:00 ]
then
#No start trim was specified -> trim end
ffmpeg -i $_arg_input -to $_arg_end \
-c:v libx264 -crf:v $_arg_crf -preset:v slow -pix_fmt yuv420p \
-c:a aac -b:a 192k \
-movflags +faststart -r 25 \
-metadata author="Familie Ammann" \
-metadata title="$title" $output
else
#Start trim was specified -> trim start and end
ffmpeg -i $_arg_input -ss $_arg_start -to $_arg_end \
-c:v libx264 -crf:v $_arg_crf -preset:v slow -pix_fmt yuv420p \
-c:a aac -b:a 192k \
-movflags +faststart -r 25 \
-metadata author="Familie Ammann" \
-metadata title="$title" \
$output
fi
echo
echo "$filename written to $output"
# ] <-- needed because of Argbash