我正在尝试使用伪造的网络摄像头文件打开 Chrome,该文件为 .y4m,如下所示:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager as cdm
service = Service(cdm().install())
options = Options()
options.add_argument("--use-fake-device-for-media-stream")
options.add_argument("--use-file-for-fake-video-capture=fake_web_cam.y4m")
driver = webdriver.Chrome(service=service, options=options)
它完全有效,但问题是在某些网站中选择此摄像机时,摄像机名称与 .y4m 文件的路径相同。我搜索了很多但没有找到解决这个问题的方法。
不幸的是这是不可能的。通过查看 Chromium 源代码,您会找到原因。
此链接包含最常见的 Chrome 参数及其在源代码中的引用。如果您单击
--use-file-for-fake-video-capture
,则会打开 media_switches.cc。检查它的引用,你会看到 file_video_capture_device_factory.cc中有一个函数
GetFilePathFromCommandLine
:
base::FilePath GetFilePathFromCommandLine() {
base::FilePath command_line_file_path =
base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
switches::kUseFileForFakeVideoCapture);
return command_line_file_path;
}
通过检查此函数的引用,您可以在同一文件中看到此代码:
devices_info.emplace_back(
VideoCaptureDeviceDescriptor(GetFilePathFromCommandLine().AsUTF8Unsafe(),
kFileVideoCaptureDeviceName, api));
VideoCaptureDeviceDescriptor
在video_capture_device_descriptor.cc中定义如下:
VideoCaptureDeviceDescriptor::VideoCaptureDeviceDescriptor(
const std::string& display_name,
const std::string& device_id,
VideoCaptureApi capture_api,
const VideoCaptureControlSupport& control_support,
VideoCaptureTransportType transport_type)
: device_id(device_id),
facing(VideoFacingMode::MEDIA_VIDEO_FACING_NONE),
capture_api(capture_api),
transport_type(transport_type),
display_name_(TrimDisplayName(display_name)),
control_support_(control_support) {}
因此,文件路径默认为
display_name
,无法更改。
您可以使用其他替代方法从视频文件创建虚拟网络摄像头,例如OBS Studio,SplitCam。