This project is a serverless function designed for Cloudflare Pages that dynamically fetches, merges, and standardizes M3U playlist files from multiple sources. It's built to be deployed as a single _worker.js file, offering a powerful way to manage IPTV playlists with advanced features like channel deduplication, category standardization, and robust channel number assignment.
- Playlist Merging: Aggregates multiple M3U playlists into a single, unified list.
- Deduplication: Intelligently removes duplicate channels based on their stream URL or TVG ID to ensure a clean playlist.
- Chinese Channel Filtering: Provides a dedicated endpoint to generate a playlist containing only Chinese-language channels.
- Category Standardization: Normalizes channel groups into a consistent set of categories (e.g., 'News', 'Sports', 'Movies').
- Robust Channel Numbering: Assigns a unique and persistent channel number (
tvg-chno) to every entry, starting from 101. It respects existing valid numbers and intelligently fills in missing ones. - On-the-Fly Processing: All operations are performed in real-time when you request the playlist.
- Caching: Utilizes Cloudflare's cache to deliver playlists quickly and reduce fetches to the source URLs.
- Debugging Mode: A built-in debug view to inspect the merging and channel assignment process.
- Serverless: Runs entirely on the Cloudflare network with no servers to manage.
Once deployed to your Cloudflare Pages project, you can access the following endpoints:
/hello- A simple health check endpoint to confirm the worker is running. Returns an "ok" response.
/merged.m3u- Provides the main playlist with all channels from the configured sources, after merging, deduplication, and standardization.
/chinese.m3u- Delivers a filtered playlist containing only channels identified as Chinese.
To understand how the script is processing your channels, you can access a detailed debug page. This page provides a complete directory of all processed channels, statistics on channel number assignment, and a detailed breakdown of the first few entries.
To activate the debug mode, add ?debug=1 to the end of any playlist URL.
- Example for the merged playlist:
/merged.m3u?debug=1/chinese.m3u?debug=1
The debug output is a plain text file that includes:
- Playlist Statistics: Total sources, raw entry count, and the final count after deduplication.
- Complete Channel Directory: A numbered list of every single channel in the final playlist, showing its assigned channel number, name, and final group.
- Channel Assignment Statistics: Data on how many channels kept their original number versus how many were assigned a new one.
- Detailed Sample: A comprehensive look at the first 10 channels, showing all their original and final attributes.
The _worker.js script intercepts requests to specific paths on your Cloudflare Pages site.
- Fetch: When a request for a playlist is made, the worker fetches the content from the M3U sources defined in the
SOURCESarray. - Parse: Each M3U file is parsed line-by-line to extract channel information, including its name, stream URL, TVG ID, group title, and channel number.
- Categorize & Filter: Channels are identified as Chinese if they contain Chinese characters in their name or come from a designated primary Chinese source. For other channels, a
standardizeCategoryfunction is used to map various group titles to a clean, standard set. - Deduplicate: The script removes duplicate channels by tracking stream URLs and TVG IDs.
- Assign Channel Numbers: A two-pass system ensures every channel gets a valid number. It first catalogs all existing valid channel numbers and then assigns new, sequential numbers (starting from 101) to any channel that lacks one.
- Serialize: The final, clean list of channels is serialized back into a standard M3U text format.
- Cache & Deliver: The generated playlist is cached on Cloudflare's edge network for faster subsequent requests and then delivered to the user.
This project is designed to be deployed as a single script using the "advanced" routing mode on Cloudflare Pages.
- Fork this Repository: Start by forking this repository to your own GitHub account.
- Create a Cloudflare Pages Project:
- Log in to your Cloudflare dashboard.
- Go to Workers & Pages and select the Pages tab.
- Click Create a project and connect it to your forked GitHub repository.
- Configure the Build:
- You can skip the build settings as there's no traditional build step.
- Deploy:
- Deploy the project.
- Enable Advanced Mode:
- After the initial deployment, go to your project's Settings > Functions.
- The
_worker.jsfile will be automatically detected and used to handle requests.
You can easily customize the script by editing the _worker.js file:
-
Add More Sources: To include more M3U playlists, simply add their URLs to the
SOURCESarray.const SOURCES = [ 'https://aktv.space/live.m3u', 'https://iptv-org.github.io/iptv/index.m3u', // Add your new source URL here ];
-
Change Chinese Source: Modify the
PRIMARY_CHINESE_SOURCEvariable if you have a different primary source for Chinese channels.const PRIMARY_CHINESE_SOURCE = 'https://your-primary-source.com/live.m3u';
-
Adjust Categories: You can expand the
categoryMapin thestandardizeCategoryfunction to handle more group titles.const categoryMap = { 'news': 'News', 'sport': 'Sports', 'movie': 'Movies', // Add new mappings here };