You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同局域网下基于Python实现PC摄像头跨设备推流方案咨询

Non-Socket Approaches to Stream Webcam Feed Over Local Network with Python

Hey there! Great question—sending raw RGB frame data over raw sockets is super inefficient, so it’s totally smart to explore lighter, more practical alternatives. Here are some solid non-socket (or socket-abstraction) methods to get your webcam stream from one PC to another on the same LAN:

1. HTTP Streaming with Flask (Browser-Friendly & Simple)

Instead of dealing with raw sockets, you can build a lightweight HTTP server that serves encoded video frames. Compressing frames to JPEG/PNG first cuts down data size drastically, making this approach way more efficient.

Server Side (Sender PC)

Use Flask to create an endpoint that streams encoded frames:

from flask import Flask, Response
import cv2

app = Flask(__name__)
camera = cv2.VideoCapture(0)  # 0 for default webcam

def generate_frames():
    while True:
        success, frame = camera.read()
        if not success:
            break
        # Encode frame to JPEG (far smaller than raw RGB)
        ret, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 70])
        frame_bytes = buffer.tobytes()
        # Yield frame in HTTP multipart format
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=False)

Client Side (Receiver PC)

You have two easy options:

  • Open a browser and navigate to http://<sender-ip>:5000/video_feed
  • Or use Python to fetch and display the stream programmatically:
import cv2
import requests
from io import BytesIO
from PIL import Image
import numpy as np

sender_ip = "192.168.1.100"  # Replace with sender's LAN IP
url = f"http://{sender_ip}:5000/video_feed"

while True:
    response = requests.get(url, stream=True)
    bytes_data = b''
    for chunk in response.iter_content(chunk_size=1024):
        bytes_data += chunk
        a = bytes_data.find(b'\r\n\r\n')
        b = bytes_data.find(b'\r\n--frame')
        if a != -1 and b != -1:
            jpg_data = bytes_data[a+4:b]
            bytes_data = bytes_data[b+9:]
            frame = Image.open(BytesIO(jpg_data))
            cv2.imshow('Received Stream', cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR))
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
cv2.destroyAllWindows()

Pros: No special client software required (browser works), easy to set up, low overhead from JPEG compression.

2. RTSP Streaming (Low-Latency for Real-Time Use)

RTSP is designed specifically for real-time video streaming. You’ll need a local RTSP server (like rtsp-simple-server, a lightweight single-binary option) to act as a middleman, then push frames from your Python script to it, and have the receiver pull the stream.

Step 1: Set up a local RTSP server

Download and run a lightweight RTSP server on your LAN (rtsp-simple-server is free and requires minimal configuration).

Step 2: Push Stream from Sender PC

Use OpenCV to encode and push frames to the RTSP server:

import cv2

camera = cv2.VideoCapture(0)
# Define codec and RTSP output path
fourcc = cv2.VideoWriter_fourcc(*'H264')
out = cv2.VideoWriter('rtsp://<rtsp-server-ip>:8554/webcam_stream', fourcc, 20.0, (640, 480))

while True:
    success, frame = camera.read()
    if success:
        out.write(frame)
        cv2.imshow('Sending Stream', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

camera.release()
out.release()
cv2.destroyAllWindows()

Step 3: Receive Stream on Client PC

Use OpenCV to pull and display the RTSP stream:

import cv2

cap = cv2.VideoCapture('rtsp://<rtsp-server-ip>:8554/webcam_stream')

while True:
    success, frame = cap.read()
    if success:
        cv2.imshow('Received RTSP Stream', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Pros: Ultra-low latency (perfect for real-time use), standardized protocol, supports multiple simultaneous clients.

3. MQTT with Message Broker (Lightweight & Scalable)

MQTT is a lightweight publish-subscribe protocol ideal for LAN devices. Set up a local MQTT broker (like Eclipse Mosquitto), then have the sender publish encoded frames to a topic, and the receiver subscribe to that topic to get the stream.

Server Side (Sender)

import cv2
import paho.mqtt.client as mqtt
import base64

broker_ip = "192.168.1.101"  # Replace with your MQTT broker IP
topic = "webcam/stream"

client = mqtt.Client()
client.connect(broker_ip, 1883, 60)

camera = cv2.VideoCapture(0)

while True:
    success, frame = camera.read()
    if success:
        ret, buffer = cv2.imencode('.jpg', frame, [cv2.IMWRITE_JPEG_QUALITY, 60])
        frame_base64 = base64.b64encode(buffer).decode('utf-8')
        client.publish(topic, frame_base64)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

camera.release()
client.disconnect()
cv2.destroyAllWindows()

Client Side (Receiver)

import cv2
import paho.mqtt.client as mqtt
import base64
import numpy as np

broker_ip = "192.168.1.101"
topic = "webcam/stream"

def on_message(client, userdata, msg):
    frame_base64 = msg.payload.decode('utf-8')
    frame_bytes = base64.b64decode(frame_base64)
    np_arr = np.frombuffer(frame_bytes, np.uint8)
    frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
    cv2.imshow('MQTT Stream', frame)
    cv2.waitKey(1)

client = mqtt.Client()
client.connect(broker_ip, 1883, 60)
client.subscribe(topic)
client.on_message = on_message

client.loop_forever()
cv2.destroyAllWindows()

Pros: Extremely lightweight, supports multiple subscribers, easy to integrate with other IoT devices if needed.

4. Shared Network Folder (Low-Effort, Lower Real-Time Performance)

If real-time latency isn’t a top priority, you can save encoded frames to a network-shared folder (via SMB/NFS) on the sender, and have the client poll the folder for new frames.

Server Side (Sender)

import cv2
import os
import time

# Replace with your shared folder path (SMB for Windows, mounted NFS for Linux)
shared_folder = "\\\\192.168.1.100\\WebcamStream"
camera = cv2.VideoCapture(0)
frame_count = 0

while True:
    success, frame = camera.read()
    if success:
        frame_path = os.path.join(shared_folder, f"frame_{frame_count}.jpg")
        cv2.imwrite(frame_path, frame, [cv2.IMWRITE_JPEG_QUALITY, 70])
        # Delete old frames to save space
        if frame_count > 10:
            old_frame_path = os.path.join(shared_folder, f"frame_{frame_count-10}.jpg")
            if os.path.exists(old_frame_path):
                os.remove(old_frame_path)
        frame_count += 1
        time.sleep(0.05)  # Adjust to match desired frame rate
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

camera.release()
cv2.destroyAllWindows()

Client Side (Receiver)

import cv2
import os
import time

shared_folder = "\\\\192.168.1.100\\WebcamStream"
last_frame = -1

while True:
    frame_files = sorted([f for f in os.listdir(shared_folder) if f.startswith("frame_")])
    if frame_files:
        latest_frame = frame_files[-1]
        current_frame_num = int(latest_frame.split("_")[1].split(".")[0])
        if current_frame_num > last_frame:
            frame_path = os.path.join(shared_folder, latest_frame)
            frame = cv2.imread(frame_path)
            cv2.imshow('Shared Folder Stream', frame)
            last_frame = current_frame_num
    cv2.waitKey(1)
    time.sleep(0.05)

cv2.destroyAllWindows()

Pros: No network programming needed—just use OS-level file sharing. Cons: Higher latency, performance depends on file system speed.


内容的提问来源于stack exchange,提问作者KaramJaber

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:59:40