[SHARE] HLS Stream M3U8

BlurBase

Junior
Joined
Aug 3, 2025
Messages
72
Reactions
22
MR
2.014
Chat with me via Yahoo Messenger X.com
Như tiêu đề lên không dài thì mình share source code cho ae tự dùng, code này cũng hỗ trợ cho ae Upload lên google driver, R2 hoặc upload trực tiếp trên server.
Để tránh tình trạng Cloudflare block acc do .ts lên mình đã tùy biến thay đổi mọi thứ cho ae bypass cloudflare cho ae chơi.
Ae tự clone về .env.example về thành .env nhé trong đó có sẵn file Dockerfile rồi chỉ việc deploy thành container muốn bê đi đâu cũng dễ, cái này chỉ là backend ae muốn hoàn chỉnh thì tự viết thêm frontend nhé. Muốn biết API cứ vào phần routes/web.php.
Còn ae muốn test trên môi trường dev thì nhớ chạy composer install . Còn không đẩy thẳng lên docker cho nhàn nhé, nhớ phải thêm nginx vào.

Loại code: Lumen framework (PHP)


# Mình sẽ để 0.333 MR ae tự dùng mà đổi để download chắc tính ra được mấy ngàn vnđ thôi, kêu nữa thì chịu =)).

conf nginx

Code:
server {
    listen 80;
    server_name localhost;
    root /var/www/html/public;
    index index.php index.html;

    client_max_body_size 500M;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Serve HLS files
    location /hls/ {
        alias /var/www/html/storage/app/public/hls/;
       
        # CORS headers for HLS streaming
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, OPTIONS';
        add_header Access-Control-Allow-Headers 'Range, Content-Type';
       
        # Cache HLS segments
        expires 1h;
        add_header Cache-Control "public, immutable";
       
        # Handle m3u8 playlist files
        location ~ \.m3u8$ {
            add_header Content-Type application/vnd.apple.mpegurl;
            expires -1;
        }
       
        # Handle ts segment files
        location ~ \.ts$ {
            add_header Content-Type video/mp2t;
        }
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_read_timeout 600;
    }

    location ~ /\.ht {
        deny all;
    }
}
1761529581201.png


1761529348502.png
 
Update đã fix các lỗi lặt vặt, mời các ae xơi. Ae tải bản cũ thì cứ vào link mà tải bản mới mà dùng thế nhé. Rảnh thì mình update tiếp sau. Ae nào biết code thì khuyên lên viết thêm Node.js để làm Kafka bắn cho worker làm việc thay vì chạy trên 1 con server. Thì tốc độ xử lý bất đồng bộ nhanh hơn.

1761535610106.png
 
Last edited:

Announcements

Forum statistics

Threads
434,128
Messages
7,315,605
Members
185,549
Latest member
huucuong84

Most viewed of week

Most discussed of week

Most viewed of week

Most discussed of week

Back
Top Bottom