在NGINX访问日志中记录mp4文件的视频持续时间

在NGINX访问日志中记录mp4文件的视频持续时间,nginx,http-headers,mp4,access-log,Nginx,Http Headers,Mp4,Access Log,我试图在NGINX访问日志中记录mp4文件的视频持续时间,这应该在客户端从服务器“获取”mp4文件时发生。我已按如下方式配置了自定义日志格式: log_format nginx_custom_log '$remote_addr ... $request_uri $status $sent_http_content_type $video_duration'; access_log /var/log/nginx/access.log nginx_custom_log; 我可以添加一个自定义HT

我试图在NGINX访问日志中记录mp4文件的视频持续时间,这应该在客户端从服务器“获取”mp4文件时发生。我已按如下方式配置了自定义日志格式:

log_format nginx_custom_log '$remote_addr ... $request_uri $status $sent_http_content_type $video_duration';

access_log /var/log/nginx/access.log nginx_custom_log;
我可以添加一个自定义HTTP头-
video\u duration
指向视频文件的路径并手动分配值,但这需要在每次添加视频并重新加载nginx时更改nginx配置:

location /path/video.mp4 {
    add_header video_duration 123456;
}
将以下记录写入NGINX访问日志:

192.168.0.1 ... /path/video.mp4 206 video/mp4 123456
我还尝试在NGINX配置中配置
X-Content-Duration
HTTP头(Firefox不再支持该头),但没有记录任何值

我发现了一个名为ngx_http_mp4_模块的模块。它允许指定参数,如
?start=238.88&end=555.55
,这让我相信NGINX能够读取mp4文件的元数据

有没有办法在NGINX访问日志中记录mp4视频文件的持续时间,类似于如何记录文件的
内容长度(以字节为单位)和
内容类型(视频/mp4)?

感谢您建议添加元数据文件并使用Lua读取它。以下是供参考的实施方案:

nginx.conf

location ~* \.(mp4)$ {
    set $directoryPrefix '/usr/local/openresty/nginx/html';
    set $metaFile '$directoryPrefix$request_uri.duration';

    set $mp4_header "NOT_SET";

    rewrite_by_lua_block {
        local f = assert(io.open(ngx.var.metaFile, "r"))
        ngx.var.mp4_header = f:read("*line")
        f:close()
    }

    add_header mp4_duration $mp4_header;
}

log_format nginxlog '$remote_addr ... "$request_uri" $status $sent_http_content_type $sent_http_mp4_duration';
access_log /usr/local/openresty/nginx/logs/access.log nginxlog;
请注意,$metaFile是指包含持续时间的元数据文件:

$directoryPrefix: /usr/local/openresty/nginx/html
$request_uri: /video/VideoABC.mp4
$metaFile: /usr/local/openresty/nginx/html/video/VideoABC.mp4.duration
access.log

192.168.0.1 ... "/video/VideoABC.mp4" 206 video/mp4 1234567890
mp4&元数据文件路径

root@ubuntu: cd /usr/local/openresty/nginx/html/video/
root@ubuntu: ls
VideoABC.mp4  VideoABC.mp4.duration

root@ubuntu: cat VideoABC.mp4.duration
1234567890
感谢您建议添加元数据文件并使用Lua读取它。以下是供参考的实施方案:

nginx.conf

location ~* \.(mp4)$ {
    set $directoryPrefix '/usr/local/openresty/nginx/html';
    set $metaFile '$directoryPrefix$request_uri.duration';

    set $mp4_header "NOT_SET";

    rewrite_by_lua_block {
        local f = assert(io.open(ngx.var.metaFile, "r"))
        ngx.var.mp4_header = f:read("*line")
        f:close()
    }

    add_header mp4_duration $mp4_header;
}

log_format nginxlog '$remote_addr ... "$request_uri" $status $sent_http_content_type $sent_http_mp4_duration';
access_log /usr/local/openresty/nginx/logs/access.log nginxlog;
请注意,$metaFile是指包含持续时间的元数据文件:

$directoryPrefix: /usr/local/openresty/nginx/html
$request_uri: /video/VideoABC.mp4
$metaFile: /usr/local/openresty/nginx/html/video/VideoABC.mp4.duration
access.log

192.168.0.1 ... "/video/VideoABC.mp4" 206 video/mp4 1234567890
mp4&元数据文件路径

root@ubuntu: cd /usr/local/openresty/nginx/html/video/
root@ubuntu: ls
VideoABC.mp4  VideoABC.mp4.duration

root@ubuntu: cat VideoABC.mp4.duration
1234567890

我将其分为两部分:在同一目录中创建第二个包含持续时间的文件(很可能是文本文件)。持续时间可以是在文件体中,也可以是在文件名中(如原始文件mp4.Duration)。然后用Lua读取这个文件,提取并记录持续时间,我会将它分成两部分:在同一目录中创建第二个包含持续时间的文件(很可能是文本文件)。持续时间可以是在文件体中,也可以是在文件名中(如原始文件mp4.Duration)。然后使用Lua读取此文件,并提取和记录持续时间