Metadata-Version: 2.5
Name: hlsfetch
Version: 0.1.1
Summary: Fast async HLS (m3u8) downloader for Python: resume, AES-128, fMP4, ffmpeg remux.
Project-URL: Homepage, https://github.com/doumamay/hlsfetch
Project-URL: Issues, https://github.com/doumamay/hlsfetch/issues
Author: doumamay
License-Expression: MIT
License-File: LICENSE
Keywords: aes-128,asyncio,downloader,hls,m3u8,streaming,video
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Multimedia :: Video
Requires-Python: >=3.9
Requires-Dist: cryptography>=41
Requires-Dist: httpx>=0.26
Provides-Extra: socks
Requires-Dist: httpx[socks]>=0.26; extra == 'socks'
Description-Content-Type: text/markdown

# hlsfetch

Fast, async HLS (`.m3u8`) downloader for Python, usable as a library or from the command line.

- Parallel segment downloads on `asyncio` + `httpx`
- Resumes interrupted downloads: run the same call again and finished segments are skipped
- AES-128 decryption (key rotation and explicit or implicit IVs are handled)
- MPEG-TS and fMP4 (`#EXT-X-MAP`) streams, plus `#EXT-X-BYTERANGE`
- Quality selection: `best`, `worst`, max height, or your own function
- Custom headers, Referer, proxy (HTTP or SOCKS)
- Optional remux to `.mp4` through ffmpeg (stream copy, no re-encoding)
- Only two dependencies: `httpx` and `cryptography`

## Install

```bash
pip install hlsfetch
```

For SOCKS proxies: `pip install "hlsfetch[socks]"`. To get `.mp4` output from MPEG-TS streams, install [ffmpeg](https://ffmpeg.org/) and put it on your `PATH`. Without ffmpeg the raw `.ts` file is saved.

## Command line

```bash
hlsfetch "https://example.com/video/master.m3u8" -o movie.mp4
```

```text
hlsfetch URL [-o OUTPUT] [-q best|worst|720] [-H 'Name: value']... [-r REFERER]
             [-j CONCURRENCY] [--retries N] [--timeout S] [--proxy URL]
             [--no-remux] [--keep-parts] [-i/--info]
```

`hlsfetch URL --info` lists the variants, segment count, duration and encryption without downloading anything.

If a download is interrupted, run the same command again to resume it.

## Library

```python
import hlsfetch

path = hlsfetch.download(
    "https://example.com/video/master.m3u8",
    "movie.mp4",
    quality=720,                      # "best", "worst", 720, or a callable
    headers={"Referer": "https://example.com/"},
    concurrency=16,
)
print(path)
```

Async, with a progress callback:

```python
import asyncio
import hlsfetch

def on_progress(p: hlsfetch.Progress):
    print(f"{p.done}/{p.total} segments, {p.bytes / 1e6:.1f} MB, {p.speed / 1e6:.1f} MB/s")

async def main():
    await hlsfetch.adownload(url, "movie.mp4", progress=on_progress)

asyncio.run(main())
```

The callback may also be an `async def`. You can pass your own `httpx.AsyncClient` through `client=` to share cookies or connection pools.

Parse a playlist yourself:

```python
pl = hlsfetch.parse_playlist(text, url)
if isinstance(pl, hlsfetch.MasterPlaylist):
    for v in pl.variants:
        print(v.resolution, v.bandwidth, v.uri)
else:
    print(len(pl.segments), "segments,", pl.duration, "seconds")
```

### `download()` / `adownload()` options

| Option | Default | Meaning |
|---|---|---|
| `quality` | `"best"` | `"best"`, `"worst"`, max height (`720`), or `callable(variants) -> Variant` |
| `headers` | `None` | Extra HTTP headers |
| `concurrency` | `8` | Segments downloaded at once |
| `retries` | `5` | Retries per request (network errors, 408/429/5xx) |
| `timeout` | `30.0` | Request timeout in seconds |
| `proxy` | `None` | Proxy URL |
| `remux` | `True` | Use ffmpeg when the output extension differs from the stream's container |
| `keep_parts` | `False` | Keep the `<output>.parts/` folder after merging |
| `progress` | `None` | Called with a `Progress` after each segment |
| `client` | `None` | Your own `httpx.AsyncClient` (it is not closed for you) |

Errors are raised as subclasses of `hlsfetch.HLSFetchError`: `PlaylistError`, `UnsupportedError` and `DownloadError`.

## Limitations

- VOD only. Live playlists (no `#EXT-X-ENDLIST`) raise `UnsupportedError`.
- No DRM (`SAMPLE-AES`, Widevine, FairPlay), and there are no plans to add it.
- Separate audio renditions (`#EXT-X-MEDIA`) are not merged yet. Only the selected variant is downloaded.

## Disclaimer

hlsfetch is a general-purpose tool for working with HLS streams, intended for
legitimate uses such as archiving your own content, testing and debugging
streaming setups, and downloading media you are licensed or permitted to save.

- **You are responsible for how you use it.** Only download content you own,
  content that is in the public domain, or content you have explicit permission
  to download. Respect the terms of service of the sites you access and the
  copyright laws of your country.
- **No DRM circumvention.** hlsfetch does not and will not support DRM systems
  (Widevine, FairPlay, PlayReady, SAMPLE-AES). It only handles standard
  AES-128 HLS encryption, using keys that the stream itself makes publicly
  available to any compatible player.
- **No affiliation.** This project is not affiliated with, endorsed by, or
  connected to any streaming service or content provider.
- **No warranty.** The software is provided "as is", without warranty of any
  kind, as stated in the MIT License. The authors are not liable for any misuse
  or damage arising from its use.

If you are a rights holder and believe this project infringes your rights,
please open an issue so it can be addressed.

## License

MIT
