-
Notifications
You must be signed in to change notification settings - Fork 3
/
concat.py
189 lines (151 loc) · 5.4 KB
/
concat.py
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
180
181
182
183
184
185
186
187
188
189
import argparse
import os
import subprocess
import sys
from tqdm import tqdm
from alabamaEncode.core.util.bin_utils import get_binary
from alabamaEncode.core.ffmpeg import Ffmpeg
from alabamaEncode.core.util.path import PathAlabama
# example usage:
# python genConcat.py out.mp4
# python genConcat.py Epic.mkv ./temp/
# Command line arguments
parser = argparse.ArgumentParser(
prog="Concater",
description="Concatenate all .ivf files in a directory",
epilog="""Example usage:
python concat.py out.mp4
python concat.py Epic.mkv ./temp/""",
)
parser.add_argument("output", help="Output file name")
# optional temp dir
parser.add_argument(
"temp_dir",
help="Temp directory",
nargs="?",
default="temp/",
type=str,
metavar="temp_dir",
)
# optinal flag that muxes the audio
parser.add_argument("-a", "--audio", help="Mux audio", action="store_true")
# max .ivf index number
parser.add_argument(
"-m", "--max", help="Max .ivf index number", type=int, default=-1, metavar="max"
)
parser.add_argument("-e", help="extension of files to concat", type=str, default="ivf")
parser.add_argument("--start_offset", help="start offset", type=int, default=0)
args = parser.parse_args()
mux_audio = args.audio
# output file name
output = args.output
ext = args.e
# temp dir
tmp_dir = args.temp_dir
# if output is not set, quit
if output == "" or output is None:
print("Output file name not set")
sys.exit(1)
print("Output file name: " + output)
# make sure the temp dir ends with a slash
if tmp_dir[-1] != "/":
tmp_dir += "/"
# make sure the temp dir doesn't start with a slash
if tmp_dir[0] == "/":
tmp_dir = tmp_dir[1:]
# make sure the directory exists
if not os.path.exists(tmp_dir):
print(f"Directory {tmp_dir} does not exist")
sys.exit(1)
# make sure the directory is not empty
if not os.listdir(tmp_dir):
print(f"Directory {tmp_dir} is empty")
sys.exit(1)
# list of file names so we can sort alphabetically later
file_names = []
# list of invalid files so the user can delete them
invalid_files = []
for name in tqdm(os.listdir(tmp_dir), desc="Checking files"):
if name.endswith(f".{ext}"):
# get the file name
name = name[:-4]
# check if the filename is a number
if name.isdigit():
number_name = int(name)
# if the max flag is set and the number is greater than the max then skip it
if args.max != -1 and number_name > args.max:
continue
# add name to list
file_names.append(name)
# run ffmpeg command that checks if the file is valid
# ffmpeg -v error -i $i -c copy -f null -
argv_ = f'ffmpeg -v error -i "{tmp_dir}{name}.{ext}" -c copy -f null -'
# if the command has any output then the file is invalid
p = subprocess.Popen(
argv_,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
p.wait()
proc_output = p.stdout.read()
if len(proc_output) > 0:
tqdm.write(f"Found invalid file: {name}.{ext}")
invalid_files.append(name)
for i in range(args.max):
if str(i) not in file_names:
tqdm.write(f"Missing file: {i}.{ext}")
invalid_files.append(str(i))
# if there are invalid files, print them out
if len(invalid_files) > 0:
# red text
print("\033[91m", end="")
print(f"Found invalid files")
print("To remove run:")
# assamble the command to remove the invalid files
command = "rm "
for name in invalid_files:
command += f"{tmp_dir}{name}.{ext} "
print(command)
quit()
# treat the names like numbers and sort them
file_names.sort(key=lambda f: int("".join(filter(str.isdigit, f))))
with open("mhmconcat", "w") as f:
for name in file_names:
f.write(f"file '{tmp_dir}{name}.{ext}'\n")
# if the audio flag is set, then mux the audio
if mux_audio:
print("muxing audio innt luv")
output_ = f'ffmpeg -v error -f concat -safe 0 -i mhmconcat -movflags +faststart -c:v copy "{output}.TEMP.mkv"'
print("Running command: " + output_)
os.system(output_)
video_length = Ffmpeg.get_video_length(PathAlabama(f"{output}.TEMP.mkv"))
offset = f"-ss {args.start_offset}" if args.start_offset != 0 else ""
audio_enc = (
f'ffmpeg -y -v error {offset} -i "{tmp_dir}/temp.mkv" '
f'-c:a libopus -ac 2 -b:a 96k -map 0:a:0 -t {video_length} "{output}.AUDIO.mkv"'
)
print("Running command: " + audio_enc)
os.system(audio_enc)
kumannds = []
kumannds.append(
f'ffmpeg -y -v error -i "{output}.TEMP.mkv" -i "{output}.AUDIO.mkv" -map 0:v -map 1:a '
f'-movflags +faststart -c:a copy -c:v copy "{output}"'
)
# second command that uses mkvmerge to write additional metadata & fixup the container
# kumannds.append(f'mkvmerge -o "{output}" "{output}.TTEMP.mkv"')
# remove temp
kumannds.append(f'rm "{output}.TEMP.mkv"')
kumannds.append(f'rm "{output}.AUDIO.mkv"')
# kumannds.append(f'rm "{output}.TTEMP.mkv"')
for command in kumannds:
print("Running: " + command)
os.system(command)
else:
print("not muxing audio")
argv_ = f'{get_binary("ffmpeg")} -v error -f concat -safe 0 -i mhmconcat -c copy "{output}"'
print("Running: " + argv_)
os.system(argv_)
print("Removing mhmconcat")
os.system("rm mhmconcat")