-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
183 lines (154 loc) · 6.34 KB
/
render.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
import argparse
import os
import random
import subprocess
import tempfile
import numpy
import tqdm
from beatviewer import BeatTracker, Config, FileAudioSource
from beatviewer.video.video_reader import VideoReader
from beatviewer.beat_tracker import EventFlag, BeatTrackingEvent
class FFmpegVideoOutput:
def __init__(self, path: str, width: int, height: int, framerate: int,
vcodec: str = "h264"):
self.width = width
self.height = height
self.path = path
self.framerate = framerate
self.vcodec = vcodec
self.process = None
def __enter__(self):
dirname = os.path.dirname(self.path)
if not os.path.isdir(dirname) and dirname != "":
os.makedirs(dirname)
self.process = subprocess.Popen([
"ffmpeg",
"-hide_banner",
"-loglevel", "error",
"-f", "rawvideo",
"-vcodec","rawvideo",
"-s", f"{self.width}x{self.height}",
"-pix_fmt", "rgb24",
"-r", f"{self.framerate}",
"-i", "-",
"-r", f"{self.framerate}",
"-pix_fmt", "yuv420p",
"-an",
"-vcodec", self.vcodec,
self.path,
"-y"
], stdin=subprocess.PIPE)
return self
def feed(self, frame: numpy.ndarray):
self.process.stdin.write(frame.astype(numpy.uint8).tobytes())
def __exit__(self, exc_type, exc_value, exc_traceback):
self.process.stdin.close()
self.process.wait()
class Renderer:
def __init__(self, audio_path: str, video_path: str, output_path: str, config: Config):
self.audio_path = audio_path
self.video_path = video_path
self.output_path = output_path
self.events: list[BeatTrackingEvent] = []
self.duration: float = 0
self.frame_cursor: float = -1
self.event_cursor: int = 0
self.reader = VideoReader(self.video_path)
self.config = config
def analyze_audio(self):
audio_source = FileAudioSource(self.config, self.audio_path, pbar_kwargs={
"desc": "Analyzing audio"
})
tracker = BeatTracker(self.config, audio_source, register_events=True, warmup=True)
tracker.run()
self.events = tracker.events
self.duration = tracker.frame_index / tracker.sampling_rate_oss
def move_event_cursor(self, t: float) -> bool:
has_beat = False
while self.event_cursor < len(self.events) and self.events[self.event_cursor].time < t:
if self.events[self.event_cursor].flag == EventFlag.BEAT:
has_beat = True
self.event_cursor += 1
return has_beat
def update_frame_cursor(self, frame_index: int, has_beat: bool):
raise NotImplementedError()
def render_output(self, aux_path: str):
subprocess.Popen([
"ffmpeg",
"-hide_banner",
"-loglevel", "error",
"-stats",
"-i", aux_path,
"-i", self.audio_path,
"-c:v", "copy",
"-c:a", "aac",
self.output_path,
"-y"
]).wait()
def run(self):
self.analyze_audio()
self.reader.open()
aux_path = os.path.join(tempfile.gettempdir(), "foo.mp4")
output = FFmpegVideoOutput(aux_path, self.reader.width, self.reader.height, round(self.reader.fps))
with output:
for i in tqdm.tqdm(range(round(self.duration * self.reader.fps)), unit="frame", desc="Encoding video"):
t = i / self.reader.fps
has_beat = self.move_event_cursor(t)
self.update_frame_cursor(i, has_beat)
frame = self.reader.read_frame(int(self.frame_cursor))
output.feed(self.reader.convert_frame(frame, transpose=False))
self.render_output(aux_path)
class SeekOnBeatRenderer(Renderer):
def update_frame_cursor(self, frame_index: int, has_beat: bool):
if has_beat:
self.frame_cursor = random.randint(-1, self.reader.frame_count - 2)
self.frame_cursor = (self.frame_cursor + 1) % self.reader.frame_count
class SlowDownRenderer(Renderer):
def __init__(self, audio_path: str, video_path: str, output_path: str, config: Config, decay: float, jumpcut: bool):
Renderer.__init__(self, audio_path, video_path, output_path, config)
self.decay = decay
self.jumpcut = jumpcut
self.playback_speed = 1
def update_frame_cursor(self, frame_index: int, has_beat: bool):
if has_beat:
self.playback_speed = 1
if self.jumpcut:
self.frame_cursor = frame_index % self.reader.frame_count - 1
self.frame_cursor += self.playback_speed
if self.frame_cursor >= self.reader.frame_count:
self.frame_cursor -= self.reader.frame_count
self.playback_speed *= self.decay
def pipeline(audio_path: str, video_path: str, output_path: str, mode: str,
config: Config, decay: float = 0.9, jumpcut: bool = False,
execute: bool = True):
base_args = [audio_path, video_path, output_path, config]
if mode == "seek":
renderer = SeekOnBeatRenderer(*base_args)
elif mode == "slow":
renderer = SlowDownRenderer(*base_args, decay=decay, jumpcut=jumpcut)
else:
raise ValueError(f"Invalid mode: {mode}")
renderer.run()
if execute:
try:
os.startfile(os.path.realpath(output_path))
except AttributeError:
pass
def main():
parser = argparse.ArgumentParser()
parser.add_argument("audio_path", type=str)
parser.add_argument("video_path", type=str)
parser.add_argument("output_path", type=str)
parser.add_argument("-c", "--config", type=str, default=None, help="Path to a configuration file (see default 'config.txt')")
parser.add_argument("-m", "--mode", type=str, default="seek", choices=["seek", "slow"])
parser.add_argument("-d", "--decay", type=float, default=0.9)
parser.add_argument("-j", "--jumpcut", action="store_true")
args = parser.parse_args()
if args.config is not None:
config = Config.from_file(args.config)
else:
config = Config(bps_epsilon_t=0)
pipeline(args.audio_path, args.video_path, args.output_path, args.mode,
config, args.decay, args.jumpcut)
if __name__ == "__main__":
main()