录制.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. # Author : Charley
  3. # Python : 3.10.8
  4. # Date : 2025/8/12 16:01
  5. """
  6. https://www.whatnot.com/live/fed63b2d-6a9d-4be9-a488-5e7a5440814c
  7. """
  8. import cv2
  9. import numpy as np
  10. import pyautogui
  11. import time
  12. from selenium import webdriver
  13. from selenium.webdriver.common.by import By
  14. from selenium.webdriver.chrome.options import Options
  15. from selenium.webdriver.support.ui import WebDriverWait
  16. from selenium.webdriver.support import expected_conditions as EC
  17. from selenium.common.exceptions import TimeoutException
  18. class ScreenRecorder:
  19. def __init__(self, output_file='selenium_test.mp4', fps=60.0):
  20. self.screen_size = pyautogui.size()
  21. self.fps = fps
  22. self.fourcc = cv2.VideoWriter_fourcc(*'XVID')
  23. self.output_file = output_file
  24. self.out = None
  25. def start_recording(self):
  26. self.out = cv2.VideoWriter(self.output_file, self.fourcc, self.fps,
  27. (self.screen_size.width, self.screen_size.height))
  28. print("开始录制...")
  29. def record_for_duration(self, duration):
  30. start_time = time.time()
  31. while time.time() - start_time < duration:
  32. img = pyautogui.screenshot()
  33. frame = np.array(img)
  34. frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  35. self.out.write(frame)
  36. def stop_recording(self):
  37. if self.out:
  38. self.out.release()
  39. cv2.destroyAllWindows()
  40. print("录制完成!")
  41. def main():
  42. # 配置参数
  43. url = 'https://www.whatnot.com/live/fed63b2d-6a9d-4be9-a488-5e7a5440814c'
  44. recording_duration = 10
  45. # 配置Chrome选项
  46. chrome_options = Options()
  47. chrome_options.add_argument("--headless=False") # 显示浏览器窗口
  48. chrome_options.add_argument("--start-maximized") # 最大化窗口
  49. # 初始化录制器
  50. recorder = ScreenRecorder()
  51. # 启动浏览器
  52. driver = webdriver.Chrome(options=chrome_options)
  53. try:
  54. # 开始录制
  55. recorder.start_recording()
  56. time.sleep(2)
  57. # 访问网页
  58. driver.get(url)
  59. wait = WebDriverWait(driver, 10)
  60. # 等待页面加载完成(可根据实际需要调整)
  61. time.sleep(2)
  62. # 这里可以添加对该网站的实际操作
  63. # 注意:原代码中的搜索框定位不适用于该网站
  64. # 录制指定时长
  65. recorder.record_for_duration(recording_duration)
  66. except TimeoutException:
  67. print("页面加载超时")
  68. except Exception as e:
  69. print(f"发生错误: {e}")
  70. finally:
  71. driver.quit()
  72. recorder.stop_recording()
  73. if __name__ == "__main__":
  74. main()