JSON转TXT工具需求:FIBA LiveStats数据对接OBS Studio实时更新
Got it, let's solve this—you need to pull specific player stats from FIBA LiveStats' JSON output and turn them into a plain text file that OBS can use for real-time updates. Here are two reliable methods, depending on your platform and preference:
This script will watch your FIBA LiveStats JSON file for changes, automatically extract the target player's stats, and update a text file OBS can read. It’s perfect for continuous live updates.
Step-by-Step Setup:
- First, install the required dependency to monitor file changes:
pip install watchdog - Copy this script into a file (e.g.,
stats_updater.py) and tweak the configuration values to match your file paths and target player:import json import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler # --- CONFIGURE THESE VALUES --- JSON_FILE_PATH = "/path/to/your/fiba_livestats_data.json" # Replace with your JSON file path OUTPUT_TXT_PATH = "obs_player_stats.txt" # Output file OBS will read TARGET_PLAYER = "T. Maihi" # Player name to extract stats for # ------------------------------ class StatsUpdater(FileSystemEventHandler): def on_modified(self, event): # Only react when the target JSON file is updated if event.src_path == JSON_FILE_PATH: try: with open(JSON_FILE_PATH, 'r') as f: data = json.load(f) # Locate the target player in the JSON data # Adjust this loop if your JSON structure differs (e.g., different top-level keys) for player in data.get('players', []): if player.get('name') == TARGET_PLAYER: comp_stats = player.get('comp', {}) # Customize the text format to fit your OBS layout output_text = ( f"Player: {TARGET_PLAYER}\n" f"Avg Minutes: {comp_stats.get('sMinutesAverage', 'N/A')}\n" f"Avg Points: {comp_stats.get('sPointsAverage', 'N/A')}\n" f"Avg Rebounds: {comp_stats.get('sReboundsTotalAverage', 'N/A')}\n" f"Avg Assists: {comp_stats.get('sAssistsAverage', 'N/A')}" ) # Write to the text file with open(OUTPUT_TXT_PATH, 'w') as f: f.write(output_text) print(f"Updated stats for {TARGET_PLAYER}") break except Exception as e: print(f"Error updating stats: {str(e)}") if __name__ == "__main__": event_handler = StatsUpdater() observer = Observer() # Monitor the directory containing your JSON file observer.schedule(event_handler, path='./', recursive=False) observer.start() print(f"Monitoring {JSON_FILE_PATH} for updates...") try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() - Run the script:
python stats_updater.py - In OBS, add a Text (GDI+) source, select
obs_player_stats.txtas the file, and check the box for "Read file contents continuously".
If you prefer a Windows-native solution without installing Python, this PowerShell script will do the same job.
Step-by-Step Setup:
- Copy this into a file (e.g.,
Update-PlayerStats.ps1) and adjust the config paths:# --- CONFIGURE THESE VALUES --- $jsonPath = "C:\Path\To\Your\fiba_livestats_data.json" $txtPath = "C:\obs_player_stats.txt" $targetPlayer = "T. Maihi" # ------------------------------ # Loop to check for JSON updates every second while ($true) { try { # Load and parse the JSON file $data = Get-Content $jsonPath -Raw | ConvertFrom-Json # Find the target player $player = $data.players | Where-Object { $_.name -eq $targetPlayer } if ($player) { # Build the output text (customize this format as needed) $output = @"
Player: $targetPlayer
Avg Minutes: $($player.comp.sMinutesAverage)
Avg Points: $($player.comp.sPointsAverage)
Avg Rebounds: $($player.comp.sReboundsTotalAverage)
Avg Assists: $($player.comp.sAssistsAverage)
"@
# Write to the text file
Set-Content $txtPath $output
Write-Host "Updated stats for $targetPlayer"
}
}
catch {
Write-Host "Error updating stats: $_"
}
# Wait 1 second before checking again
Start-Sleep -Seconds 1
}
2. Right-click the script and select *"Run with PowerShell"*. 3. Set up the OBS text source the same way as in Solution 1. --- ### Key Notes: - **JSON Structure Adjustment**: If your FIBA LiveStats JSON uses a different structure (e.g., the player list is under a different key like `roster` instead of `players`), tweak the player lookup part of the script to match. - **Customize Output Format**: Modify the `output_text` (Python) or `$output` (PowerShell) blocks to match how you want stats displayed in OBS—e.g., single line, different order, or additional fields. - **FIBA LiveStats Export**: Ensure FIBA LiveStats is set to **automatically save updates** to the JSON file. If it only exports on demand, you’ll need to set up auto-export in the program’s settings. 内容的提问来源于stack exchange,提问作者Liam Collins

