跳至正文
首页 » 利用 Jellyfin 建立媒体服务器

利用 Jellyfin 建立媒体服务器

前面几篇文章介绍了使用树莓派搭建代码托管服务器网盘离线下载器,本文将介绍 利用 Jellyfin 建立媒体服务器 。使用离线下载器功能下载好电影、电视剧后,直接通过网页或通过 Jellyfin 客户端访问媒体资源。

利用 Jellyfin 建立媒体服务器 —— Jellyfin LOGO

1 安装 Jellyfin

安装 apt-transport-https ,使用 HTTPS 协议的软件仓库

sudo apt install apt-transport-https

添加 Jellyfin 的 GPG Keys

curl https://repo.jellyfin.org/debian/jellyfin_team.gpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/jellyfin-archive-keyring.gpg >/dev/null

添加 Jellyfin 的软件仓库

echo "deb [signed-by=/usr/share/keyrings/jellyfin-archive-keyring.gpg arch=$( dpkg --print-architecture )] https://repo.jellyfin.org/debian $( lsb_release -c -s ) main" | sudo tee /etc/apt/sources.list.d/jellyfin.list

更新软件仓库列表

sudo apt update

安装 Jellyfin

sudo apt install jellyfin

安装完成后,通过访问 http://[IPADDRESS]:8096 即可访问 Jellyfin 。

2 配置 NGINX

安装 NGINX

sudo apt-get install nginx

配置 /etc/nginx/sites-enabled/default.conf 或新建配置文件 /etc/nginx/sites-enabled/jellyfin.conf。需要注意对于一个域名,不用在多个文件中重复定义。 需根据实际情况修改配置文件中的 DOMAIN_NAME

server {
    listen 80;
    listen [::]:80;
    server_name DOMAIN_NAME;

    # use a variable to store the upstream proxy
    set $jellyfin 127.0.0.1;
    resolver 127.0.0.1 valid=30;

    # Security / XSS Mitigation Headers
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    location = / {
        return 302 http://$host/web/;
        #return 302 https://$host/web/;
    }

    location / {
        # Proxy main Jellyfin traffic
        proxy_pass http://$jellyfin:8096;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header X-Forwarded-Host $http_host;

        # Disable buffering when the nginx proxy gets very resource heavy upon streaming
        proxy_buffering off;
    }

    # location block for /web - This is purely for aesthetics so /web/#!/ works instead of having to go to /web/index.html/#!/
    location = /web/ {
        # Proxy main Jellyfin traffic
        proxy_pass http://$jellyfin:8096/web/index.html;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header X-Forwarded-Host $http_host;
    }

    location /socket {
        # Proxy Jellyfin Websockets traffic
        proxy_pass http://$jellyfin:8096;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header X-Forwarded-Host $http_host;
    }
}

3 配置 HTTPS

在进行下面配置前,需要通过 Certbot 或 证书申请网站,申请域名的证书文件,并将其上传至服务器。修改 NGINX 配置文件如下,需根据实际情况修改配置文件中的 DOMAIN_NAMEPATHTOKEY

# Uncomment the commented sections after you have acquired a SSL Certificate
server {
    listen 80;
    listen [::]:80;
    server_name DOMAIN_NAME;

    # Uncomment to redirect HTTP to HTTPS
     return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name jellyfin.beekc.top;

    # use a variable to store the upstream proxy
    # in this example we are using a hostname which is resolved via DNS
    # (if you aren't using DNS remove the resolver line and change the variable to point to an IP address e.g `set $jellyfin 127.0.0.1`)
    set $jellyfin 127.0.0.1;
    resolver 127.0.0.1 valid=30;

    ssl_certificate     /PATHTOKEY/beekc.crt;
    ssl_certificate_key /PATHTOKEY/beekc.key;
    ssl_trusted_certificate     /PATHTOKEY/beekc.crt;
    add_header Strict-Transport-Security "max-age=31536000" always;

    # Security / XSS Mitigation Headers
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    location = / {
        return 302 http://$host/web/;
        #return 302 https://$host/web/;
    }

    location / {
        # Proxy main Jellyfin traffic
        proxy_pass http://$jellyfin:8096;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header X-Forwarded-Host $http_host;

        # Disable buffering when the nginx proxy gets very resource heavy upon streaming
        proxy_buffering off;
    }

    # location block for /web - This is purely for aesthetics so /web/#!/ works instead of having to go to /web/index.html/#!/
    location = /web/ {
        # Proxy main Jellyfin traffic
        proxy_pass http://$jellyfin:8096/web/index.html;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header X-Forwarded-Host $http_host;
    }

    location /socket {
        # Proxy Jellyfin Websockets traffic
        proxy_pass http://$jellyfin:8096;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header X-Forwarded-Host $http_host;
    }
}

4 硬件加速

这里以树莓派为例,介绍 Jellyfin 的硬件加速功能配置。其他环境步骤类似,可以参考官方文档

将 Jellyfin 增加 video 用户组,使其可以访问硬件编码器

sudo usermod -aG video jellyfin
sudo systemctl restart jellyfin

Jellyfin 页面中 设置 - 控制台 - 播放 - 转码硬件加速 中选择 OpenMAX OMX

编辑 /boot/config.txt ,树莓派4增加 gpu_mem=320 ,树莓派3增加 gpu_mem=256,之后重启树莓派即可使用应景加速功能。

5 问题排查

无法更新电视剧媒体信息

Jellyfin 自带媒体信息的削刮功能,导入电影后,会自动更新媒体信息。但受于国内网络状况限制和命名格式,电视剧的媒体信息更新比较困难。

使用 DNSChekcer 获取 api.themoviedb.org 国内可以访问的 ip,将其加入 /etc/hosts 中,如

127.0.1.1       raspberrypi
143.204.143.12  api.themoviedb.org

一级目录:电视剧名称、二级目录:Season X、媒体文件:SXXEXX 的格式存储媒体文件。再重新刷新元数据即可更新电视剧媒体信息。

安装时报错 Depends: jellyfin-ffmpeg but it is not installable

安装 Jellyfin 时报错,缺少依赖,jellyfin-ffmpeg 无法安装。手动安装该软件,又会报告缺少其他依赖。最后排查发现为软件仓库问题。我开始使用的是阿里的镜像源,恢复为官方源后问题解决。

6 参考

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注