-
Notifications
You must be signed in to change notification settings - Fork 1
/
ffconcat_filter
executable file
·180 lines (148 loc) · 3.77 KB
/
ffconcat_filter
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
#!/bin/sh -e
# XXX document that tabs, single quotes, and newlines aren't allowed in
# filenames, and 1 video stream and 1 audio stream are assumed
# XXX document transition and atransition
help() {
cat - >&2 <<EOF
ffconcat_filter - convert ffconcat script to shell script
ffconcat_filter [-av] in_script out_script [input_args] [output_args] [filters]
This program converts an ffconcat script (see ffmpeg-formats(1)) into a shell
script invoking ffmpeg using the concat filter. The main reason why you might
want something like this is for splicing multiple files together with precise
cuts.
The -a and -v options disable audio and video respectively.
EOF
}
NL='
'
ffconcat() { :; }
duration() { :; }
file_packet_metadata() { :; }
file_packet_meta() { :; }
option() { :; }
stream() { :; }
exact_stream_id() { :; }
stream_meta() { :; }
stream_codec() { :; }
stream_extradata() { :; }
chapter() { :; }
inpoint() { segment="$segment in=$1"; }
outpoint() { segment="$segment out=$1"; }
transition() { segment="$segment transition=$1"; }
atransition() { segment="$segment atransition=$1"; }
eval_opts() {
for opt in $opts; do
case "$opt" in *=*)
eval "${opt%%=*}"="${opt#*=}" ;;
esac
done
}
offset() {
if [ ! "$1" ]; then
echo offset requires a duration argument >&2
exit 1
fi
duration="$1"
offsets="${offsets:=0}"
offset=0
while IFS=' ' read -r f opts; do
[ "$f" ] || continue
[ "$f $opts" != "$segment" ] || continue
unset -v out in
eval_opts
[ "$out" ] || continue
[ "$in" ] || continue
offset=$(bc -l) <<-EOF
$offset + ($out - $in)
EOF
done <<-EOF
$segments
EOF
offset=$(bc -l) <<-EOF
$offset - $duration - $offsets
EOF
offsets=$(bc -l) <<-EOF
$offsets + $duration
EOF
}
file() {
segments="${segments}${NL}${segment}"
segment="$1"
inputs_count=$((inputs_count+1))
}
VIDEO=1; AUDIO=1;
while getopts av o; do
case "$o" in
a) unset -v AUDIO ;;
v) unset -v VIDEO ;;
h) help; exit 0 ;;
*) help; exit 1 ;;
esac
done
shift $((OPTIND-1))
if [ ! -f "$1" ]; then
echo input file does not exist >&2
exit 1
elif [ ! "$2" ]; then
echo output file not provided >&2
exit 1
elif [ "$1" = "$2" ]; then
echo output is the same as input >&2
exit 1
fi
if [ -f "$2" ]; then
echo overwrite "$2" y/N?
read -r user_input
[ "$user_input" = y ] || [ "$user_input" = Y ]
fi
inputs_count=0
. "$1"
segments="${segments}${NL}${segment}"
# this used to be piped into uniq and then the trim filters would reuse
# the previous input file number if the previous filename was the same
# as the current one, so the generated code could look a little neater,
# but that caused ffmpeg to use a ton of memory and make my system crash
# https://video.stackexchange.com/questions/26132/why-trimming-in-ffmpeg-eats-up-so-much-memory
while IFS=' ' read -r segment opts; do
[ "$segment" ] || continue
eval_opts
inputs="$inputs -ss $in -to $out -i '$segment' \\$NL"
done <<-EOF
$segments
EOF
inputs=${inputs%"$NL"}
# generate trim filters
i=0
out_i=0
not_final=1
while IFS=' ' read -r segment opts; do
[ "$segment" ] || continue
unset -v in out
transition='concat'
atransition='concat=v=0:a=1'
eval_opts
filters="$filters
${VIDEO:+[$i:v]trim=$in:$out,setpts=PTS-STARTPTS,settb=AVTB[v$out_i];}
${AUDIO:+[$i:a]atrim=$in:$out,asetpts=PTS-STARTPTS,asettb=AVTB[a$out_i];}"
if [ "$((i+1))" -eq "$inputs_count" ]; then
unset -v not_final
fi
if [ "$i" -ge 1 ]; then
out_i=$((out_i+1))
filters="$filters
${VIDEO:+[v$((out_i-2))][v$((out_i-1))]$transition${not_final:+[v$out_i]};}
${AUDIO:+[a$((out_i-2))][a$((out_i-1))]$atransition${not_final:+[a$out_i]};}"
fi
[ "$not_final" ] || filters="${filters%;*}"
i=$((i+1))
out_i=$((out_i+1))
done <<-EOF
$segments
EOF
cat > "$2" <<EOF
#!/bin/sh -e
ffmpeg $3 \$1$inputs
-copyts -filter_complex "$filters$5\$3" \\
\$2 $4
EOF
chmod u+x "$2"