Skip to content

Fix download_video KeyError, eliminate CDP stutter, and fix 0-byte WebM bug#28

Open
MuhammadSamarShehzad wants to merge 1 commit into
omkarcloud:masterfrom
MuhammadSamarShehzad:master
Open

Fix download_video KeyError, eliminate CDP stutter, and fix 0-byte WebM bug#28
MuhammadSamarShehzad wants to merge 1 commit into
omkarcloud:masterfrom
MuhammadSamarShehzad:master

Conversation

@MuhammadSamarShehzad
Copy link
Copy Markdown

This PR resolves several critical bugs and significantly improves the performance and reliability of the Element.download_video() method in botasaurus_driver.

1. Fixed Python .format() KeyError crash

Issue: The raw JavaScript payload string contained single curly braces {} around the JS functions. When Python called .format(filename=..., duration=...) on it, it attempted to parse the JS braces as python format variables, throwing a KeyError.
Fix: Escaped all JavaScript syntax braces to double braces {{ and }}, leaving only {filename} and {duration} for the Python formatter.

2. Eliminated 3-Second Playback Stutter (CDP Latency)

Issue: Previously, download_video() paused the video, injected the MediaRecorder setup, and resumed the video using three separate, sequential Python self.apply() CDP calls. The network round-trips caused the user's browser video to visibly stutter and freeze for 2–3 seconds.
Fix: Consolidated the logic into a single JS execution block. The vid.pause() and vid.play() are now executed entirely client-side. This makes the capture transition instantaneous and completely invisible to the user.

3. Fixed Chrome captureStream() 0-Byte Empty Frame Bug

Issue: It is a known Chrome bug that calling captureStream() on an HTML5 video that is actively playing can result in a dead stream with zero frames (resulting in 0-byte corrupted downloads).
Fix: Added a synchronous bypass inside the JS payload: vid.pause() -> vid.captureStream() -> vid.play(). By doing this synchronously in Javascript, we bypass the Chrome bug and guarantee frame capture without interrupting playback.

4. Added Native MP4 Container Support

Issue: The previous code passed invalid MediaRecorder options ({audio:true, video:true}) and forced a .mp4 file extension on a generic octet/stream blob. In Chrome, this defaults to a WebM container, which causes "Video Error" playback failures on macOS QuickTime when mislabeled as an MP4.
Fix: Removed the invalid options and added dynamic MediaRecorder.isTypeSupported('video/mp4') detection. Chrome M107+ natively supports MP4 encoding; if available, it will now encode a true MP4 container. Otherwise, it safely falls back to standard WebM.

Testing:

  • Verified that botasaurus no longer crashes with a KeyError during download_video.
  • Tested the video capture to confirm the 3-second UI freeze is completely gone.
  • Confirmed output files are no longer 0-bytes and play natively on Mac QuickTime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant