首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在NGINX访问日志中记录mp4文件的视频时长

在NGINX访问日志中记录mp4文件的视频时长
EN

Stack Overflow用户
提问于 2021-01-07 11:02:41
回答 1查看 174关注 0票数 1

我正在尝试在NGINX访问日志中记录mp4文件的视频持续时间,这应该在客户端从服务器‘获取’mp4文件时发生。我已经配置了一个自定义日志格式,如下所示:

代码语言:javascript
运行
复制
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;

我可以添加一个自定义的header video_duration,指向视频文件的路径并手动赋值,但这需要在每次添加视频并重新加载nginx时更改nginx的配置:

代码语言:javascript
运行
复制
location /path/video.mp4 {
    add_header video_duration 123456;
}

将以下记录写入NGINX访问日志:

代码语言:javascript
运行
复制
192.168.0.1 ... /path/video.mp4 206 video/mp4 123456

我还尝试在X-Content-Duration配置中配置NGINX头文件(Firefox不再支持它),但是没有记录任何值。

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

有没有办法在NGINX访问日志中记录mp4视频文件的时长,类似于记录文件的content-length (字节)和content-type (视频/mp4)的方式?

EN

回答 1

Stack Overflow用户

发布于 2021-01-12 19:11:46

下面是供参考的实现:

nginx.conf

代码语言:javascript
运行
复制
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是指包含时长的元数据文件:

代码语言:javascript
运行
复制
$directoryPrefix: /usr/local/openresty/nginx/html
$request_uri: /video/VideoABC.mp4
$metaFile: /usr/local/openresty/nginx/html/video/VideoABC.mp4.duration

access.log

代码语言:javascript
运行
复制
192.168.0.1 ... "/video/VideoABC.mp4" 206 video/mp4 1234567890

元数据文件路径(&M)

代码语言:javascript
运行
复制
root@ubuntu: cd /usr/local/openresty/nginx/html/video/
root@ubuntu: ls
VideoABC.mp4  VideoABC.mp4.duration

root@ubuntu: cat VideoABC.mp4.duration
1234567890
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65606089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档