What Determines the Ordering of Channels Within Each Country IPTV Playlist?

The order of channels in each country-specific IPTV playlist is determined entirely by the sequence of entries in the corresponding markdown list file located in the lists/ directory.

The Free-TV/IPTV repository generates country-specific M3U playlists through an automated Python script that processes human-curated markdown files. Unlike streaming services that algorithmically sort content, this open-source project preserves the exact manual ordering defined by maintainers in the source markdown, creating a deterministic channel sequence that reflects intentional curation rather than automated sorting.

How Channel Ordering Works in the Playlist Generator

The playlist generation logic in make_playlist.py implements a straightforward line-by-line processing approach that maintains the original source order without modification.

The Source of Truth: Markdown List Files

Each country maintains its channel definitions in a dedicated markdown file within the lists/ directory (e.g., lists/germany.md, lists/usa.md). These files contain channel entries marked with the [>] directive:


# Germany

[>] 1 | ARD | http://example.com/ard.m3u8 | <img src="https://example.com/logo/ard.png">
[>] 2 | ZDF | http://example.com/zdf.m3u8 | <img src="https://example.com/logo/zdf.png">
[>] 3 | RTL | http://example.com/rtl.m3u8 | <img src="https://example.com/logo/rtl.png">

The sequence of these entries in the markdown file directly determines the sequence in the generated playlists/playlist_germany.m3u8 file. No reordering occurs during processing.

The Line-by-Line Processing Logic

In make_playlist.py, the script reads each country's markdown file sequentially and preserves the line order when writing to the output playlist. The critical code section demonstrates this pass-through behavior:


# 1. List markdown files and sort them alphabetically

for filename in sorted(os.listdir(lists_dir)):
    if filename == "README.md" or not filename.endswith(".md"):
        continue

    markup_path = os.path.join(lists_dir, filename)
    country_path = os.path.join(dir_playlists, "playlist_" + filename[:-3] + ".m3u8")
    country_key = filename[:-3]
    group = country_key.replace("_", " ").title()
    country_code = COUNTRY_CODES.get(country_key, "")

    # 2. Read the markdown file line-by-line

    with open(markup_path, encoding='utf-8') as markup_file, \
         open(country_path, "w", encoding='utf-8') as playlist_country:
        playlist_country.write(head_playlist)
        for line in markup_file:
            # Only process lines that contain a channel entry

            if "[>]" not in line:
                continue
            channel = Channel(group, line, country_code)
            m3u_line = channel.to_m3u_line()
            # Write exactly in the order encountered

            print(m3u_line, file=playlist)
            print(m3u_line, file=playlist_country)

The Channel class parses each line containing [>] and generates the M3U output, with the print() statements executing immediately upon encountering each source line. This ensures that the first channel defined in lists/germany.md becomes the first entry in playlists/playlist_germany.m3u8, and so on.

Why Country Playlists Follow Manual Curation

The repository intentionally avoids automated sorting to give maintainers full control over channel presentation. This human-curated approach allows for:

  • Logical grouping of related channels (e.g., placing all sports channels adjacent to one another regardless of alphabetical order)
  • Priority ordering of high-demand channels at the top of the list
  • Geographic or language-based sequencing within multilingual countries

Since the script does not implement sorting by channel name, number, or metadata fields, the burden of organization falls entirely on the markdown file structure.

Country File Processing vs. Channel Ordering

While the script does sort the country files alphabetically using sorted(os.listdir(lists_dir)) to determine the order in which it processes different nations, this alphabetical sorting applies only to the sequence of countries being handled. It does not affect the internal ordering of channels within any specific country's playlist.

The flag_order.txt file mentioned in the repository determines the order of countries in the master playlist compilation, but again, this has no bearing on how channels are arranged within individual country-specific M3U files.

Summary

  • Channel order is source-defined: The sequence in lists/<country>.md directly maps to the output in playlists/playlist_<country>.m3u8.
  • No automated sorting: The make_playlist.py script processes entries line-by-line without reordering by name, number, or metadata.
  • Human-curated arrangement: Maintainers control playlist order by manually positioning entries in the markdown source files.
  • Alphabetical sorting applies only to countries: While country files are processed alphabetically, this affects only the generation sequence, not internal channel arrangement.

Frequently Asked Questions

Does the Free-TV/IPTV script sort channels alphabetically?

No, the script does not sort channels alphabetically. According to the source code in make_playlist.py, channels are written to the playlist in the exact order they appear in the respective markdown list file. The only alphabetical sorting performed is on the country filenames themselves when determining which country to process next.

Can I reorder channels by editing the generated M3U files?

While you can manually edit the generated files in the playlists/ directory, any changes will be overwritten the next time make_playlist.py runs. To permanently change channel ordering, you must modify the source markdown files in the lists/ directory, as these serve as the authoritative source for the generation process.

What does the [>] marker signify in the markdown files?

The [>] string serves as a delimiter that identifies valid channel entries in the markdown parser. When make_playlist.py encounters a line containing [>], it instantiates a Channel object and converts the line to a proper M3U format. Lines without this marker are ignored during playlist generation, allowing maintainers to include comments or headers without affecting output.

Is there a way to automatically sort channels by name or number?

The current implementation in make_playlist.py does not support automatic sorting by channel metadata. To achieve alphabetical or numerical ordering, maintainers must manually arrange entries in the lists/<country>.md files. Any sorting would need to be implemented as a pre-processing step before the script executes, or by modifying the Channel class logic to implement sorting before the to_m3u_line() method is called.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →