如何在Zalenium中同时捕获音频与视频?现有命令仅支持视频捕获
Great question! Zalenium builds on top of docker-selenium, which only handles video recording out of the box. To capture both audio and video, you'll need to extend the selenium node containers and tweak the recording command. Let's walk through a working solution step by step:
1. Understand the Base Setup
Zalenium uses ffmpeg inside selenium node containers to record screen video via the x11grab input. By default, there's no audio capture configured because the base docker-selenium images don't include audio tools or virtual audio devices. We'll fix that by adding audio utilities and modifying the ffmpeg command to include audio input.
2. Build a Custom Selenium Node Image
Create a custom Dockerfile that extends the official docker-selenium image and adds audio capture dependencies:
# Use the official Chrome selenium node as base FROM selenium/standalone-chrome:latest USER root # Install audio capture tools (pulseaudio for virtual audio, alsa-utils for device setup) RUN apt-get update && \ apt-get install -y --no-install-recommends pulseaudio alsa-utils && \ rm -rf /var/lib/apt/lists/* # Configure pulseaudio to run in headless mode without a desktop session RUN mkdir -p /var/run/pulse && \ chown selenium:selenium /var/run/pulse && \ echo "default-server = unix:/var/run/pulse/native" > /etc/pulse/client.conf && \ echo "autospawn = no" >> /etc/pulse/client.conf && \ echo "daemonize = yes" >> /etc/pulse/client.conf USER selenium # Start pulseaudio before launching the selenium entry point CMD ["sh", "-c", "pulseaudio --start && /opt/bin/entry_point.sh"]
Build the custom image with:
docker build -t custom-selenium-chrome .
Note: For Firefox, replace selenium/standalone-chrome:latest with selenium/standalone-firefox:latest in the Dockerfile.
3. Launch Zalenium with Audio+Video Capture
When starting Zalenium, specify your custom image and override the video recording command to include audio capture via ffmpeg:
docker run --rm -ti --name zalenium -p 4444:4444 \ -v /var/run/docker.sock:/var/run/docker.sock \ -v /tmp/videos:/home/seluser/videos \ dosel/zalenium start \ --dockerContainersImage custom-selenium-chrome \ --videoRecordingCommand "ffmpeg -y -f x11grab -video_size 1920x1080 -i :99.0 -f pulse -i default -c:v libx264 -preset ultrafast -c:a aac -strict experimental -tune zerolatency /home/seluser/videos/%s.mp4"
Let's break down the modified ffmpeg command:
-f pulse -i default: Captures audio from the default pulseaudio device in the container-c:a aac: Encodes audio using the AAC codec (compatible with MP4 containers)- The rest of the parameters maintain the original video recording behavior (x11grab for screen, libx264 codec)
4. Key Notes to Keep in Mind
- Headless Environment Compatibility: This setup works in headless environments (like CI pipelines) because we're using a virtual audio device. No physical audio hardware is required.
- Test Audio Playback: To verify audio capture, add a step in your test that plays audio (e.g., a video element with sound, or a JavaScript audio player).
- Resource Usage: Capturing both audio and video increases CPU/memory usage in the selenium nodes. Adjust container resources (via Zalenium's
--dockerContainerRamor--dockerContainerCpuflags) if needed. - Firefox Adjustments: For Firefox, you may need to enable media playback in headless mode by adding Firefox preferences like
media.autoplay.default=0to your test setup.
内容的提问来源于stack exchange,提问作者Muhammad Younus

