-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathffmpeg-concat
More file actions
executable file
·97 lines (82 loc) · 1.78 KB
/
ffmpeg-concat
File metadata and controls
executable file
·97 lines (82 loc) · 1.78 KB
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
#!/usr/bin/env bash
NO_FORMAT="\033[0m"
F_BOLD="\033[1m"
F_DIM="\033[2m"
C_GREEN="\033[38;5;2m"
C_GRAY="\033[38;5;8m"
C_RED="\033[38;5;9m"
if [ -n "$DEBUG" ]; then
set -e
fi
set -euo pipefail
function log() {
printf "${F_DIM}[%s]${NO_FORMAT} $1${NO_FORMAT}\n" "$(date +'%Y-%m-%d %H:%M:%S.%3N')" "${@:2}"
}
function info() {
log "${C_GREEN}INFO${NO_FORMAT} $*"
}
function error() {
log "${C_RED}ERROR${NO_FORMAT} $*"
}
function usage() {
echo -e "$(
cat <<EOF
Concaternate multiple video files into a new file.
Usage:
${C_GREEN}$(basename "$0")${NO_FORMAT} ${F_DIM}${C_GRAY}[flags]${NO_FORMAT} ${F_DIM}--output ${F_BOLD}<output_file> ...<file>${NO_FORMAT}
Flags:
-h, --help ${F_DIM}
Show this help text${NO_FORMAT}
-o, --output ${F_DIM}
Output file${NO_FORMAT}
EOF
)"
}
function main() {
eval set -- "$(getopt --options 'ho:' --long 'help,output:' -- "$@")"
file_out=""
for opt; do
case "$opt" in
--output | -o)
file_out="$2"
shift 2
;;
--help | -h)
usage
exit 0
;;
--)
shift 1
break
;;
esac
done
if [ -z "$file_out" ]; then
error "The -o flag is required"
usage
exit 1
fi
ffmpeg_cmd=("ffmpeg")
filter_complex=""
i=0
for f in "$@"; do
ffmpeg_cmd+=("-i" "$f")
filter_complex+="[$i:v][$i:a]"
i="$((i + 1))"
done
filter_complex+=" concat=n=$i:v=1:a=1 [outv] [outa]"
ffmpeg_cmd+=(-filter_complex "$filter_complex")
ffmpeg_cmd+=(-map '[outv]' -map '[outa]' -preset 'slow')
ffmpeg_cmd+=("$file_out")
printf '%q\n' "${ffmpeg_cmd[@]}"
declare -a file_times
for f in "$@"; do
file_times+=("$(stat -c '%Y' "$f")")
done
max_time="$(printf '%s\n' "${file_times[@]}" | sort -n | tail -1)"
# add one second to the max time
max_time="$((max_time + 1))"
"${ffmpeg_cmd[@]}"
touch -d "@$max_time" "$file_out"
}
main "$@"