Skip to content

ChenZhu-Xie/youtube-livechat-sync

Repository files navigation

YouTube Live Chat Sync

Automated YouTube Live Chat URL management and πŸ”„ cross-device stream link sharing for OBS Studio.

2025-09-28.13-18-19_x264.mp4
  1. Usage:
  2. My everyday Dual-Channel live-streaming setup:
  3. Other wonderful OBS stuff:
  4. Products that functions similarly to this one:

Table of Contents

Features

  • πŸš€ Automatic Chat URL Detection
    • Primary HTML parsing method with API fallback for reliable video ID detection
  • πŸ”— Cross-Device Stream Sharing
    • Share live stream links between multiple computers automatically (with verysync)
  • ⚑ Real-time Monitoring
    • Continuous browser source URL monitoring and correction with periodic video ID updates
  • πŸ›‘οΈ Enhanced API Protection
    • Advanced quota management with exponential backoff, request rate limiting, and consecutive failure tracking
  • πŸ”„ Auto Browser Refresh
    • Automatic browser source cache refresh every 4 seconds to prevent chat display issues
  • πŸ“ JSON Logging
    • Structured logging system for stream data synchronization with timestamped entries
  • 🎯 Smart Channel Input
    • Supports channel IDs, handles (@username), and full YouTube URLs with intelligent normalization
  • βš™οΈ Thread-Safe Operations
    • Background processing with thread locks for reliable multi-timer coordination
  • πŸ”§ Pending Video ID System
    • Smooth video ID transitions with background updates to prevent chat interruption

How It Works

  1. Stream Detection β†’ Automatically detects stream start/stop events and initializes chat monitoring
  2. Video ID Fetching β†’ Uses HTML parsing (primary) and YouTube API (fallback) to find live video IDs
  3. URL Management β†’ Updates OBS browser source with correct YouTube Studio chat popout URLs
  4. Cross-Device Sync β†’ Writes/reads JSON log files for automatic stream link sharing between computers
  5. Real-time Updates β†’ Periodic video ID updates and browser source refresh to maintain chat connectivity
  6. Smart Retry Logic β†’ Exponential backoff with failure tracking for robust error recovery
  7. Background Processing β†’ Thread-safe background video ID updates with pending system for seamless transitions

Installation

  1. Download the script file youtube_livechat_sync.py
  2. In OBS Studio, go to Tools β†’ Scripts
  3. Click the + button and select the downloaded script
  4. Configure the required settings (see Configuration section)

Configuration

Required Settings

Setting Description Example
YouTube API Key YouTube Data API v3 key (Optional - HTML parsing works without it) AIza...
Channel Handle/ID/URL YouTube channel identifier (multiple formats supported) @username or UC... or full URL
Browser Source Name Name of your OBS browser source for chat display YouTube Chat
Computer Identifier Unique name for this computer in multi-PC setups StreamPC1
Write Log File Path Directory or file path where this PC writes its stream logs C:\StreamLogs\PC1.jsonl
Read Log Directory Path Directory path to read other PC's stream logs \\OtherPC\SharedLogs\PC2.jsonl
image image

Optional Settings

Setting Default Description
Base Init Interval (sec) 1 Initial retry interval for stream detection
Monitor & Refresh Interval (sec) 4 Frequency of URL monitoring and browser refresh
Maximum Init Attempts 3 Maximum initialization retry attempts
Max Init Retry Interval (sec) 30 Maximum retry interval with exponential backoff
Video ID Update Interval (sec) 30 Frequency of background video ID updates

YouTube API Setup

Note: API key is now optional - the script primarily uses HTML parsing which doesn't require API quota.

For API fallback functionality:

  1. Go to Google Cloud Console
  2. Create a new project or select existing one
  3. Enable YouTube Data API v3
  4. Create credentials (API Key)
  5. Copy the API key to the script configuration

Multi-PC Setup

For cross-device streaming setups:

  1. PC 1 (Main): Set Write Log File Path to a shared folder
  2. PC 2 (Secondary): Set Read Log Directory Path to the same shared folder
  3. Both PCs will automatically sync stream links through JSON log files

Example File Structure

\SharedFolder\StreamLogs\
β”œβ”€β”€ MainPC.jsonl      # Written by main streaming PC
└── SecondaryPC.jsonl # Written by secondary PC
image

API Quota Management

Enhanced quota protection system:

  • Primary HTML Parsing: Reduces API dependency by 90%+ with intelligent page parsing
  • Request Rate Limiting: Thread-safe request spacing with minimum 2-second intervals
  • Exponential Backoff: Dynamic interval calculation (1.5x multiplier) up to 30s maximum
  • Failure Tracking: Consecutive failure counting for intelligent retry logic
  • Quota Monitoring: Real-time API usage tracking and logging with detailed statistics

Log Format

Stream data is logged in JSON Lines format:

{
  "timestamp": "2024-01-15T14:30:00.123456",
  "videoId": "dQw4w9WgXcQ",
  "shareLink": "https://youtube.com/live/dQw4w9WgXcQ?feature=share",
  "popoutChatUrl": "https://studio.youtube.com/live_chat?is_popout=1&v=dQw4w9WgXcQ",
  "sourceComputer": "StreamPC1"
}
image

Troubleshooting

Debug Tips

  1. Monitor OBS Script Log: Check timestamped log entries for detailed operation status
  2. Verify Channel Input: Test different channel input formats (@handle, channel ID, full URL)
  3. Check File Permissions: Ensure read/write access to log directories
  4. Network Connectivity: Verify network access between PCs for cross-device sync
  5. Browser Source Settings: Confirm browser source URL updates are working correctly
  6. API Usage Tracking: Monitor quota usage in logs to optimize API calls
image

Contributing

Contributions welcome! Please feel free to submit issues, feature requests, or pull requests. Focus areas:

  • Currently, the initial acquisition of HTML methods is a bit slow, but the API methods are fast.
  • When just clicked on the live broadcast, the script and the push stream were started at the same time, which may be a bit stuck. (This problem should no longer exist now.)
  • Long-term polling HTML method update id may over-consuming upload bandwidth? (But it's not a matter for live broadcasters?)
  • More stable timed polling: Does the logic of HTML request timeout need to set the maximum time limit?
  • Real multithreaded multi-timer in non-python?

License

GPLv3.0 License - feel free to use and modify for your streaming needs.

Disclaimer

  • This tool uses HTML parsing as primary method and YouTube Data API v3 as fallback.
  • Please be mindful of your API quota limits and YouTube's Terms of Service.
  • The HTML parsing method may be affected by YouTube page structure changes.

⭐ If this script helps your streaming setup, please give it a star!

About

Automated YouTube Live Chat URL management and cross-device stream link sharing for OBS Studio. πŸ”„ Syncs chat URLs between multiple streaming setups with quota protection.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages