How To Make Iptv Server In Ubuntu

How To Make Iptv Server In Ubuntu

Reading time1 min
#Streaming#Ubuntu#IPTV#FFmpeg#Linux

Building a Robust IPTV Server on Ubuntu

Deploying your own IPTV server on Ubuntu eliminates dependency on commercial services and introduces operational control, cost efficiency, and scalability. Whether you're distributing in-house live feeds, archiving video sources, or running hobbyist setups, a Linux-based IPTV backbone is the method of choice.

This walkthrough focuses on leveraging Ubuntu LTS (18.04/20.04/22.04), FFmpeg (≥4.2), and Nginx for a no-nonsense HLS-based IPTV platform.


Infrastructure Prerequisites

  • Ubuntu 20.04 LTS (bare metal, VM, or typical cloud VPS).
  • Root or sudo-enabled CLI access.
  • Sufficient upstream bandwidth for concurrent viewers; e.g., 5Mbps × viewers for 1080p H.264.
  • Source: local video files or live (IP cam, DVB card, etc.).

Note: Insufficient I/O or CPU will cause segment generation lag (Non-monotonous DTS in output stream errors from FFmpeg).


1. Baseline System Prep

sudo apt update && sudo apt upgrade -y
sudo apt install -y ffmpeg nginx curl

FFmpeg must be compiled with --enable-gpl --enable-libx264. Ubuntu repos usually suffice, but for hardware transcoding (VAAPI/QuickSync/Nvidia), custom builds might be required.


2. Filesystem and Permissions Layout

Determine storage path (default: /var/www/html/iptv). Set up channel directories:

sudo mkdir -p /var/www/html/iptv/live
sudo chown -R $USER:www-data /var/www/html/iptv
sudo chmod -R 775 /var/www/html/iptv

Gotcha: On high-throughput systems, locate /iptv on XFS or ext4 with noatime for better disk efficiency.


3. FFmpeg: HLS Segmenting Pipeline

Start with static stream validation using a test file:

ffmpeg -hide_banner -re -i /home/user/videos/sample.mp4 \
  -c:v libx264 -preset superfast -crf 22 -g 48 -sc_threshold 0 \
  -c:a aac -b:a 128k \
  -f hls \
  -hls_time 6 -hls_list_size 5 -hls_flags delete_segments+append_list \
  /var/www/html/iptv/live/sample.m3u8

Breakdown:

  • -g 48 sets 2s GOP for 24 fps; adapt for stream resilience.
  • -hide_banner minimizes log output.
  • -sc_threshold 0 disables scene cut, producing more regular segments (critical for some clients).
  • append_list in -hls_flags aids in playlist continuity for VOD playback.

Error messages like:

[hls muxer @ ...] Opening '/var/www/html/iptv/live/sample0.ts' for writing

show chunk generation. Segment lag or “filename already exists” indicates sync issues or disk bottlenecks.


4. Nginx Configuration

Nginx is defaulted to /var/www/html. Confirm status:

sudo systemctl status nginx

For basic setups, no change to /etc/nginx/sites-available/default unless using custom ports/tls. For HTTPS, generate and reference Let's Encrypt certificates:

server {
    listen 443 ssl;
    server_name iptv.example.com;

    ssl_certificate /etc/letsencrypt/live/iptv.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/iptv.example.com/privkey.pem;

    root /var/www/html;
    location /iptv/ {
        autoindex on;
        add_header Cache-Control no-cache;
    }
}

Note: For >100 concurrent viewers, enable sendfile on; and tune worker_connections appropriately.

Firewall:

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS' # if running TLS

5. HLS Client Playback

Sample .m3u8 endpoint:

http://<server_ip>/iptv/live/sample.m3u8

Test with VLC (Ctrl+N > paste URL). For browser, video.js with HLS plugin or Safari natively.

Common playback issue:

  • VLC “Your input can't be opened”: check file permissions and Nginx access logs.

6. Live Source Integration

Example: ingest an RTSP stream from a camera:

ffmpeg -rtsp_transport tcp -i rtsp://user:pw@192.168.1.25/stream1 \
  -c:v libx264 -preset veryfast -crf 21 \
  -an \
  -f hls \
  -hls_time 4 -hls_segment_filename "/var/www/html/iptv/live/cam_%04d.ts" \
  /var/www/html/iptv/live/cam.m3u8
  • For input instability, add -timeout 20000000 -stimeout 20000000 to avoid disconnects.

7. Multi-Channel Management: M3U Playlists

Assemble a master .m3u:

#EXTM3U
#EXTINF:-1 tvg-id="ch1" tvg-name="Main Feed",Main TV
http://<server_ip>/iptv/live/sample.m3u8
#EXTINF:-1 tvg-id="ch2" tvg-name="Camera 1",Security Cam
http://<server_ip>/iptv/live/cam.m3u8

Store at /var/www/html/iptv/channels.m3u. Clients supporting M3U can ingest this as a channel list (e.g., Kodi, Smart TVs).


8. Automation & Hardening

  • Autostart Streams: Use systemd unit files to supervise FFmpeg jobs (restart on failure).
  • Resource-aware Tuning: Monitor with htop, iotop, or nload. HLS segment size and duration affect seek latency and RAM usage.
  • TLS: Always enable for external access; otherwise, sniffed endpoints leak stream URLs.
  • Log Rotation: Large HLS setups can fill disks—clear old .ts if hls_flags doesn’t keep up.

Sample systemd unit (/etc/systemd/system/ffmpeg-sample.service):

[Unit]
Description=FFmpeg HLS Stream (Sample)
After=network.target

[Service]
ExecStart=/usr/bin/ffmpeg -re -i /opt/iptv/sample.mp4 ...etc...
Restart=on-failure
User=www-data

[Install]
WantedBy=multi-user.target

Known issue: systemd restarts may cause partial .m3u8 files. Use OnFailure hooks for alarm notifications if critical.


Note on Scaling

Presence of multi-bitrate (ABR) ladders requires variant playlists. FFmpeg’s built-in HLS support is basic—production setups often migrate to tools like nginx-rtmp-module for RTMP ingest or use packager frameworks (e.g., GPAC, Shaka Packager). For very high concurrency, CDN in front of nginx is compulsory.


In Summary

Ubuntu, FFmpeg, and Nginx form a solid IPTV backbone. The outlined approach is resilient, but expect to adapt segmenter parameters and automate recovery for real environments. Using HLS is not the only protocol—DASH and even SRT may be necessary for advanced scenarios, but these add complexity.

Practical note: Always benchmark end-to-end before putting in front of real users. Small config oversights surface as stream stalls and unhappy clients.

Further reading: consult FFmpeg docs, nginx tuning manuals, and consider proper monitoring (Prometheus exporters, etc.) once out of the lab.


Questions or real-world issues? Drop into the comment thread.