-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjoin-vids
More file actions
executable file
·173 lines (150 loc) · 3.95 KB
/
join-vids
File metadata and controls
executable file
·173 lines (150 loc) · 3.95 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
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
#!/bin/bash
# SPDX-FileCopyrightText: 2017 - 2024 sudorook <daemon@nullcodon.com>
#
# 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 -eu
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
#
# Functions
#
# Check if all files passed to script are mp4s.
function is_all_mp4s() {
for file in "${@}"; do
echo "${file}"
if ! [ "${file##*.}" = "mp4" ]; then
return 1
fi
done
return 0
}
# Check if all files passed to script are mkvs.
function is_all_mkvs() {
for file in "${@}"; do
echo "${file}"
if ! [ "${file##*.}" = "mkv" ]; then
return 1
fi
done
return 0
}
# Join mp4s by converting them to a transport stream (ts), concatenating the
# streams, and converting the result back to an mp4.
function join_mp4s() {
local name
local extension
local input="concat:"
local idx=0
for file in "${@}"; do
if [ "${file}" = "${OUTFILE}" ]; then
continue
fi
name=${file%.*}
extension=${file##*.}
ffmpeg -i "${file}" -c copy -bsf:v h264_mp4toannexb -f h264 "${name}.ts"
input="${input}${name}.ts|"
done
input=${input::-1}
rm -f "${OUTFILE}"
ffmpeg -i "${input}" -codec copy -bsf:a aac_adtstoasc "${OUTFILE}"
for file in "${@}"; do
name=${file%.*}
rm -f "${name}.ts"
done
}
# Join mkvs by using a concatenation file.
function join_mkvs() {
local concatfile="concat.txt"
local idx=0
for file in "${@}"; do
if [ "${file}" = "${OUTFILE}" ]; then
continue
fi
echo "file '${file}'" >> "${concatfile}"
done
rm -f "${OUTFILE}"
ffmpeg -f concat -i "${concatfile}" -codec copy "${OUTFILE}"
rm -f "${concatfile}"
}
# Join videos with mixed codecs using ffmpeg concat filter.
function join_mixed() {
local input=""
local filter=""
local idx=0
local cmd
for file in "${@}"; do
if [ "${file}" = "${OUTFILE}" ]; then
continue
fi
input="${input} -i file:${file@Q} "
filter="${filter}[${idx}:v:0] [${idx}:a:0] "
idx=$((idx + 1))
done
filter="${filter}concat=n=${#}:v=1:a=1 [v] [a]"
rm -f "${OUTFILE}"
cmd=$(echo ffmpeg "${input}" -filter_complex \""${filter}"\" \
-map \"[v]\" -map \"[a]\" "${OPTIONS}" "${OUTFILE}")
eval "${cmd}"
}
#
# Globals
#
OUTFILE=output.mp4
OPTIONS=""
#
# Main
#
# When all provided files are mp4s, use lossless concatenation by transcoding
# mp4s to mpeg transport stream (ts) files.
if is_all_mp4s "${@}"; then
show_header "Joining MP4s."
join_mp4s "${@}"
# When all provided files are mkvs, create a concat file with the filenames and
# use the -f concat option for ffmpeg.
elif is_all_mkvs "${@}"; then
show_header "Joining MKVs."
OUTFILE=output.mkv
join_mkvs "${@}"
# # If multiple codes are present, use the ffmpeg concat filter.
# else
# show_header "Joining mixed codecs."
# join_mixed "${@}"
# fi
# If multiple codecs are present, convert non-mp4 files to mp4s and then join
# them.
else
show_header "Converting mixed codecs to mp4 and then joining them."
mp4s=()
temp=()
for file in "${@}"; do
if [ "${file}" = "${OUTFILE}" ]; then
continue
fi
name=${file%.*}
extension=${file##*.}
if [ "${extension}" = "mp4" ]; then
mp4s+=("${file}")
else
ffmpeg -i "${file}" "${name}.mp4"
mp4s+=("${name}.mp4")
temp+=("${name}.mp4")
fi
done
join_mp4s "${mp4s[@]}"
for f in "${temp[@]}"; do
rm "${f}"
done
fi