-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffmpeg_encoder.py
92 lines (78 loc) · 2.58 KB
/
ffmpeg_encoder.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
# Copyright (C) 2025 Anthony Casagrande
# AGPL-3.0 license
from dataclasses import dataclass
import ffmpeg
import torch.cuda
@dataclass
class FfmpegVideoEncoder:
output_file: str
out_size: tuple[int, int]
fps: float | str
preset: str = 'p3'
qp: int = 21
use_cuda: bool = None
overwrite: bool = True
_encode_process = None
frames_written = 0
def __post_init__(self):
if self.use_cuda is None or self.use_cuda:
self.use_cuda = torch.cuda.is_available()
# configure GoPro's odd framerates as strings to avoid issues with floats
if 119 < self.fps < 120:
self.fps = "119.88"
elif 89 < self.fps < 90:
self.fps = "89.91"
elif 59 < self.fps < 60:
self.fps = "59.94"
elif 29 < self.fps < 30:
self.fps = "29.97"
def start_encoding(self):
# self._init_encode_process_nvenc()
# time.sleep(0.25)
# if self._encode_process.poll() is not None:
self._init_encode_process()
def write_frame(self, frame):
try:
self._encode_process.stdin.write(frame.tobytes())
self.frames_written += 1
except Exception as e:
print(f"ffmpeg write error: {e}")
def close(self):
self._encode_process.stdin.close()
self._encode_process.wait()
def _init_encode_process_nvenc(self):
out = (
ffmpeg
.input('pipe:',
format='rawvideo',
pix_fmt='bgr24',
s=f'{self.out_size[0]}x{self.out_size[1]}',
framerate=self.fps)
.output(self.output_file,
vcodec='hevc_nvenc',
pix_fmt='yuv420p',
preset=self.preset,
qp=self.qp)
)
if self.overwrite:
out = out.overwrite_output()
print(out.compile())
self._encode_process = out.run_async(pipe_stdin=True)
def _init_encode_process(self):
out = (
ffmpeg
.input('pipe:',
format='rawvideo',
pix_fmt='bgr24',
s=f'{self.out_size[0]}x{self.out_size[1]}',
framerate=self.fps)
.output(self.output_file,
preset='ultrafast',
vcodec='libx265',
pix_fmt='yuv420p',
crf=16)
)
if self.overwrite:
out = out.overwrite_output()
print(out.compile())
self._encode_process = out.run_async(pipe_stdin=True)