commit 5616f65e8ee0916c0174662d647c1264a5f5743c Author: Fierelier Date: Tue Apr 13 20:33:16 2021 +0200 first commit diff --git a/ffmpstream-client.py b/ffmpstream-client.py new file mode 100644 index 0000000..1978c75 --- /dev/null +++ b/ffmpstream-client.py @@ -0,0 +1,19 @@ +import subprocess +import socket +bufferSize = 1000 +serverAddr = ("127.0.0.1",12000) +connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +connection.settimeout(15) +connection.connect(serverAddr) + +proc = subprocess.Popen([ + "ffplay","-f","mpegts", + "-i","-", + "-fflags","nobuffer", + "-flags","low_delay", + "-infbuf","-fast","-framedrop" +],stdin=subprocess.PIPE) + +while True: + data = connection.recv(bufferSize) + proc.stdin.write(data) \ No newline at end of file diff --git a/ffmpstream-server.py b/ffmpstream-server.py new file mode 100644 index 0000000..4a1bba7 --- /dev/null +++ b/ffmpstream-server.py @@ -0,0 +1,80 @@ +import threading +import queue +import socket +import subprocess + +threads = {} +threadId = 0 +threadsLock = threading.Lock() + +serverAddr = ("127.0.0.1",12000) +serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +serverSocket.bind(serverAddr) +serverSocket.listen(1024) + +bufferSize = 1000 # buffer size in bytes + +proc = subprocess.Popen([ + # binary + "ffmpeg", + # input: audio + "-f","dshow","-audio_buffer_size","10","-i","audio=virtual-audio-capturer", + # input: video + "-f","gdigrab","-framerate","30","-i","desktop","-vf","scale=-1:480", + # output-codec: video + "-c:v","libx264","-pix_fmt","yuv420p","-preset","fast","-tune","zerolatency", + # output-codec: audio + "-c:a","aac", + # output properties: + "-bufsize","2M","-maxrate","1M", + # output-file: + "-f","mpegts", + "-" +],stdout=subprocess.PIPE,stdin=subprocess.PIPE) + +class outThread(threading.Thread): + def __init__(self,threadId,connection,address): + threading.Thread.__init__(self) + self.threadId = threadId + self.queue = queue.Queue() + self.connection = connection + self.address = address + self.connection.settimeout(15) + + def run(self): + try: + while True: + data = self.queue.get() + self.connection.sendall(data) + except: + with threadsLock: + del threads[str(self.threadId)] + raise + +class ffmpegThread(threading.Thread): + def __init__(self): + threading.Thread.__init__(self) + + def run(self): + while proc.poll() is None: + data = proc.stdout.read(bufferSize) + with threadsLock: + for thread in threads: + thread = threads[thread] + thread.queue.put(data) + +def main(): + global threadId + ffmpegThread().start() + while True: + connection, address = serverSocket.accept() + with threadsLock: + threadId += 1 + while str(threadId) in threads: + threadId += 1 + + thread = outThread(threadId,connection,address) + threads[str(threadId)] = thread + thread.start() + +main() \ No newline at end of file