-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_encoder.py
More file actions
1076 lines (874 loc) · 33.4 KB
/
gpu_encoder.py
File metadata and controls
1076 lines (874 loc) · 33.4 KB
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Remote GPU Encoding - Encoder Server
远程 GPU 编码服务器
这是一个独立的编码服务器,需要在 GPU 机器上单独部署。
功能:
- 接收视频帧(通过 ZMQ)
- NVENC 硬件编码(通过 FFmpeg)
- 音视频合并
- 多会话支持
- 批量帧处理
使用:
python gpu_encoder.py --bind tcp://0.0.0.0:5555 --codec h264_nvenc
协议版本: 2.0
"""
import zmq
import subprocess
import sys
import signal
import time
import argparse
import os
import tempfile
from typing import Optional, Dict, Any
from dataclasses import dataclass, field
from datetime import datetime
from enum import IntEnum
import struct
# ============================================================================
# 品牌标识
# ============================================================================
LOGO_PREFIX = "[RemoteGPU]"
LOGO_BANNER = """
╔═══════════════════════════════════════════════════════════════╗
║ ║
║ ██████╗ ███████╗███╗ ███╗ ██████╗ ████████╗███████╗ ║
║ ██╔══██╗██╔════╝████╗ ████║██╔═══██╗╚══██╔══╝██╔════╝ ║
║ ██████╔╝█████╗ ██╔████╔██║██║ ██║ ██║ █████╗ ║
║ ██╔══██╗██╔══╝ ██║╚██╔╝██║██║ ██║ ██║ ██╔══╝ ║
║ ██║ ██║███████╗██║ ╚═╝ ██║╚██████╔╝ ██║ ███████╗ ║
║ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚══════╝ ║
║ ║
║ ██████╗ ██████╗ ██╗ ██╗ ║
║ ██╔════╝ ██╔══██╗██║ ██║ ║
║ ██║ ███╗██████╔╝██║ ██║ ║
║ ██║ ██║██╔═══╝ ██║ ██║ ║
║ ╚██████╔╝██║ ╚██████╔╝ ║
║ ╚═════╝ ╚═╝ ╚═════╝ ║
║ ║
║ ENCODER SERVER NVENC Hardware Encoding ║
║ ║
╚═══════════════════════════════════════════════════════════════╝
"""
# ============================================================================
# 协议常量(与 protocol/protocol.py 保持一致)
# ============================================================================
PROTOCOL_MAGIC = 0x5A4D5646
class MessageType(IntEnum):
SESSION_START = 1
SESSION_END = 2
FRAME_DATA = 3
AUDIO_DATA = 4
HEARTBEAT = 5
BATCH_FRAMES = 7
class AudioFormat(IntEnum):
NONE = 0
PCM_F32LE = 1
PCM_S16LE = 2
class SessionFlags(IntEnum):
NONE = 0
HAS_AUDIO = 1
# ============================================================================
# 颜色和日志(简化版,与 logger/logger.py 类似但更轻量)
# ============================================================================
class Color:
RESET = "\033[0m"
BOLD = "\033[1m"
DIM = "\033[2m"
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
MAGENTA = "\033[95m"
CYAN = "\033[96m"
class Logger:
"""日志类"""
def __init__(self, tag: str):
self.tag = tag
def _ts(self) -> str:
return datetime.now().strftime("%H:%M:%S.%f")[:-3]
def _fmt(self, level: str, color: str, msg: str) -> str:
return (
f"{Color.MAGENTA}{LOGO_PREFIX}{Color.RESET} "
f"{Color.DIM}{self._ts()}{Color.RESET} "
f"{color}[{level:5s}]{Color.RESET} "
f"{Color.BLUE}[{self.tag:<10s}]{Color.RESET} "
f"{msg}"
)
def debug(self, msg: str):
print(self._fmt("DEBUG", Color.DIM, msg))
def info(self, msg: str):
print(self._fmt("INFO", Color.CYAN, msg))
def success(self, msg: str):
print(self._fmt("OK", Color.GREEN, msg))
def warning(self, msg: str):
print(self._fmt("WARN", Color.YELLOW, msg))
def error(self, msg: str):
print(self._fmt("ERROR", Color.RED, msg))
def data(self, msg: str):
print(self._fmt("DATA", Color.MAGENTA, msg))
def progress(self, current: int, total: int, suffix: str = ""):
if total <= 0:
return
pct = current / total
width = 30
filled = int(width * pct)
bar = "█" * filled + "░" * (width - filled)
line = (
f"\r{Color.MAGENTA}{LOGO_PREFIX}{Color.RESET} "
f"{Color.DIM}{self._ts()}{Color.RESET} "
f"{Color.MAGENTA}[RECV ]{Color.RESET} "
f"[{bar}] {pct * 100:5.1f}% ({current}/{total})"
)
if suffix:
line += f" | {suffix}"
sys.stdout.write(line + " ")
sys.stdout.flush()
if current >= total:
print()
def header(self, title: str):
line = "─" * 60
print(
f"\n{Color.MAGENTA}{LOGO_PREFIX}{Color.RESET} {Color.CYAN}{line}{Color.RESET}"
)
print(
f"{Color.MAGENTA}{LOGO_PREFIX}{Color.RESET} {Color.BOLD}{title}{Color.RESET}"
)
print(
f"{Color.MAGENTA}{LOGO_PREFIX}{Color.RESET} {Color.CYAN}{line}{Color.RESET}"
)
def separator(self):
print(
f"{Color.MAGENTA}{LOGO_PREFIX}{Color.RESET} {Color.DIM}{'─' * 60}{Color.RESET}"
)
def kv(self, key: str, value: Any, indent: int = 2):
print(
f"{Color.MAGENTA}{LOGO_PREFIX}{Color.RESET} "
f"{' ' * indent}{Color.DIM}{key}:{Color.RESET}".ljust(35)
+ f" {value}"
)
# ============================================================================
# 数据结构
# ============================================================================
@dataclass
class VideoMessage:
"""视频消息"""
msg_type: MessageType
flags: int = 0
width: int = 0
height: int = 0
channels: int = 3
data_len: int = 0
frame_num: int = 0
timestamp_us: int = 0
session_id: bytes = field(default_factory=lambda: b"\x00" * 16)
total_frames: int = 0
fps: int = 30
output_path: str = ""
data: Optional[bytes] = None
@property
def has_audio(self) -> bool:
return bool(self.flags & SessionFlags.HAS_AUDIO)
@property
def session_hex(self) -> str:
return self.session_id.hex()[:16]
@dataclass
class AudioMessage:
"""音频消息"""
audio_format: AudioFormat = AudioFormat.PCM_F32LE
channels: int = 2
sample_rate: int = 44100
num_samples: int = 0
data_len: int = 0
session_id: bytes = field(default_factory=lambda: b"\x00" * 16)
data: Optional[bytes] = None
@property
def duration(self) -> float:
return self.num_samples / self.sample_rate if self.sample_rate > 0 else 0
@dataclass
class Session:
"""编码会话"""
session_id: str
output_path: str
width: int
height: int
fps: int
total_frames: int
has_audio: bool
start_time: float = field(default_factory=time.time)
frames_received: int = 0
bytes_received: int = 0
audio_data: Optional[bytes] = None
audio_sample_rate: int = 44100
audio_channels: int = 2
audio_format: AudioFormat = AudioFormat.PCM_F32LE
@property
def elapsed(self) -> float:
return time.time() - self.start_time
@property
def fps_actual(self) -> float:
return self.frames_received / self.elapsed if self.elapsed > 0 else 0
@property
def mb_received(self) -> float:
return self.bytes_received / (1024 * 1024)
@property
def throughput_gbps(self) -> float:
return (self.bytes_received * 8) / self.elapsed / 1e9 if self.elapsed > 0 else 0
# ============================================================================
# 消息解析器
# ============================================================================
class MessageParser:
"""消息解析器"""
VIDEO_HEADER_FORMAT = "<I B B B B I I I I Q Q 16s I I 60s 4x"
VIDEO_HEADER_SIZE = 128
AUDIO_HEADER_FORMAT = "<I B B B B I I I 4x Q 16s 16x"
AUDIO_HEADER_SIZE = 64
BATCH_HEADER_FORMAT = "<I B B B B I I I H H I Q 16s I I 60s 4x"
BATCH_HEADER_SIZE = 128
LEGACY_VIDEO_FORMAT = "<BBBB IIII Q Q 16s I I 64s 4x"
LEGACY_VIDEO_SIZE = 128
LEGACY_AUDIO_FORMAT = "<BBBB IIII Q 16s 20x"
LEGACY_AUDIO_SIZE = 64
@classmethod
def parse(cls, data: bytes) -> Optional[VideoMessage | AudioMessage]:
"""解析消息"""
if len(data) < 4:
return None
magic = struct.unpack("<I", data[:4])[0]
if magic == PROTOCOL_MAGIC:
return cls._parse_new(data)
else:
return cls._parse_legacy(data)
@classmethod
def _parse_new(cls, data: bytes) -> Optional[VideoMessage | AudioMessage]:
"""解析新协议"""
if len(data) < 6:
return None
msg_type = MessageType(data[5])
if msg_type == MessageType.AUDIO_DATA:
if len(data) < cls.AUDIO_HEADER_SIZE:
return None
hdr = struct.unpack(cls.AUDIO_HEADER_FORMAT, data[: cls.AUDIO_HEADER_SIZE])
return AudioMessage(
audio_format=AudioFormat(hdr[3]),
channels=hdr[4],
sample_rate=hdr[5],
num_samples=hdr[6],
data_len=hdr[7],
session_id=hdr[9],
data=data[cls.AUDIO_HEADER_SIZE :]
if len(data) > cls.AUDIO_HEADER_SIZE
else None,
)
if msg_type == MessageType.BATCH_FRAMES:
if len(data) < cls.BATCH_HEADER_SIZE:
return None
hdr = struct.unpack(cls.BATCH_HEADER_FORMAT, data[: cls.BATCH_HEADER_SIZE])
return VideoMessage(
msg_type=MessageType.BATCH_FRAMES,
flags=hdr[3],
width=hdr[5],
height=hdr[6],
channels=hdr[7],
data_len=hdr[10],
frame_num=hdr[9],
timestamp_us=hdr[11],
session_id=hdr[12],
total_frames=hdr[13],
fps=hdr[14],
output_path=hdr[15].rstrip(b"\x00").decode("utf-8", errors="ignore"),
data=data[cls.BATCH_HEADER_SIZE :]
if len(data) > cls.BATCH_HEADER_SIZE
else None,
)
if len(data) < cls.VIDEO_HEADER_SIZE:
return None
hdr = struct.unpack(cls.VIDEO_HEADER_FORMAT, data[: cls.VIDEO_HEADER_SIZE])
return VideoMessage(
msg_type=MessageType(hdr[2]),
flags=hdr[3],
width=hdr[5],
height=hdr[6],
channels=hdr[7],
data_len=hdr[8],
frame_num=hdr[9],
timestamp_us=hdr[10],
session_id=hdr[11],
total_frames=hdr[12],
fps=hdr[13],
output_path=hdr[14].rstrip(b"\x00").decode("utf-8", errors="ignore"),
data=data[cls.LEGACY_VIDEO_SIZE :]
if len(data) > cls.LEGACY_VIDEO_SIZE
else None,
)
if len(data) < cls.VIDEO_HEADER_SIZE:
return None
hdr = struct.unpack(cls.VIDEO_HEADER_FORMAT, data[: cls.VIDEO_HEADER_SIZE])
return VideoMessage(
msg_type=MessageType(hdr[2]),
flags=hdr[3],
width=hdr[5],
height=hdr[6],
channels=hdr[7],
data_len=hdr[8],
frame_num=hdr[9],
timestamp_us=hdr[10],
session_id=hdr[11],
total_frames=hdr[12],
fps=hdr[13],
output_path=hdr[14].rstrip(b"\x00").decode("utf-8", errors="ignore"),
data=data[cls.VIDEO_HEADER_SIZE :]
if len(data) > cls.VIDEO_HEADER_SIZE
else None,
)
@classmethod
def _parse_legacy(cls, data: bytes) -> Optional[VideoMessage | AudioMessage]:
"""解析旧协议"""
if len(data) < 4:
return None
msg_type = data[0]
if msg_type == MessageType.AUDIO_DATA and len(data) >= cls.LEGACY_AUDIO_SIZE:
hdr = struct.unpack(cls.LEGACY_AUDIO_FORMAT, data[: cls.LEGACY_AUDIO_SIZE])
return AudioMessage(
audio_format=AudioFormat(hdr[1]),
channels=hdr[2],
sample_rate=hdr[4],
num_samples=hdr[5],
data_len=hdr[6],
session_id=hdr[9],
data=data[cls.LEGACY_AUDIO_SIZE :]
if len(data) > cls.LEGACY_AUDIO_SIZE
else None,
)
if len(data) >= cls.LEGACY_VIDEO_SIZE:
hdr = struct.unpack(cls.LEGACY_VIDEO_FORMAT, data[: cls.LEGACY_VIDEO_SIZE])
return VideoMessage(
msg_type=MessageType(hdr[0]),
flags=hdr[1],
width=hdr[4],
height=hdr[5],
channels=hdr[6],
data_len=hdr[7],
frame_num=hdr[8],
timestamp_us=hdr[9],
session_id=hdr[10],
total_frames=hdr[11],
fps=hdr[12],
output_path=hdr[13].rstrip(b"\x00").decode("utf-8", errors="ignore"),
data=data[cls.LEGACY_VIDEO_SIZE :]
if len(data) > cls.LEGACY_VIDEO_SIZE
else None,
)
return None
# ============================================================================
# 接收器
# ============================================================================
class Receiver:
"""消息接收器"""
def __init__(self, endpoint: str):
self.log = Logger("Receiver")
self.endpoint = endpoint
self.running = True
self.log.info(f"Connecting to: {endpoint}")
self.context = zmq.Context()
self.socket = self.context.socket(zmq.SUB)
self.socket.setsockopt(zmq.RCVHWM, 500)
self.socket.setsockopt(zmq.RCVBUF, 256 * 1024 * 1024)
self.socket.setsockopt(zmq.TCP_KEEPALIVE, 1)
self.socket.setsockopt(zmq.TCP_KEEPALIVE_IDLE, 60)
self.socket.connect(endpoint)
self.socket.setsockopt_string(zmq.SUBSCRIBE, "")
self.log.success(f"Connected: {endpoint}")
def receive(self, timeout_ms: int = 1000) -> Optional[VideoMessage | AudioMessage]:
"""接收消息"""
self.socket.setsockopt(zmq.RCVTIMEO, timeout_ms)
try:
data = self.socket.recv()
except zmq.Again:
return None
except zmq.ZMQError as e:
self.log.error(f"Receive error: {e}")
return None
return MessageParser.parse(data)
def close(self):
"""关闭"""
self.running = False
self.socket.close()
self.context.term()
self.log.info("Connection closed")
# ============================================================================
# FFmpeg 编码器
# ============================================================================
class FFmpegEncoder:
"""FFmpeg 编码器"""
def __init__(
self,
output: str,
width: int,
height: int,
fps: int,
codec: str = "h264_nvenc",
preset: str = "p4",
bitrate: str = "20M",
gpu: int = 0,
):
self.log = Logger("FFmpeg")
self.output = output
self.frame_count = 0
self.start_time = time.time()
self.log.info(f"Init: {width}×{height}@{fps}fps → {output}")
output_dir = os.path.dirname(output)
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir, exist_ok=True)
if "M" in bitrate:
bufsize = f"{int(bitrate.replace('M', '')) * 2}M"
else:
bufsize = "40M"
cmd = [
"ffmpeg",
"-y",
"-hwaccel",
"cuda",
"-hwaccel_device",
str(gpu),
"-f",
"rawvideo",
"-pix_fmt",
"rgb24",
"-s",
f"{width}x{height}",
"-r",
str(fps),
"-i",
"-",
"-c:v",
codec,
"-preset",
preset,
"-b:v",
bitrate,
"-maxrate",
bitrate,
"-bufsize",
bufsize,
"-pix_fmt",
"yuv420p",
"-movflags",
"+faststart",
output,
]
self.process = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
bufsize=width * height * 3 * 10,
)
self.log.success("Encoder ready")
def write(self, frame_data: bytes) -> bool:
"""写入帧"""
try:
self.process.stdin.write(frame_data)
self.frame_count += 1
return True
except Exception as e:
self.log.error(f"Write error: {e}")
return False
def close(self) -> bool:
"""关闭"""
self.log.info("Finalizing video...")
try:
if self.process.stdin:
self.process.stdin.close()
self.process.wait(timeout=120)
if self.process.returncode != 0:
stderr = self.process.stderr.read().decode()[-500:]
self.log.error(f"FFmpeg error:\n{stderr}")
return False
elapsed = time.time() - self.start_time
self.log.success(
f"Encoded {self.frame_count} frames in {elapsed:.2f}s "
f"({self.frame_count / elapsed:.1f} fps)"
)
return True
except subprocess.TimeoutExpired:
self.log.error("FFmpeg timeout")
self.process.kill()
return False
except Exception as e:
self.log.error(f"Close error: {e}")
return False
# ============================================================================
# 音视频合并器
# ============================================================================
class AudioMerger:
"""音视频合并"""
def __init__(self):
self.log = Logger("Merger")
def merge(
self,
video_path: str,
audio_data: bytes,
output_path: str,
sample_rate: int,
channels: int,
audio_format: AudioFormat,
) -> bool:
"""合并"""
self.log.header("Merging Audio + Video")
self.log.kv("Video", video_path)
self.log.kv(
"Audio", f"{len(audio_data) / 1024:.1f}KB, {sample_rate}Hz, {channels}ch"
)
self.log.kv("Output", output_path)
if not os.path.exists(video_path):
self.log.error(f"Video not found: {video_path}")
return False
audio_tmp = tempfile.mktemp(suffix=".raw")
try:
with open(audio_tmp, "wb") as f:
f.write(audio_data)
fmt_map = {
AudioFormat.PCM_F32LE: "f32le",
AudioFormat.PCM_S16LE: "s16le",
}
pcm_fmt = fmt_map.get(audio_format, "f32le")
cmd = [
"ffmpeg",
"-y",
"-i",
video_path,
"-f",
pcm_fmt,
"-ar",
str(sample_rate),
"-ac",
str(channels),
"-i",
audio_tmp,
"-c:v",
"copy",
"-c:a",
"aac",
"-b:a",
"192k",
"-map",
"0:v:0",
"-map",
"1:a:0",
"-shortest",
output_path,
]
self.log.info("Running FFmpeg merge...")
result = subprocess.run(cmd, capture_output=True, timeout=300)
if result.returncode != 0:
self.log.error(f"Merge failed:\n{result.stderr.decode()[-300:]}")
return False
if os.path.exists(output_path):
size_mb = os.path.getsize(output_path) / (1024 * 1024)
self.log.success(f"Merged: {output_path} ({size_mb:.2f}MB)")
return True
return False
except subprocess.TimeoutExpired:
self.log.error("Merge timeout")
return False
except Exception as e:
self.log.error(f"Merge error: {e}")
return False
finally:
if os.path.exists(audio_tmp):
try:
os.remove(audio_tmp)
except:
pass
# ============================================================================
# 主程序
# ============================================================================
def main():
parser = argparse.ArgumentParser(
description="Remote GPU Encoding - Encoder Server",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
%(prog)s --bind tcp://0.0.0.0:5555
%(prog)s --bind tcp://0.0.0.0:5555 --codec hevc_nvenc --bitrate 30M
%(prog)s --bind tcp://0.0.0.0:5555 --output-dir /mnt/videos
""",
)
parser.add_argument(
"--bind",
"-b",
default="tcp://10.10.0.1:5555",
help="Bind address (default: tcp://10.10.0.1:5555)",
)
parser.add_argument(
"--output-dir", "-o", default="", help="Override output directory"
)
parser.add_argument(
"--codec",
"-c",
default="h264_nvenc",
choices=["h264_nvenc", "hevc_nvenc", "av1_nvenc"],
help="Video codec (default: h264_nvenc)",
)
parser.add_argument(
"--preset",
"-p",
default="p4",
choices=["p1", "p2", "p3", "p4", "p5", "p6", "p7"],
help="Encoder preset (p1=fastest, p7=best)",
)
parser.add_argument("--bitrate", default="20M", help="Video bitrate (default: 20M)")
parser.add_argument(
"--gpu", type=int, default=0, help="GPU device index (default: 0)"
)
parser.add_argument(
"--single-session", action="store_true", help="Exit after first session"
)
parser.add_argument(
"--idle-timeout",
type=int,
default=0,
help="Exit after N seconds idle (0=disabled)",
)
args = parser.parse_args()
log = Logger("Main")
# 显示横幅
print(f"{Color.CYAN}{LOGO_BANNER}{Color.RESET}")
# 配置
log.header("Configuration")
log.kv("Bind Address", args.bind)
log.kv("Output Dir", args.output_dir or "(from sender)")
log.kv("Codec", args.codec)
log.kv("Preset", args.preset)
log.kv("Bitrate", args.bitrate)
log.kv("GPU", args.gpu)
log.kv("Single Session", args.single_session)
log.kv(
"Idle Timeout", f"{args.idle_timeout}s" if args.idle_timeout > 0 else "disabled"
)
log.separator()
# 创建组件
receiver = Receiver(args.bind)
encoder: Optional[FFmpegEncoder] = None
session: Optional[Session] = None
merger = AudioMerger()
total_sessions = 0
idle_start = time.time()
# 信号处理
shutdown = False
def signal_handler(sig, frame):
nonlocal shutdown
if shutdown:
sys.exit(1)
shutdown = True
log.warning("Shutdown requested...")
receiver.running = False
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
log.header("Waiting for Sessions")
log.info("Press Ctrl+C to stop")
try:
while receiver.running:
msg = receiver.receive(timeout_ms=1000)
if msg is None:
idle_time = time.time() - idle_start
if (
not session
and args.idle_timeout > 0
and idle_time > args.idle_timeout
):
log.info(f"Idle timeout ({args.idle_timeout}s)")
break
if not session and int(idle_time) % 30 == 0 and int(idle_time) > 0:
log.info(f"Idle: {idle_time:.0f}s | Sessions: {total_sessions}")
continue
idle_start = time.time()
# ===== 音频 =====
if isinstance(msg, AudioMessage):
if session and msg.data:
session.audio_data = msg.data
session.audio_sample_rate = msg.sample_rate
session.audio_channels = msg.channels
session.audio_format = msg.audio_format
log.data(
f"Audio: {len(msg.data) / 1024:.1f}KB, "
f"{msg.sample_rate}Hz, {msg.channels}ch, "
f"{msg.duration:.2f}s"
)
continue
# ===== 视频 =====
if not isinstance(msg, VideoMessage):
continue
# ----- SESSION_START -----
if msg.msg_type == MessageType.SESSION_START:
if session and encoder:
encoder.close()
encoder = None
if args.output_dir:
filename = (
os.path.basename(msg.output_path)
or f"video_{msg.session_hex}.mp4"
)
output_path = os.path.join(args.output_dir, filename)
else:
output_path = msg.output_path or f"/tmp/video_{msg.session_hex}.mp4"
log.header("Session Start")
log.kv("Session", msg.session_hex)
log.kv("Resolution", f"{msg.width}×{msg.height}")
log.kv("Frames", msg.total_frames)
log.kv("FPS", msg.fps)
log.kv("Audio", msg.has_audio)
log.kv("Output", output_path)
log.separator()
session = Session(
session_id=msg.session_hex,
output_path=output_path,
width=msg.width,
height=msg.height,
fps=msg.fps if msg.fps > 0 else 30,
total_frames=msg.total_frames,
has_audio=msg.has_audio,
)
log.info("Waiting for frames...")
continue
# ----- SESSION_END -----
if msg.msg_type == MessageType.SESSION_END:
print()
log.header("Session End")
if not session:
continue
log.kv("Frames", session.frames_received)
log.kv("Data", f"{session.mb_received:.2f} MB")
log.kv("Time", f"{session.elapsed:.2f}s")
log.kv("Speed", f"{session.fps_actual:.2f} fps")
log.kv("Bandwidth", f"{session.throughput_gbps:.3f} Gbps")
log.kv("Audio", session.has_audio)
if session.audio_data:
log.kv("Audio Size", f"{len(session.audio_data) / 1024:.1f} KB")
log.separator()
video_ok = False
if encoder:
video_ok = encoder.close()
encoder = None
final_output = session.output_path
if video_ok and session.audio_data:
log.info("Merging audio...")
video_only = session.output_path + ".video_only.mp4"
try:
if os.path.exists(session.output_path):
os.rename(session.output_path, video_only)
merge_ok = merger.merge(
video_path=video_only,
audio_data=session.audio_data,
output_path=final_output,
sample_rate=session.audio_sample_rate,
channels=session.audio_channels,
audio_format=session.audio_format,
)
if merge_ok and os.path.exists(video_only):
os.remove(video_only)
elif not merge_ok:
log.warning("Merge failed, keeping video-only")
if os.path.exists(video_only):
os.rename(video_only, final_output)
except Exception as e:
log.error(f"Merge error: {e}")
elif video_ok:
log.info("No audio, video-only output")
if os.path.exists(final_output):
size_mb = os.path.getsize(final_output) / (1024 * 1024)
log.separator()
log.success(f"OUTPUT: {final_output}")
log.kv("Size", f"{size_mb:.2f} MB")
log.kv("Audio", "Yes" if session.audio_data else "No")
session = None
total_sessions += 1
log.success(f"Session complete! Total: {total_sessions}")
if args.single_session:
break
log.info("Waiting for next session...")
continue
# ----- BATCH_FRAMES -----
if msg.msg_type == MessageType.BATCH_FRAMES:
if not session or not msg.data:
continue
if encoder is None:
encoder = FFmpegEncoder(
output=session.output_path,
width=session.width,
height=session.height,
fps=session.fps,
codec=args.codec,
preset=args.preset,
bitrate=args.bitrate,
gpu=args.gpu,
)
log.separator()
frame_size = msg.width * msg.height * 3
batch_size = len(msg.data) // frame_size
batch_success = True
for i in range(batch_size):
offset = i * frame_size