flandre/src/nodes/ImageFFMPEG.py

70 lines
2.5 KiB
Python
Raw Normal View History

2025-02-20 14:06:23 +08:00
import logging
import subprocess
import time
import numpy as np
import zmq
from BusClient import BusClient
from config import VIDEO_HEIGHT, VIDEO_WIDTH
from nodes.Node import Node
from utils.Msg import BMMsg, SetWindowVisibleMsg, Msg
logger = logging.getLogger(__name__)
class ImageFFMPEG(Node):
2025-03-23 22:33:55 +08:00
topics = [BMMsg]
2025-02-20 14:06:23 +08:00
def __init__(self, level=logging.INFO):
super().__init__(level=level)
self.buffer = np.zeros((VIDEO_HEIGHT, VIDEO_WIDTH, 3), dtype=np.uint8) + 128
self.buffer = self.buffer.tobytes()
def loop(self):
self.c = BusClient(BMMsg, pub=False, conflare=True, poller=True)
p = subprocess.Popen(['ffmpeg',
'-f', 'rawvideo',
'-pixel_format', 'rgb24',
'-video_size', '1080x1920',
2025-02-26 21:41:15 +08:00
'-framerate', '24',
2025-02-20 14:06:23 +08:00
'-hwaccel', 'nvdec',
'-i', '-',
'-vcodec', 'h264_nvenc',
2025-02-26 21:41:15 +08:00
'-preset', 'fast',
'-gpu', '1',
# '-profile:v', 'high',
'-zerolatency', '1',
# '-tune', 'ull',
# '-level', '42',
2025-02-20 14:06:23 +08:00
'-pix_fmt', 'yuv420p',
# '-vcodec', 'libx264',
2025-02-26 21:41:15 +08:00
# '-b:v', '40M',
2025-02-20 14:06:23 +08:00
'-f', 'flv',
2025-02-26 21:41:15 +08:00
'rtmp://q1hyb.as/live/bscan'
2025-02-20 14:06:23 +08:00
# '-f', 'mpegts',
# 'srt://localhost:10080?streamid=#!::r=live/livestream,m=publish'
],
stdin=subprocess.PIPE,
)
2025-02-26 21:41:15 +08:00
lasttime = time.time()
2025-02-20 14:06:23 +08:00
while True:
# socks = dict(self.c.poller.poll(1 / 30))
2025-02-26 21:41:15 +08:00
events = self.c.poller.poll(1000 / 24)
2025-02-20 14:06:23 +08:00
if events:
msg: BMMsg = Msg.decode_msg(events[0][0].recv())
b = np.frombuffer(msg.data, dtype=np.uint8)
b = np.reshape(b, (VIDEO_HEIGHT, VIDEO_WIDTH, 4))[:, :, :3]
self.buffer = b.tobytes()
p.stdin.write(self.buffer)
2025-02-26 21:41:15 +08:00
# time.sleep(1 / 60)
currenttime = time.time()
logger.debug(f'{currenttime - lasttime}')
lasttime = currenttime
2025-02-20 14:06:23 +08:00
if __name__ == '__main__':
ImageFFMPEG()()