-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclipify
executable file
·143 lines (127 loc) · 3.71 KB
/
clipify
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
#!/bin/bash
# SPDX-FileCopyrightText: 2018 - 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -Eeuo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command bc ffmpeg ffprobe sed && exit 3
# Global functions.
function print_usage() {
show_header "Usage:"
cat << EOF
-i --input input video (e.g. input.mp4)"
-o --output output video (e.g. output.mp4)"
-s --start starting timestamp (e.g. 01:20.5)"
-l --length length in seconds (e.g. 3.5)"
-e --end ending timestamp (e.g. 01:20.5)"
EOF
}
function scan_input {
local vstring
local sstring
fstring="$(ffprobe -probesize 1G -analyzeduration 1G -v error -show_format file:"${IN}")"
vstring="$(ffprobe -probesize 1G -analyzeduration 1G -v error \
-select_streams v:0 -show_streams file:"${IN}")"
sstring="$(ffprobe -probesize 1G -analyzeduration 1G -v error \
-select_streams s:0 -show_streams file:"${IN}")"
if ! [[ -v VCODEC ]]; then
VCODEC="$(echo "${vstring}" | sed -n "s/^codec_name=\(.*\)$/\1/p")"
fi
if ! [[ -v WIDTH ]]; then
WIDTH="$(echo "${vstring}" | sed -n "s/^width=\(.*\)$/\1/p")"
fi
if ! [[ -v HEIGHT ]]; then
HEIGHT="$(echo "${vstring}" | sed -n "s/^height=\(.*\)$/\1/p")"
fi
if ! [[ -v RESOLUTIONS ]]; then
RESOLUTIONS=("native")
fi
if ! [[ -v DURATION ]]; then
DURATION="$(echo "${fstring}" | sed -n "s/^duration=\(.*\)$/\1/p")"
fi
if ! [[ -v DIMENSIONS ]]; then
DIMENSIONS=("${WIDTH}x${HEIGHT}")
fi
if ! [[ -v ASPECTRATIO ]]; then
ASPECTRATIO="$(echo "${vstring}" | sed -n "s/^display_aspect_ratio=\(.*\)$/\1/p")"
fi
if ! [[ -v FPS ]]; then
FPS="$(echo "${vstring}" | sed -n "s/^r_frame_rate=\(.*\)$/\1/p" | bc -l)"
fi
if ! [[ -v SCODEC ]]; then
SCODEC="$(echo "${sstring}" | sed -n "s/^codec_name=\(.*\)$/\1/p")"
fi
}
function make_clip {
cmd="ffmpeg -probesize 1G -analyzeduration 1G -v info -i file:${IN@Q} -ss ${START}"
if [[ -v LENGTH ]]; then
cmd="${cmd} -t ${LENGTH}"
elif [[ -v END ]]; then
cmd="${cmd} -to ${END}"
fi
cmd="${cmd} -c copy -y file:${OUT@Q}"
eval "${cmd}"
}
START=0.0
OPTIONS=i:o:s:e:l:t:
LONGOPTIONS=input:,output:,start:,end:,length:,subtitle:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-i | --input)
IN="${2}"
scan_input
shift 2
;;
-o | --output)
OUT="${2}"
shift 2
;;
-s | --start)
START="${2}"
shift 2
;;
-e | --end)
END="${2}"
shift 2
;;
-l | --length)
LENGTH="${2}"
shift 2
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid flag ${1}."
print_usage
exit 3
;;
esac
done
# Check if variables are set before running ffmpeg.
if ! [[ -v IN ]] || ! [[ -v OUT ]]; then
show_error "ERROR: missing input/output files."
print_usage
exit 3
fi
if ! [[ -v LENGTH ]] && ! [[ -v END ]]; then
LENGTH="$(echo "${DURATION} - ${START}" | bc -l)"
fi
make_clip