-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.py
579 lines (481 loc) · 30.9 KB
/
encode.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# Copyright (C) 2022 luckytyphlosion
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import subprocess
import ffmpeg
import os
import re
import pathlib
import platform
import dir_config
import job_process
import PyRKG.VideoGenerator
import transform2d
from stateclasses.timeline_classes import *
from stateclasses.encode_classes import *
from stateclasses.music_option_classes import *
from stateclasses.input_display import *
from constants.encode import *
nb_frames_regex = re.compile(r"^nb_frames=([0-9]+)", flags=re.MULTILINE)
dev_null = "/dev/null" if os.name == "posix" else "NUL"
platform_system = platform.system()
class DynamicFilterArgs:
__slots__ = ("adelay_frame_value", "fade_start_frame", "trim_start_frame", "input_display_start_frame")
def __init__(self, adelay_frame_value, fade_start_frame, trim_start_frame, input_display_start_frame):
self.adelay_frame_value = adelay_frame_value
self.fade_start_frame = fade_start_frame
self.trim_start_frame = trim_start_frame
self.input_display_start_frame = input_display_start_frame
FROM_TT_GHOST_SELECT_TRACK_LOADING_BLACK_SCREEN_FRAME_TIMESTAMP = 186
FROM_TT_GHOST_SELECT_TRACK_LOADING_BLACK_SCREEN_TIMESTAMP = FROM_TT_GHOST_SELECT_TRACK_LOADING_BLACK_SCREEN_FRAME_TIMESTAMP/59.94005994006
class InputDisplayGfxInfo:
__slots__ = ("input_box_filename", "box_x", "box_y", "inputs_x", "inputs_y", "inputs_width", "inputs_height")
def __init__(self, input_box_filename, inputs_width, inputs_height, inputs_x, inputs_y, box_x, box_y):
self.input_box_filename = input_box_filename
self.inputs_width = inputs_width
self.inputs_height = inputs_height
self.inputs_x = inputs_x
self.inputs_y = inputs_y
self.box_x = box_x
self.box_y = box_y
# positions and scaling were manually calculated
# these are not centered by any means, and are some pixels off for each resolution
dolphin_resolution_to_input_display_gfx_info = {
"480p": InputDisplayGfxInfo("data/input_box_480p.png", 203, 127, 22, 361, 32, 366),
"720p": InputDisplayGfxInfo("data/input_box_720p.png", None, None, 56, 724, 75, 734),
"1080p": InputDisplayGfxInfo("data/input_box_1080p.png", 608, 380, 76, 1082, 106, 1102),
"1440p": InputDisplayGfxInfo("data/input_box_2k.png", 810, 516, 88, 1447, 129, 1471),
"2160p": InputDisplayGfxInfo("data/input_box.png", 1216, 769, 148, 2161, 209, 2195),
}
# 2160p input box = 1060x681
#ENABLE_EQUALS =
class Encoder:
__slots__ = ("ffmpeg_filename", "ffprobe_filename", "dolphin_resolution", "music_option", "timeline_settings", "video_frame_durations", "print_cmd")
def __init__(self, ffmpeg_filename, ffprobe_filename, dolphin_resolution, music_option, timeline_settings, print_cmd=False):
self.ffmpeg_filename = ffmpeg_filename
self.ffprobe_filename = ffprobe_filename
self.dolphin_resolution = dolphin_resolution
self.music_option = music_option
self.timeline_settings = timeline_settings
self.video_frame_durations = {}
self.print_cmd = print_cmd
def get_video_frame_duration(self, video_file=None):
if video_file is None:
video_file = f"{dir_config.dolphin_dirname}/User/Dump/Frames/framedump0.avi"
video_frame_duration = self.video_frame_durations.get(video_file)
if video_frame_duration is not None:
return video_frame_duration
framedump_ffprobe_output = subprocess.check_output((self.ffprobe_filename, "-v", "0", "-select_streams", "v", "-show_entries", "stream=nb_frames", video_file), stderr=subprocess.STDOUT).decode(encoding="ascii")
nb_frames_match_obj = nb_frames_regex.search(framedump_ffprobe_output)
if not nb_frames_match_obj:
raise RuntimeError("FFmpeg command did not return dspdump.wav audio duration!")
video_frame_duration = int(nb_frames_match_obj.group(1))
self.video_frame_durations[video_file] = video_frame_duration
return video_frame_duration
def encode_stream_copy(self, output_video_filename):
ffmpeg_stream_copy_cmd = (self.ffmpeg_filename, "-y", "-i", f"{dir_config.dolphin_dirname}/User/Dump/Frames/framedump0.avi", "-i", f"{dir_config.dolphin_dirname}/User/Dump/Audio/dspdump.wav", "-c", "copy", output_video_filename)
if platform_system == "Windows":
job_process.run_subprocess_as_job(ffmpeg_stream_copy_cmd)
else:
subprocess.run(
ffmpeg_stream_copy_cmd, check=True
)
def gen_dynamic_filter_args(self, fade_frame_duration):
output_params = {}
with open(f"{dir_config.dolphin_dirname}/output_params.txt", "r") as f:
for line in f:
if line.strip() == "":
continue
param_name, param_value = line.split(": ", maxsplit=1)
output_params[param_name] = param_value.strip()
frame_replay_starts = int(output_params["frameReplayStarts"])
frame_recording_starts = int(output_params["frameRecordingStarts"])
adelay_frame_value = frame_replay_starts - frame_recording_starts
video_frame_len = self.get_video_frame_duration()
fade_start_frame = video_frame_len - fade_frame_duration
trim_start_frame = frame_replay_starts - frame_recording_starts
frame_input_starts = int(output_params["frameInputStarts"])
input_display_start_frame = frame_input_starts - frame_recording_starts
return DynamicFilterArgs(adelay_frame_value, fade_start_frame, trim_start_frame, input_display_start_frame)
def add_input_display_to_video_new(self, video_in_file, dynamic_filter_args):
input_display = self.timeline_settings.input_display
video_generator = PyRKG.VideoGenerator.VideoGenerator(input_display.name, input_display.rkg_file_or_data)
print("Generating input display!")
if not input_display.dont_create:
video_generator.run(f"{dir_config.temp_dirname}/input_display.mov", self.ffmpeg_filename)
input_display_frame_duration = video_generator.inputs.get_total_frame_nr()
#input_display_gfx_info = dolphin_resolution_to_input_display_gfx_info[self.dolphin_resolution]
input_box_in_file = ffmpeg.input(input_display.box_filename)
input_display_start_frame = dynamic_filter_args.input_display_start_frame
input_display_end_frame = input_display_start_frame + input_display_frame_duration - 1
input_display_in_file = ffmpeg.input(f"{dir_config.temp_dirname}/input_display.mov")
#input_box_shifted = input_box_in_file#.setpts(f"PTS-STARTPTS+{input_display_start_frame}")
ffmpeg_in_streams_info = {
"canvas": transform2d.FFmpegInStreamInfo("canvas", video_in_file),
"input_box": transform2d.FFmpegInStreamInfo("input_box", input_box_in_file,
start_frame=input_display_start_frame,
end_frame=input_display_end_frame,
after_setpts="PTS-STARTPTS",
scale_flags="bicubic"
),
"inputs": transform2d.FFmpegInStreamInfo("inputs", input_display_in_file,
start_frame=input_display_start_frame,
end_frame=input_display_end_frame,
before_setpts=f"PTS-STARTPTS+{input_display_start_frame/FPS}/TB",
eof_action="pass"
)
}
video_with_input_display = transform2d.calc_overlay_objs_coords_dimensions(self.dolphin_resolution, input_display, self.timeline_settings.encode_settings.aspect_ratio_16_by_9, ffmpeg_in_streams_info)
return video_with_input_display
#box_on_video = ffmpeg.filter(
# (video_in_file, input_box_in_file),
# "overlay",
# enable=f"between(t,{input_display_start_frame/FPS},{input_display_end_frame/FPS})",
# x=input_display_gfx_info.box_x, y=input_display_gfx_info.box_y,
# eval="init"
#).setpts("PTS-STARTPTS")
#
#
#if input_display_gfx_info.inputs_width is not None:
# scaled_input_display = ffmpeg.filter(input_display_in_file, "scale",
# input_display_gfx_info.inputs_width, input_display_gfx_info.inputs_height, flags="bicubic"
# )
#else:
# scaled_input_display = input_display_in_file
#
## +{input_display_start_frame/60}/TB
## {input_display_start_frame}
#print(f"input_display_start_frame: {input_display_start_frame}")
#scaled_input_display_shifted = scaled_input_display.setpts(f"PTS-STARTPTS+{input_display_start_frame/FPS}/TB")
#video_with_input_display = ffmpeg.filter(
# (box_on_video, scaled_input_display_shifted),
# "overlay",
# enable=f"between(t,{input_display_start_frame/FPS},{input_display_end_frame/FPS})",
# x=input_display_gfx_info.inputs_x, y=input_display_gfx_info.inputs_y,
# eof_action="pass", eval="init"
#)
#
#return video_with_input_display
def add_input_display_to_video(self, video_in_file, dynamic_filter_args):
video_generator = PyRKG.VideoGenerator.VideoGenerator("classic", self.timeline_settings.input_display.rkg_file_or_data)
print("Generating input display!")
if not self.timeline_settings.input_display.dont_create:
video_generator.run(f"{dir_config.temp_dirname}/input_display.mov", self.ffmpeg_filename)
input_display_frame_duration = video_generator.inputs.get_total_frame_nr()
input_display_gfx_info = dolphin_resolution_to_input_display_gfx_info[self.dolphin_resolution]
input_box_in_file = ffmpeg.input(input_display_gfx_info.input_box_filename)
input_display_start_frame = dynamic_filter_args.input_display_start_frame
input_display_end_frame = input_display_start_frame + input_display_frame_duration - 1
#input_box_shifted = input_box_in_file#.setpts(f"PTS-STARTPTS+{input_display_start_frame}")
box_on_video = ffmpeg.filter(
(video_in_file, input_box_in_file),
"overlay",
enable=f"between(t,{input_display_start_frame/FPS},{input_display_end_frame/FPS})",
x=input_display_gfx_info.box_x, y=input_display_gfx_info.box_y,
eval="init"
).setpts(f"PTS-STARTPTS")
input_display_in_file = ffmpeg.input(f"{dir_config.temp_dirname}/input_display.mov")
if input_display_gfx_info.inputs_width is not None:
scaled_input_display = ffmpeg.filter(input_display_in_file, "scale",
input_display_gfx_info.inputs_width, input_display_gfx_info.inputs_height, flags="bicubic"
)
else:
scaled_input_display = input_display_in_file
# +{input_display_start_frame/60}/TB
# {input_display_start_frame}
print(f"input_display_start_frame: {input_display_start_frame}")
scaled_input_display_shifted = scaled_input_display.setpts(f"PTS-STARTPTS+{input_display_start_frame/FPS}/TB")
video_with_input_display = ffmpeg.filter(
(box_on_video, scaled_input_display_shifted),
"overlay",
enable=f"between(t,{input_display_start_frame/FPS},{input_display_end_frame/FPS})",
x=input_display_gfx_info.inputs_x, y=input_display_gfx_info.inputs_y,
eof_action="pass", eval="init"
)
return video_with_input_display
def add_in_music(self, music_volume_stream, game_volume_stream):
encode_settings = self.timeline_settings.encode_settings
if encode_settings.music_volume != 1.0:
music_volume_stream = ffmpeg.filter(music_volume_stream, "volume", volume=encode_settings.music_volume)
game_volume_stream = ffmpeg.filter(game_volume_stream, "volume", volume=encode_settings.game_volume)
audio_combined_stream = ffmpeg.filter([game_volume_stream, music_volume_stream], "amix", inputs=2, duration="first")
return audio_combined_stream
def encode_complex_common(self, secondary_video_in_filename, secondary_audio_in_filename):
secondary_video_in_file = ffmpeg.input(secondary_video_in_filename)
secondary_audio_in_file = ffmpeg.input(secondary_audio_in_filename)
dolphin_resolution = self.dolphin_resolution
music_option = self.music_option
timeline_settings = self.timeline_settings
has_secondary_video_in_file = secondary_video_in_filename is not None and secondary_audio_in_filename is not None
if has_secondary_video_in_file:
secondary_video_in_file_frame_len = self.get_video_frame_duration(secondary_video_in_filename)
else:
secondary_video_in_file_frame_len = 0
encode_settings = timeline_settings.encode_settings
fade_frame_duration = encode_settings.fade_frame_duration
dynamic_filter_args = self.gen_dynamic_filter_args(fade_frame_duration)
video_in_file = ffmpeg.input(f"{dir_config.dolphin_dirname}/User/Dump/Frames/framedump0.avi")
audio_in_file = ffmpeg.input(f"{dir_config.dolphin_dirname}/User/Dump/Audio/dspdump.wav")
if music_option.option == MUSIC_CUSTOM_MUSIC and not music_option.start_music_at_beginning:
music_in_file = ffmpeg.input(music_option.music_filename).audio
audio_combined_stream = self.add_in_music(music_in_file, audio_in_file)
else:
audio_combined_stream = audio_in_file
if timeline_settings.input_display.type in {INPUT_DISPLAY_CLASSIC, INPUT_DISPLAY_NUNCHUCK}:
video_with_input_display = self.add_input_display_to_video_new(video_in_file, dynamic_filter_args)
elif timeline_settings.input_display.type == INPUT_DISPLAY_NONE:
video_with_input_display = transform2d.scale_input_stream_to_16_by_9_if_necessary(encode_settings, dolphin_resolution, video_in_file)
else:
assert False
video_faded_stream = ffmpeg.filter(video_with_input_display, "fade", type="out", duration=fade_frame_duration/FPS, start_time=dynamic_filter_args.fade_start_frame/FPS)
if not music_option.start_music_at_beginning:
audio_combined_maybefaded_stream = ffmpeg.filter(audio_combined_stream, "afade", type="out", duration=fade_frame_duration/FPS, start_time=dynamic_filter_args.fade_start_frame/FPS)
else:
audio_combined_maybefaded_stream = audio_combined_stream
if has_secondary_video_in_file:
secondary_video_in_file = transform2d.scale_input_stream_to_16_by_9_if_necessary(encode_settings, dolphin_resolution, secondary_video_in_file)
all_streams = [
secondary_video_in_file,
secondary_audio_in_file,
video_faded_stream,
audio_combined_maybefaded_stream
]
almost_final_streams = ffmpeg.filter_multi_output(all_streams, "concat", n=2, v=1, a=1)
almost_final_video_stream = almost_final_streams[0]
almost_final_audio_stream = almost_final_streams[1]
else:
almost_final_video_stream = video_faded_stream
almost_final_audio_stream = audio_combined_maybefaded_stream
if music_option.start_music_at_beginning:
music_in_file = ffmpeg.input(music_option.music_filename).audio
almost_final_audio_stream = self.add_in_music(music_in_file, almost_final_audio_stream)
almost_final_audio_stream = ffmpeg.filter(almost_final_audio_stream, "afade", type="out", duration=fade_frame_duration/FPS, start_time=(secondary_video_in_file_frame_len + dynamic_filter_args.fade_start_frame)/FPS)
if encode_settings.fade_in_at_start:
almost_final_video_stream = ffmpeg.filter(almost_final_video_stream, "fade", type="in", duration=30/FPS, start_time=0)
final_audio_stream = ffmpeg.filter(almost_final_audio_stream, "afade", type="in", duration=30/FPS, start_time=0)
else:
final_audio_stream = almost_final_audio_stream
if encode_settings.output_width is not None:
final_video_stream = ffmpeg.filter(almost_final_video_stream, "scale", encode_settings.output_width, "round(ow/a/2)*2", flags="bicubic")
else:
final_video_stream = ffmpeg.filter(almost_final_video_stream, "crop", "trunc(iw/2)*2", "trunc(ih/2)*2")
return final_video_stream, final_audio_stream, dynamic_filter_args
def encode_from_top_10_leaderboard(self):
top_10_video_in_filename = f"{dir_config.dolphin_dirname}/User/Dump/Frames/top10.avi"
top_10_audio_in_filename = f"{dir_config.dolphin_dirname}/User/Dump/Audio/top10.wav"
return self.encode_complex_common(top_10_video_in_filename, top_10_audio_in_filename)
def encode_from_tt_ghost_select(self):
tt_ghost_select_video_in_filename = f"{dir_config.dolphin_dirname}/User/Dump/Frames/tt_ghost_select.avi"
tt_ghost_select_audio_in_filename = f"{dir_config.dolphin_dirname}/User/Dump/Audio/tt_ghost_select.wav"
return self.encode_complex_common(tt_ghost_select_video_in_filename, tt_ghost_select_audio_in_filename)
def rename_passlogfile_hyphen_1(self, passlogfile_prefix_filepath):
# stupid fix due to ffmpeg update
# it seems that ffmpeg will set the ffmpeg2pass suffix to 1
# because there is an audio stream in the output
# even though `-an` is specified
# and I do not see a way to get rid of the audio stream in the output
# if you manually rename the passlogfile to have suffix 0 it works fine
# so just do that
passlogfile_hyphen_0_filepath = passlogfile_prefix_filepath.with_name("passlogfile-0.log")
passlogfile_hyphen_0_mbtree_filepath = passlogfile_prefix_filepath.with_name("passlogfile-0.log.mbtree")
passlogfile_hyphen_1_filepath = passlogfile_prefix_filepath.with_name("passlogfile-1.log")
passlogfile_hyphen_1_mbtree_filepath = passlogfile_prefix_filepath.with_name("passlogfile-1.log.mbtree")
if passlogfile_hyphen_1_filepath.is_file():
passlogfile_hyphen_0_filepath.unlink(missing_ok=True)
os.rename(passlogfile_hyphen_1_filepath, passlogfile_hyphen_0_filepath)
if passlogfile_hyphen_1_mbtree_filepath.is_file():
passlogfile_hyphen_0_mbtree_filepath.unlink(missing_ok=True)
os.rename(passlogfile_hyphen_1_mbtree_filepath, passlogfile_hyphen_0_mbtree_filepath)
def encode_complex(self, output_video_filename):
dolphin_resolution = self.dolphin_resolution
music_option = self.music_option
timeline_settings = self.timeline_settings
encode_settings = timeline_settings.encode_settings
if timeline_settings.type == TIMELINE_GHOST_ONLY:
final_video_stream, final_audio_stream, dynamic_filter_args = self.encode_complex_common(None, None)
elif timeline_settings.type == TIMELINE_FROM_TT_GHOST_SELECTION:
final_video_stream, final_audio_stream, dynamic_filter_args = self.encode_from_tt_ghost_select()
elif timeline_settings.type in (TIMELINE_FROM_TOP_10_LEADERBOARD, TIMELINE_FROM_MK_CHANNEL_GHOST_SCREEN):
final_video_stream, final_audio_stream, dynamic_filter_args = self.encode_from_top_10_leaderboard()
else:
raise RuntimeError(f"Unknown timeline type \"{timeline_settings.type}\"!")
#final_video_stream =
if encode_settings.type == ENCODE_TYPE_CRF:
ffmpeg_output_kwargs = {
"vcodec": encode_settings.video_codec,
"crf": encode_settings.crf,
"acodec": encode_settings.audio_codec,
"audio_bitrate": encode_settings.audio_bitrate,
"pix_fmt": encode_settings.pix_fmt,
"preset": encode_settings.h26x_preset
}
if encode_settings.output_format == "mp4":
if encode_settings.audio_codec == "libopus":
ffmpeg_output_kwargs["strict"] = "-2"
ffmpeg_output_kwargs["movflags"] = "+faststart"
if encode_settings.youtube_settings:
if encode_settings.video_codec == "libx264":
ffmpeg_output_kwargs.update({
"bf": 2,
"g": 30,
"profile:v": "high"
})
elif encode_settings.video_codec == "libx265":
ffmpeg_output_kwargs["x265-params"] = "no-open-gop=1:keyint=30:bframes=2"
else:
assert False
output_stream = ffmpeg.output(final_video_stream, final_audio_stream, output_video_filename, **ffmpeg_output_kwargs)
if platform_system == "Windows":
ffmpeg_final_command = ffmpeg.compile(output_stream, cmd=self.ffmpeg_filename, overwrite_output=True)
job_process.run_subprocess_as_job(ffmpeg_final_command)
else:
ffmpeg_final_command = ffmpeg.compile(output_stream, cmd=self.ffmpeg_filename, overwrite_output=True)
print(f"ffmpeg_final_command: {ffmpeg_final_command}")
ffmpeg.run(output_stream, cmd=self.ffmpeg_filename, overwrite_output=True)
elif encode_settings.type == ENCODE_TYPE_SIZE_BASED:
encode_size_bits = encode_settings.encode_size * 8
if timeline_settings.type == TIMELINE_FROM_TT_GHOST_SELECTION:
run_frame_len = self.get_video_frame_duration() + self.get_video_frame_duration(f"{dir_config.dolphin_dirname}/User/Dump/Frames/tt_ghost_select.avi")
elif timeline_settings.type in (TIMELINE_FROM_TOP_10_LEADERBOARD, TIMELINE_FROM_MK_CHANNEL_GHOST_SCREEN):
run_frame_len = self.get_video_frame_duration() + self.get_video_frame_duration(f"{dir_config.dolphin_dirname}/User/Dump/Frames/top10.avi")
elif timeline_settings.type == TIMELINE_GHOST_ONLY:
run_frame_len = self.get_video_frame_duration()
else:
assert False
print(f"run_frame_len: {run_frame_len}, run_len: {run_frame_len/FPS}")
if encode_settings.video_codec == "libx264":
dampening_factor = 0.98
else:
dampening_factor = 0.99
avg_video_bitrate = int(dampening_factor * (encode_size_bits*FPS/run_frame_len - encode_settings.audio_bitrate))
ffmpeg_output_kwargs = {
"vcodec": encode_settings.video_codec,
"video_bitrate": avg_video_bitrate,
"pix_fmt": encode_settings.pix_fmt
}
if encode_settings.video_codec == "libvpx-vp9":
ffmpeg_output_kwargs.update({
"row-mt": 1,
"threads": 8
})
elif encode_settings.video_codec == "libx264":
ffmpeg_output_kwargs["preset"] = "slow"
else:
assert False
if encode_settings.output_format == "mp4":
ffmpeg_output_kwargs["movflags"] = "+faststart"
passlogfile_prefix_filepath = pathlib.Path(dir_config.temp_dirname) / "passlogfile"
passlogfile_prefix = str(passlogfile_prefix_filepath)
ffmpeg_output_kwargs_pass1 = ffmpeg_output_kwargs.copy()
ffmpeg_output_kwargs_pass1.update({
#"acodec": encode_settings.audio_codec,
#"audio_bitrate": encode_settings.audio_bitrate,
"pass": 1,
"format": "null",
"an": None,
"passlogfile": passlogfile_prefix
})
output_stream_pass1 = ffmpeg.output(final_video_stream, dev_null, **ffmpeg_output_kwargs_pass1)
ffmpeg_output_kwargs_pass2 = ffmpeg_output_kwargs.copy()
ffmpeg_output_kwargs_pass2.update({
"acodec": encode_settings.audio_codec,
"audio_bitrate": encode_settings.audio_bitrate,
"pass": 2,
"passlogfile": passlogfile_prefix
})
# ffmpeg segfaults when doing a 2 pass encoding with mp4???
# but it works fine with mkv
# so do a 2 pass using libx264 in mkv, then stream copy to mp4
if encode_settings.output_format == "mp4":
output_video_filepath_as_mkv = pathlib.Path(output_video_filename).with_suffix(".mkv")
tentative_output_video_filename = str(output_video_filepath_as_mkv)
else:
tentative_output_video_filename = output_video_filename
output_stream_pass2 = ffmpeg.output(final_video_stream, final_audio_stream, tentative_output_video_filename, **ffmpeg_output_kwargs_pass2)
if platform_system == "Windows":
pass1_command = ffmpeg.compile(output_stream_pass1, cmd=self.ffmpeg_filename, overwrite_output=True)
job_process.run_subprocess_as_job(pass1_command)
self.rename_passlogfile_hyphen_1(passlogfile_prefix_filepath)
pass2_command = ffmpeg.compile(output_stream_pass2, cmd=self.ffmpeg_filename, overwrite_output=True)
job_process.run_subprocess_as_job(pass2_command)
else:
pass1_command = ffmpeg.compile(output_stream_pass1, cmd=self.ffmpeg_filename, overwrite_output=True)
print(f"pass1_command: {pass1_command}")
ffmpeg.run(output_stream_pass1, cmd=self.ffmpeg_filename, overwrite_output=True)
self.rename_passlogfile_hyphen_1(passlogfile_prefix_filepath)
ffmpeg.run(output_stream_pass2, cmd=self.ffmpeg_filename, overwrite_output=True)
if encode_settings.output_format == "mp4":
mkv_to_mp4_args = [self.ffmpeg_filename, "-y", "-i", tentative_output_video_filename, "-c", "copy"]
if encode_settings.audio_codec == "libopus":
mkv_to_mp4_args.extend(("-strict", "-2"))
# -strict -2 must be before output video
mkv_to_mp4_args.append(output_video_filename)
if platform_system == "Windows":
job_process.run_subprocess_as_job(mkv_to_mp4_args)
else:
subprocess.run(mkv_to_mp4_args, check=True)
output_video_filepath_as_mkv.unlink(missing_ok=True)
else:
assert False
def encode(self, output_video_filename):
output_video_path = pathlib.Path(output_video_filename)
output_video_path.parent.mkdir(parents=True, exist_ok=True)
if self.timeline_settings.type == TIMELINE_NO_ENCODE:
self.encode_stream_copy(output_video_filename)
elif self.timeline_settings.type in (TIMELINE_GHOST_ONLY, TIMELINE_FROM_TT_GHOST_SELECTION, TIMELINE_FROM_TOP_10_LEADERBOARD, TIMELINE_FROM_MK_CHANNEL_GHOST_SCREEN):
self.encode_complex(output_video_filename)
else:
raise RuntimeError("Unimplemented timeline settings type!")
# return f"[2:a]adelay={adelay_value}|{adelay_value}[music_delayed];\
# [1:a]volume=0.6[game_audio_voldown];\
# [game_audio_voldown][music_delayed]amix=inputs=2:duration=first[audio_combined];\
# [0:v]fade=type=out:duration={fade_duration}:start_time={fade_start_time},split[video_faded_out1][video_faded_out2];\
# [audio_combined]afade=type=out:duration={fade_duration}:start_time={fade_start_time},asplit[audio_combined_faded_out1][audio_combined_faded_out2];\
# [video_faded_out1]trim=end=3.1,setpts=PTS-STARTPTS[v0];\
# [audio_combined_faded_out1]atrim=end=3.1,asetpts=PTS-STARTPTS[a0];\
# [video_faded_out2]trim=start={trim_start},setpts=PTS-STARTPTS[v1];\
# [audio_combined_faded_out2]atrim=start={trim_start},asetpts=PTS-STARTPTS[a1];\
# [v0][a0][v1][a1]concat=n=2:v=1:a=1[v_almost_final][a];\
# [v_almost_final]scale=2560:trunc(ow/a/2)*2:flags=bicubic[v]"
# '[0]fade=duration=2.5:start_time=37.02:type=out[s0];[s0]split=2[s1][s2];[s1]trim=end=3.1[s3];[s3]setpts=PTS-STARTPTS[s4];[1]volume=volume=0.6[s5];[2]adelay=6566.666666666667|6566.666666666667[s6];[s5][s6]amix=duration=first:inputs=2[s7];[s7]afade=duration=2.5:start_time=37.02:type=out[s8];[s8]asplit=2[s9][s10];[s9]atrim=end=3.1[s11];[s11]asetpts=PTS-STARTPTS[s12];[s2]trim=start=6.566666666666666[s13];[s13]setpts=PTS-STARTPTS[s14];[s10]atrim=start=6.566666666666666[s15];[s15]asetpts=PTS-STARTPTS[s16];[s4][s12][s14][s16]concat=a=1:n=2:v=1[s17];[s17]split=2[s18][s19];[s19]scale=854:trunc(ow/a/2)*2:flags=bicubic[s20]',
def encode_video(output_video_filename, ffmpeg_filename, ffprobe_filename, dolphin_resolution, music_option, timeline_settings):
encoder = Encoder(ffmpeg_filename, ffprobe_filename, dolphin_resolution, music_option, timeline_settings)
encoder.encode(output_video_filename)
def test_generated_command():
# output_format, crf, h26x_preset, video_codec, audio_codec, audio_bitrate, output_width, pix_fmt
# , CrfEncodeSettings, SizeBasedEncodeSettings
# SizeBasedEncodeSettings(output_format, video_codec, audio_codec, audio_bitrate, encode_size, output_width, pix_fmt)
MODE = 0
if MODE == 0:
crf_encode_settings = CrfEncodeSettings("mkv", 18, "medium", "libx264", "libopus", "128k", 854, "yuv420p", True, 0.6, 1.0)
timeline_settings = FromTTGhostSelectionTimelineSettings(crf_encode_settings, InputDisplay(INPUT_DISPLAY_CLASSIC, True))
music_option = MusicOption(MUSIC_CUSTOM_MUSIC, "bubble_bath_the_green_orbs.wav")
encoder = Encoder("ffmpeg", "ffprobe", "480p", music_option, timeline_settings, print_cmd=True)
encoder.encode("test_crf_command.mkv")
elif MODE == 1:
#size_based_encode_settings = SizeBasedEncodeSettings("webm", "libvpx-vp9", "libopus", "64k", 52428800, None, "yuv420p")
size_based_encode_settings = SizeBasedEncodeSettings("mp4", "libx264", "libopus", "64k", 52428800, None, "yuv420p", 0.6, 1.0)
timeline_settings = FromTTGhostSelectionTimelineSettings(size_based_encode_settings)
music_option = MusicOption(MUSIC_CUSTOM_MUSIC, "bubble_bath_the_green_orbs.wav")
encoder = Encoder("ffmpeg", "ffprobe", "480p", music_option, timeline_settings, print_cmd=False)
encoder.encode("test_size_based_command.mp4")
elif MODE == 2:
crf_encode_settings = CrfEncodeSettings("mkv", 18, "medium", "libx264", "libopus", "128k", None, "yuv420p", True, 0.6, 1.0)
timeline_settings = FromTTGhostSelectionTimelineSettings(crf_encode_settings)
music_option = MusicOption(MUSIC_GAME_BGM)
encoder = Encoder("ffmpeg", "ffprobe", "480p", music_option, timeline_settings, print_cmd=False)
encoder.encode("test_game_bgm_crf.mkv")
if __name__ == "__main__":
test_generated_command()