Skip to content

Commit 520ebfb

Browse files
committed
Adding output filename option
Signed-off-by: Ivan Boothe <[email protected]>
1 parent a2f2e13 commit 520ebfb

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

videos/trimvid.sh

+21-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
# USAGE
2727
#
28-
# $ ./trimvid.sh <FILE> <START> [END]
28+
# $ ./trimvid.sh <FILE> <START> [END] [OUTPUT-FILE]
2929

3030
# EXAMPLES
3131
#
@@ -36,6 +36,9 @@
3636
# number (in seconds), in which case it acts as a duration, OR a timecode in the
3737
# form of HH:MM:SS, in which case it acts as a stop position.
3838
#
39+
# OUTPUT-FILE is optional. If no value is provided, it creates a video with
40+
# "-trim" appended to the original filename.
41+
#
3942
# Trim video.mp4 beginning at 1 minute, 29 seconds to the end of the video:
4043
# $ ./trimvid.sh video.mp4 00:01:29
4144
#
@@ -44,18 +47,19 @@
4447
# $ ./trimvid.sh video.mp4 00:01:29 90
4548
#
4649
# Trim video.mp4 beginning at 1 minute, 29 seconds and ending at 1 hour, 52
47-
# minutes, 56 seconds:
48-
# $ ./trimvid.sh video.mp4 00:01:29 01:52:56
50+
# minutes, 56 seconds; name the video "final.mp4":
51+
# $ ./trimvid.sh video.mp4 00:01:29 01:52:56 final.mp4
4952

5053
# Revision history:
54+
# 2021-12-30 Adding output filename option (1.3)
5155
# 2021-11-29 Updating license (1.2)
5256
# 2021-10-15 Adding help, dependency checks, and other standardization (1.1)
5357
# 2021-10-11 Initial release (1.0)
5458
# ---------------------------------------------------------------------------
5559

5660
# Standard variables
5761
PROGNAME=${0##*/}
58-
VERSION="1.2"
62+
VERSION="1.3"
5963
red=$(tput setaf 1)
6064
green=$(tput setaf 2)
6165
yellow=$(tput setaf 3)
@@ -95,7 +99,7 @@ signal_exit() {
9599
# Usage: Separate lines for mutually exclusive options.
96100
usage() {
97101
printf "%s\n" \
98-
"${bold}Usage:${reset} ${PROGNAME} <FILE> <START> [END]"
102+
"${bold}Usage:${reset} ${PROGNAME} <FILE> <START> [END] [OUTPUT-FILE]"
99103
printf "%s\n" \
100104
" ${PROGNAME} [-h|--help]"
101105
}
@@ -123,6 +127,9 @@ the end of the video. If a value is provided, it can be either a decimal number
123127
(in seconds), in which case it acts as a duration, OR a timecode in the form of
124128
HH:MM:SS, in which case it acts as a stop position.
125129
130+
OUTPUT-FILE is optional. If no value is provided, it creates a video with
131+
"-trim" appended to the original filename.
132+
126133
Trim video.mp4 beginning at 1 minute, 29 seconds to the end of the video:
127134
128135
${green}$ ${PROGNAME} video.mp4 00:01:29${reset}
@@ -133,9 +140,9 @@ and a half minutes):
133140
${green}$ ${PROGNAME} video.mp4 00:01:29 90${reset}
134141
135142
Trim video.mp4 beginning at 1 minute, 29 seconds and ending at 1 hour, 52
136-
minutes, 56 seconds:
143+
minutes, 56 seconds; name the video "final.mp4":
137144
138-
${green}$ ${PROGNAME} video.mp4 00:01:29 01:52:56${reset}
145+
${green}$ ${PROGNAME} video.mp4 00:01:29 01:52:56 final.mp4${reset}
139146
140147
_EOF_
141148
}
@@ -170,14 +177,18 @@ name="${1%.*}"
170177
ext="${1##*.}"
171178
start=$2
172179
end=$3
180+
output=$4
173181
if [[ ! $file ]]; then
174182
usage >&2
175-
error_exit "Filename must be provided."
183+
error_exit "Input filename must be provided."
176184
fi
177185
if [[ ! $start ]]; then
178186
usage >&2
179187
error_exit "Start timecode must be provided."
180188
fi
189+
if [[ ! $output ]]; then
190+
output="${name}-trim.${ext}"
191+
fi
181192

182193
# Dependencies
183194
ffmpeg=$(command -v ffmpeg)
@@ -205,9 +216,9 @@ else
205216
fi
206217

207218
if [ -f "${file}" ]; then # Make sure video file exists
208-
"$ffmpeg" -v quiet -stats -ss "$start" -i "$file" -t "$end" -c copy -map_metadata -1 -map_chapters -1 "$name"-trim."$ext"
219+
"$ffmpeg" -v quiet -stats -ss "$start" -i "$file" -t "$end" -c copy -map_metadata -1 -map_chapters -1 "$output"
209220

210-
printf "%s\n" "${green}Video trimmed. File: ${reset}${bold}${name}-trim.${ext}${reset}"
221+
printf "%s\n" "${green}Video trimmed. File: ${reset}${bold}${output}"
211222
else
212223
error_exit "Video file '${file}' not found."
213224
fi

0 commit comments

Comments
 (0)