LanTv: Turn Local Media Folders into Shuffled TV Channels
Having a large local media library is great until you sit down to watch something. You scroll, you pick an episode, you seek, you change your mind. I wanted the opposite: a TV that is already playing something when you turn it on. LanTv is my answer. It turns folders of movies and shows into always-on, randomly shuffled channels that any device on my home network can tune into.
Why I built it
The idea was simple. I have folders such as GOT, LOTR, a dump of new releases and a big mixed library called IRMAX. I did not want to reorganize any of it. I wanted each folder to behave like a channel on a real TV: no menu, no choosing an episode, just tune in and watch whatever is on.
I also wanted it to work on whatever screen was nearby: a laptop browser, VLC, or an Android phone or TV box. That meant the output had to be something every one of those clients already understands, which pointed me to HLS.
What it does
You point each channel at one or more folders in a config.json file. Every channel then gets its own shuffled, effectively infinite playlist.
- Independent channels backed by the folders you already have.
- Infinite shuffle. A channel scans its folders recursively, shuffles the files into a queue, and when the queue empties it rescans and reshuffles. New files get picked up automatically and the channel never ends.
- On-demand transcoding. A channel only runs ffmpeg when someone opens it, and it stops after a configurable idle period with nobody watching.
- HDR to SDR tonemapping for HDR10 and Dolby Vision sources.
- Several ways to watch: a built-in browser player with a channel switcher, now-playing overlay and skip button; per-channel HLS URLs or one combined
.m3uplaylist for VLC; and an installable Android app. - Resilience: unreadable files are skipped, disconnected network volumes are retried without a restart, and the service can be set up to start on login and restart after a crash.
How it works
The server is Node.js plus ffmpeg, running on a Mac. The Android app is a Kotlin wrapper around the same browser player, so there is only one player to maintain. The web player itself is plain JavaScript and HTML.
One state machine per channel
Each channel, implemented in src/channel.js, follows the same loop:
- Scan the configured folders recursively, filtering by file extension and skipping excluded directory names such as recycle bins.
- Shuffle the file list with a Fisher-Yates shuffle into a queue.
- Play by popping one file, checking with
ffprobewhether it is HDR, and spawning ffmpeg to transcode it in real time into the channel's HLS output. - Advance when ffmpeg exits, whether the file ended or was skipped, by starting the next file immediately.
- Idle-pause through a background timer in
src/server.jsthat stops ffmpeg if nobody has requested the stream withinidleStopSeconds. The next request lazily restarts it.
Making many files look like one stream
The trick that makes this feel like TV is the -hls_flags append_list option. Instead of starting a fresh HLS stream for every file, segments keep accumulating into one continuous live playlist at hls/<id>/stream.m3u8. A player sees one unbroken live stream, and the file boundaries are invisible to it. Only one ffmpeg process runs per channel at a time, one file after another.
Why transcode at all
Source files vary wildly in codec, resolution and dynamic range. Transcoding in real time into a uniform HLS stream means any client can play any channel. On macOS I use VideoToolbox hardware encoding, so even 4K sources transcode faster than real time. The lazy start and idle stop matter for the same reason: without them I would be running a separate encode per channel around the clock for a single viewer.
HDR tonemapping
Simply stripping HDR metadata leaves the picture washed out. LanTv uses the zscale and zimg filters from Homebrew's ffmpeg-full package to do proper tonemapping to SDR. That package installs keg-only, so it can sit next to a regular ffmpeg. If it is missing, LanTv falls back to plain ffmpeg and HDR sources will look dull.
A link that keeps working
Bookmarking an IP address on a home network is fragile, since DHCP leases change. LanTv prints a Bonjour/mDNS hostname of the form <name>.local, which survives Wi-Fi switches and router reboots. On the Mac, caffeinate keeps the machine from sleeping.
What I learned
- Pick the output format your clients already speak. HLS meant I did not have to write a player for each platform. The browser, VLC and the Android wrapper all consume the same URLs.
- Do work lazily. Starting a transcode on first request and stopping it on idle is a small amount of code, and it is the difference between a toy and something I can leave running.
- Treat files as unreliable. Corrupt files and unmounted network drives are normal in a home library. Skipping bad files and retrying volumes made the service something I do not have to babysit.
- Keep the native app thin. Wrapping the web player in Kotlin gives a launcher icon and a better experience on a TV box without duplicating the player logic.
- Be honest about platform limits. The transcoding approach depends on macOS features, namely VideoToolbox and
caffeinate. The rest of the Node.js code is portable, but the hardware encoding path is not.
Conclusion
LanTv is a small server that does one thing: it makes a pile of folders behave like TV channels. The useful parts are reusable beyond this project: one state machine per stream, lazy start with idle shutdown, appending to a single HLS playlist across files, and a stable .local hostname. If you run something similar on your own network, I would start with those four ideas. Questions or feedback can go to hello@iramesh.com.