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

如何从网络摄像头视频流采集视频至文件,用于图像分类项目?

Capturing Webcam Video Streams from Public Sites for Image Classification

Hey there! Let's break down how to reliably capture video streams from public webcam sites like the one you mentioned, and save them to local files for your image classification project. I've dealt with similar use cases before, so here's what works:

First, Understand the Stream Type

Most public webcam platforms (including the one you linked) use HLS (HTTP Live Streaming) or dynamic video URLs that aren't directly visible in the page source. Your initial code might have used the page URL instead of the actual stream endpoint—so first we need to extract the real stream URL.

Method 1: Use yt-dlp (Most Reliable for HLS Streams)

yt-dlp is a powerful tool that can automatically parse and download streams from hundreds of websites, including many webcam platforms. It handles dynamic URLs and HLS out of the box, which saves you a ton of parsing work.

Step 1: Install Dependencies

pip install yt-dlp

Step 2: Capture and Save the Stream

Here's a Python script that uses yt-dlp to save the stream to a local MP4 file. You can adjust the duration or output format as needed:

import yt_dlp

def capture_webcam_stream(webcam_url, output_file="webcam_stream.mp4", duration=300):
    # yt-dlp options: save stream, limit duration, format to MP4
    ydl_opts = {
        'format': 'best[ext=mp4]',
        'outtmpl': output_file,
        'quiet': False,
        'postprocessors': [{
            'key': 'FFmpegVideoConvertor',
            'preferedformat': 'mp4',
        }],
        # Limit capture duration (in seconds) – remove this if you want continuous capture
        'postprocessor_args': [
            '-t', str(duration)
        ]
    }

    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([webcam_url])

# Replace with your target webcam page URL
webcam_url = "https://www.skylinewebcams.com/en/webcam/espana/comunidad-valenciana/alicante/benidorm-playa-poniente.html"
capture_webcam_stream(webcam_url, output_file="benidorm_beach.mp4", duration=600)  # Capture 10 minutes

Method 2: Manual Parsing + OpenCV (For More Control)

If you want more control over frame-by-frame processing (useful for image classification, since you might want to extract individual frames), you can parse the page to get the HLS URL, then use OpenCV to read and save the stream.

Step 1: Install Dependencies

pip install requests beautifulsoup4 opencv-python

Step 2: Parse the Page for the HLS Stream URL

import requests
from bs4 import BeautifulSoup

def get_hls_stream_url(webcam_url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
    }
    response = requests.get(webcam_url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # Look for the video tag or script containing the HLS URL
    # For SkylineWebcams, the stream URL is often in a script tag with "hlsUrl"
    for script in soup.find_all('script'):
        if 'hlsUrl' in script.text:
            # Extract the URL – adjust this regex based on the page's structure
            import re
            hls_url_match = re.search(r'hlsUrl:\s*"([^"]+)"', script.text)
            if hls_url_match:
                return hls_url_match.group(1)
    return None

Step 3: Capture Stream with OpenCV

import cv2

def save_stream_to_file(hls_url, output_file="webcam_frames.mp4", fps=20):
    # Open the HLS stream
    cap = cv2.VideoCapture(hls_url)
    
    if not cap.isOpened():
        print("Error: Could not open stream")
        return
    
    # Get stream width and height
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    
    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter(output_file, fourcc, fps, (width, height))
    
    print("Capturing stream... Press 'q' to stop")
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        
        # Write the frame to the output file
        out.write(frame)
        
        # Optional: Display the stream (remove if running headless)
        cv2.imshow('Webcam Stream', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    # Release resources
    cap.release()
    out.release()
    cv2.destroyAllWindows()

# Put it all together
webcam_url = "https://www.skylinewebcams.com/en/webcam/espana/comunidad-valenciana/alicante/benidorm-playa-poniente.html"
hls_url = get_hls_stream_url(webcam_url)
if hls_url:
    save_stream_to_file(hls_url, output_file="benidorm_beach_frames.mp4")
else:
    print("Error: Could not find HLS stream URL")

Important Notes

  • Compliance: Always check the website's Terms of Service and robots.txt before scraping. Many public webcam sites allow non-commercial use, but avoid overloading their servers (limit capture duration or add delays if needed).
  • Dynamic URLs: Some sites change their stream URLs periodically, so you might need to re-run the parsing function if the stream stops working.
  • Frame Extraction: For image classification, you can modify the OpenCV script to save individual frames (e.g., every 10th frame) instead of a full video—just add a counter and use cv2.imwrite() when the counter hits your interval.

内容的提问来源于stack exchange,提问作者Andrey Kite Gorin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:33:36