You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否用Python等工具从直播HLS M3U8电台流提取EXTINF歌曲信息?

Extracting Song Titles from HLS M3U8 Radio Streams with Python

Absolutely! You can absolutely automate grabbing and saving song titles from HLS M3U8 radio streams whenever tracks switch. Here's a straightforward approach using Python, tailored to your needs:

How It Works

HLS radio streams rely on M3U8 playlists that often include #EXTINF tags with track metadata (artist, song title, etc.). We'll periodically fetch this playlist, parse the EXTINF line, and save the title to a file whenever it changes from the last track we recorded.

Step-by-Step Implementation

1. Install Required Dependencies

First, make sure you have requests installed for fetching the playlist:

pip install requests

2. Python Script to Automate Extraction

This script will poll the M3U8 URL, check for track changes, and save new titles to a text file:

import time
import requests

# Configure these values based on your radio stream
M3U8_URL = "https://your-radio-stream-url/playlist.m3u8"
CHECK_INTERVAL = 30  # Seconds between checks (adjust as needed)
OUTPUT_FILE = "song_history.txt"

def get_current_song():
    try:
        response = requests.get(M3U8_URL, timeout=10)
        response.raise_for_status()  # Raise error for HTTP issues
        
        # Parse the EXTINF line (format: #EXTINF:-1,Artist - Song Title)
        for line in response.text.splitlines():
            if line.startswith("#EXTINF"):
                # Extract everything after the comma
                return line.split(",", 1)[1].strip()
        return None
    except Exception as e:
        print(f"Error fetching stream data: {e}")
        return None

def main():
    last_song = None
    print(f"Monitoring stream for track changes... (checking every {CHECK_INTERVAL}s)")
    
    while True:
        current_song = get_current_song()
        
        if current_song and current_song != last_song:
            print(f"New track detected: {current_song}")
            # Append to output file
            with open(OUTPUT_FILE, "a", encoding="utf-8") as f:
                f.write(f"{current_song}\n")
            last_song = current_song
        
        time.sleep(CHECK_INTERVAL)

if __name__ == "__main__":
    main()

3. Customization Tips

  • Adjust Polling Interval: If the station switches tracks frequently, lower CHECK_INTERVAL (but don't go below 10s to avoid overwhelming the server).
  • Handle Edge Cases: Some streams might have different EXTINF formats—tweak the parsing logic if your stream uses a different structure (e.g., extra metadata fields).
  • Add Error Handling: You could extend the script to retry failed requests or log errors to a separate file for debugging.

Notes

  • Since you can already see the song info in Chrome DevTools, this script will work for your stream (it's pulling the exact same playlist data your browser uses).
  • If the stream uses encrypted segments, you don't need to decrypt them—we only care about the plaintext M3U8 playlist metadata.

内容的提问来源于stack exchange,提问作者Andrew Fabri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:51:35