-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclips-uberblenden
executable file
·152 lines (127 loc) · 5.96 KB
/
clips-uberblenden
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
#!/bin/sh
#===============================================================================
# crossfade-clips
# clips-überblenden
# Videoclips überblenden
#===============================================================================
# Abhängigkeiten:
# ffmpeg file grep
#===============================================================================
# Verwendung von Skripten
#===============================================================================
usage()
{
# Wenn das Argument an die Funktion übergeben wird, echo es
[ -z "${1}" ] || echo "! ${1}"
# Hilfe anzeigen
echo "\
# Clips überblenden
$(basename "$0") -a clip1.(mp4|mkv|mov|m4v) -b clip2.(mp4|mkv|mov|m4v) -d (1|2) -o ausgang.mp4
-a clip1.(mp4|mkv|mov|m4v) : Erster clip
-b clip2.(mp4|mkv|mov|m4v) : Zweiter clip
-d (1|2) : Dauer der Überblendung: optionales Argument
# Wenn die Option nicht angegeben wird, ist die Voreinstellung 1 Sekunde
-o ausgang.mp4 : Dauer der Überblendung: optionales Argument
# Wenn die Option nicht angegeben wird, ist die Voreinstellung 1 Sekunde"
exit 2
}
#===============================================================================
# Fehlermeldungen
#===============================================================================
INVALID_OPT_ERR='Ungültige Option:'
REQ_ARG_ERR='erfordert eine Argumentation'
WRONG_ARGS_ERR='falsche Anzahl von Argumenten an Skript übergeben'
#===============================================================================
# Überprüfen Sie die Anzahl der an das Skript übergebenen Argumente
#===============================================================================
[ $# -gt 0 ] || usage "${WRONG_ARGS_ERR}"
#===============================================================================
# getopts überprüft die an das Skript übergebenen Optionen
#===============================================================================
while getopts ':a:b:d:o:h' opt
do
case ${opt} in
a) clip1="${OPTARG}";;
b) clip2="${OPTARG}";;
d) dur="${OPTARG}";;
o) outfile="${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))
#===============================================================================
# Variablen
#===============================================================================
# Eingabe, Eingabename und Erweiterung
clip1_nopath="${clip1##*/}"
clip1_name="${clip1_nopath%.*}"
# Clip-Dauern für Fades
clip1_dur=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$clip1" | cut -d\. -f1)
clip2_dur=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$clip2" | cut -d\. -f1)
# clip1 Verwenden Sie den bc-Befehl, um 1 Sekunde von der Länge des Clips für die Überblendung zu entfernen
clip1_offset=$(echo "${clip1_dur}-1" | bc -l)
clip2_offset=$(echo "${clip2_dur}-1" | bc -l)
# Variablen
outfile_default="${clip1_name}-xfade-$(date +"%Y-%m-%d-%H-%M-%S").mp4"
duration_default='1'
#===============================================================================
# Funktionen
#===============================================================================
# Video und Audio überblenden
xfade_va () {
ffmpeg \
-hide_banner \
-stats -v panic \
-i "${clip1}" -i "${clip2}" \
-an -filter_complex \
" [0:v]trim=start=0:end='${clip1_offset}',setpts=PTS-STARTPTS[firstclip];
[1:v]trim=start=${dur:=${duration_default}},setpts=PTS-STARTPTS[secondclip];
[0:v]trim=start='${clip1_offset}':end='${clip1_dur}',setpts=PTS-STARTPTS[fadeoutsrc];
[1:v]trim=start=0:end=${dur:=${duration_default}},setpts=PTS-STARTPTS[fadeinsrc];
[fadeinsrc]format=pix_fmts=yuva420p,
fade=t=in:st=0:d=${dur:=${duration_default}}:alpha=1[fadein];
[fadeoutsrc]format=pix_fmts=yuva420p,
fade=t=out:st=0:d=${dur:=${duration_default}}:alpha=1[fadeout];
[fadein]fifo[fadeinfifo];
[fadeout]fifo[fadeoutfifo];
[fadeoutfifo][fadeinfifo]overlay[crossfade];
[firstclip][crossfade][secondclip]concat=n=3[output];
[0:a] afade=t=in:st=0:d=${dur:=${duration_default}} [audiofadein];
[1:a] afade=t=out:st='${clip2_offset}':d=${dur:=${duration_default}} [audiofadeout];
[audiofadein][audiofadeout] acrossfade=d=${dur:=${duration_default}} [audio]
" \
-map "[output]" -map "[audio]" "${outfile:=${outfile_default}}"
}
# Video überblenden
xfade_v () {
ffmpeg \
-hide_banner \
-stats -v panic \
-i "${clip1}" -i "${clip2}" \
-an -filter_complex \
" [0:v]trim=start=0:end='${clip1_offset}',setpts=PTS-STARTPTS[firstclip];
[1:v]trim=start=${dur:=${duration_default}},setpts=PTS-STARTPTS[secondclip];
[0:v]trim=start='${clip1_offset}':end='${clip1_dur}',setpts=PTS-STARTPTS[fadeoutsrc];
[1:v]trim=start=0:end=${dur:=${duration_default}},setpts=PTS-STARTPTS[fadeinsrc];
[fadeinsrc]format=pix_fmts=yuva420p,
fade=t=in:st=0:d=${dur:=${duration_default}}:alpha=1[fadein];
[fadeoutsrc]format=pix_fmts=yuva420p,
fade=t=out:st=0:d=${dur:=${duration_default}}:alpha=1[fadeout];
[fadein]fifo[fadeinfifo];
[fadeout]fifo[fadeoutfifo];
[fadeoutfifo][fadeinfifo]overlay[crossfade];
[firstclip][crossfade][secondclip]concat=n=3[output]
" \
-map "[output]" "${outfile:=${outfile_default}}"
}
# Überprüfen Sie, ob das Video eine Audiospur hat
clip1_check="$(ffprobe -i "${clip1}" -show_streams -select_streams a -loglevel error)"
clip2_check="$(ffprobe -i "${clip2}" -show_streams -select_streams a -loglevel error)"
# Überprüfen Sie, ob audio_check null ist, was bedeutet, dass das Video keine Audiospur hat
if [ -z "${clip1_check}" ] || [ -z "${clip2_check}" ]; then
xfade_v "${clip1}" "${clip2}" # Videospur ausblenden
else
xfade_va "${clip1}" "${clip2}" # Video- und Audiospur ausblenden
fi