LoginSignup
3
1

nginx-vod-moduleでon-the-flyなhls配信環境を作る

Last updated at Posted at 2023-04-02

nginx-vod-moduleとは

サーバー上にある動画ファイルを動的にhlsやmpdに変換してくれるnginx拡張

前提

QXfKogTWwOaU4q81680420142_1680420195.png

必要なツール類をインストール

apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev git

ソースコードをダウンロード

nginx

wget https://nginx.org/download/nginx-1.23.3.tar.gz
tar -xzvf nginx-1.23.3.tar.gz

nginx-vod-module

cd nginx-1.23.3
git clone https://github.com/kaltura/nginx-vod-module

ビルド

設定

 ./configure --add-module=./nginx-vod-module --with-file-aio --with-threads --with-cc-opt='-O3 -mpopcnt' --with-cc-opt='-DNGX_VOD_MAX_TRACK_COUNT=256 -mavx2' --with-http_ssl_module --with-http_sub_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_addition_module --with-http_slice_module --with-http_dav_module --with-http_v2_module --with-http_auth_request_module --with-http_realip_module --with-http_stub_status_module --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-pcre-jit --with-cc-opt='-g -O2 -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC' --with-compat --with-debug --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module

ビルド

make
make install

設定

/etc/nginx/conf.d/sample.conf

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name  example.com;
    root  /path/to/root;
    include /etc/nginx/default.d/*.conf;
    client_max_body_size 100M;


    vod_mode local; #基本はlocalでok、remoteを入れるとhttp経由でmp4を取れる
    vod_fallback_upstream_location /fallback;
    vod_last_modified 'Sun, 19 Nov 2000 08:52:00 GMT';
    vod_last_modified_types *;
    vod_metadata_cache metadata_cache 1024m;
    vod_response_cache response_cache 512m;
    vod_manifest_segment_durations_mode accurate; #hlsなどのセグメント長を実際の数値に合わせる
    vod_segment_duration 10000; #セグメント長はお好みで
    vod_align_segments_to_key_frames on; #onにしない(キーフレームに合わせない)と音だけとかになる

    gzip on;
    gzip_types application/vnd.apple.mpegurl;

    open_file_cache          max=1000 inactive=5m;
    open_file_cache_valid    2m;
    open_file_cache_min_uses 1;
    open_file_cache_errors   on;
    aio on;

    location / {
        vod dash;
	    vod hls;
        alias /path/to/movie/;
    }
}

※パスは適時書き換えてください
※認証は別途設定することをおすすめします

アクセスしてみる

http ://ホスト名/動画へのパス/master.m3u8
へhls対応プレイヤーでアクセスすると動画を再生できます

トラブルシューティング

vmでうまく動かない

vod_modeがlocalの際にvirtioなどを使用するとエラーが出ることがあります
その場合はnfsなどを経由してみてください

segmentのurlがすべてhttpになっている

プロキシなどを噛ませているなどでサーバーがhttpで接続を受け付けている場合、セグメントのurlもhttpになります
sslを有効にしてみてください

hls.jsでセグメント間に抜けが発生する

server内に以下の設定を追記してみてください
※サンプルには設定されています

    vod_manifest_segment_durations_mode accurate; #hlsなどのセグメント長を実際の数値に合わせる
    vod_segment_duration 10000; #セグメント長はお好みで
    vod_align_segments_to_key_frames on; #onにしない(キーフレームに合わせない)と音だけとかになる
3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1