-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwrite_video.py
49 lines (41 loc) · 1.41 KB
/
write_video.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
"""write_video.py: Writes PyGame frames to a video."""
__author__ = "Gavin Uberti"
import os
import subprocess as sp
import time
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "hide"
from pygame import surfarray
import numpy as np
class FramePipeline:
def __init__(self, filename, size=(720, 720), framerate=24):
command = ['ffmpeg',
'-y', # Overwrite existing file
'-f', 'rawvideo',
'-vcodec', 'rawvideo',
'-pix_fmt', 'rgb24',
'-s', '{}x{}'.format(*size),
'-r', str(framerate), # FPS
'-i', '-', # Read from STDIN
'-an', # No audio
'-vcodec', 'libx264',
'-preset', 'ultrafast',
filename ]
self.pipeline = sp.Popen(command, stdin=sp.PIPE, stderr=sp.STDOUT)
def write_surface(self, surface):
data = surfarray.array3d(surface)
data = data.swapaxes(0,1)
self.write_numpy(data)
def write_numpy(self, nparr):
self.pipeline.stdin.write(nparr.tostring())
def close(self):
self.pipeline.stdin.close()
self.pipeline.wait()
# For testing
def main():
pipeline = FramePipeline("test_out.mp4")
for l in range(0, 5 * 24):
arr = np.random.randint(255, size=(720, 720, 3), dtype=np.uint8)
pipeline.write_numpy(arr)
pipeline.close()
if __name__ == "__main__":
main()