ラズパイでDLNAサーバを構築する

TVで録画した番組をTV以外のデバイスで見られるようにできないものかと思ったのが取り組みのきっかけです。

手始めにラズパイをDLNAサーバに仕立て上げ、ラズパイに接続した外付けHDDの映像をTVで見られるようにしました。

背景・前提

背景

引っ越しのタイミングでTVケーブルを紛失して以来、数年間テレビを見ない生活を送っていた。それで特に困ることはなかったのだが、この夏のドラマ半沢直樹を見るためにTVケーブルを購入した。

半沢直樹のドラマは前回の驚異的な視聴率に少しでも近くためか、ネット配信や見逃し配信を一切行わない。オンタイムで視聴するしかないのである。

これはなかなか姑息な手段であるとともに、有効だと思う。現に数年間TV見ていない人間がケーブルを買ってまでオンタイムで見ようとしているだから。

まあ、前置きはこのくらいにしておくが、わざわざ見られるようにしたTVを活用したい。録画した番組をどこからでも見られたら嬉しいんじゃないかと思い検討してみた。

前提

Raspberry pi 4を利用するので、前提としてRaspberry Piがファイルサーバーとして稼働している必要がある。割と記事通りに組み立てれば動作する。

https://synrock-tech.com/hardware/single_board/raspberry-pi-setting-up

https://synrock-tech.com/software/vpn/android-samba-fileserver

これらを踏まえてRaspberry pi 4 model BをDLNAサーバとして構築していく。

環境

▼Raspberri Pi 4 Model B

pi@raspberrypi:~ $ cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"

▼minidlna

pi@raspberrypi:~ $ minidlnad -V
Version 1.2.1

▼SONY BRAVIA KDL-40W600B

DLNAインストール

普段通り、最新化しておく。

pi@raspberrypi:~ $ sudo apt-get update
pi@raspberrypi:~ $ sudo apt-get upgrade

上記コマンドを実行し、最新化できたところで、minidlnaをインストールする。

pi@raspberrypi:~ $ sudo apt-get install minidlna

インストールができたら、catコマンドでconfを出力してみる。

pi@raspberrypi:~ $ cat /etc/minidlna.conf

#以下出力結果

# This is the configuration file for the MiniDLNA daemon, a DLNA/UPnP-AV media
# server.
#
# Unless otherwise noted, the commented out options show their default value.
#
# On Debian, you can also refer to the minidlna.conf(5) man page for
# documentation about this file.

# Specify the user name or uid to run as (root by default).
# On Debian system command line option (from /etc/default/minidlna) overrides this.
#user=minidlna


# Path to the directory you want scanned for media files.
#
# This option can be specified more than once if you want multiple directories
# scanned.
#
# If you want to restrict a media_dir to a specific content type, you can
# prepend the directory name with a letter representing the type (A, P or V),
# followed by a comma, as so:
#   * "A" for audio    (eg. media_dir=A,/var/lib/minidlna/music)
#   * "P" for pictures (eg. media_dir=P,/var/lib/minidlna/pictures)
#   * "V" for video    (eg. media_dir=V,/var/lib/minidlna/videos)
#   * "PV" for pictures and video (eg. media_dir=PV,/var/lib/minidlna/digital_camera)
media_dir=/var/lib/minidlna

# Set this to merge all media_dir base contents into the root container
# (The default is no.)
#merge_media_dirs=no

# Path to the directory that should hold the database and album art cache.
#db_dir=/var/cache/minidlna

# Path to the directory that should hold the log file.
#log_dir=/var/log

# Type and minimum level of importance of messages to be logged.
#
# The types are "artwork", "database", "general", "http", "inotify",
# "metadata", "scanner", "ssdp" and "tivo".
#
# The levels are "off", "fatal", "error", "warn", "info" or "debug".
# "off" turns of logging entirely, "fatal" is the highest level of importance
# and "debug" the lowest.
#
# The types are comma-separated, followed by an equal sign ("="), followed by a
# level that applies to the preceding types. This can be repeated, separating
# each of these constructs with a comma.
#
# The default is to log all types of messages at the "warn" level.
#log_level=general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn

# Use a different container as the root of the directory tree presented to
# clients. The possible values are:
#   * "." - standard container
#   * "B" - "Browse Directory"
#   * "M" - "Music"
#   * "P" - "Pictures"
#   * "V" - "Video"
#   * Or, you can specify the ObjectID of your desired root container
#     (eg. 1$F for Music/Playlists)
# If you specify "B" and the client device is audio-only then "Music/Folders"
# will be used as root.
#root_container=.

# Network interface(s) to bind to (e.g. eth0), comma delimited.
# This option can be specified more than once.
#network_interface=

# Port number for HTTP traffic (descriptions, SOAP, media transfer).
# This option is mandatory (or it must be specified on the command-line using
# "-p").
port=8200

# URL presented to clients (e.g. http://example.com:80).
#presentation_url=/

# Name that the DLNA server presents to clients.
# Defaults to "hostname: username".
#friendly_name=

# Serial number the server reports to clients.
# Defaults to the MAC address of nework interface.
#serial=

# Model name the server reports to clients.
#model_name=Windows Media Connect compatible (MiniDLNA)

# Model number the server reports to clients.
# Defaults to the version number of minidlna.
#model_number=

# Automatic discovery of new files in the media_dir directory.
#inotify=yes

# List of file names to look for when searching for album art.
# Names should be delimited with a forward slash ("/").
# This option can be specified more than once.
album_art_names=Cover.jpg/cover.jpg/AlbumArtSmall.jpg/albumartsmall.jpg
album_art_names=AlbumArt.jpg/albumart.jpg/Album.jpg/album.jpg
album_art_names=Folder.jpg/folder.jpg/Thumb.jpg/thumb.jpg

# Strictly adhere to DLNA standards.
# This allows server-side downscaling of very large JPEG images, which may
# decrease JPEG serving performance on (at least) Sony DLNA products.
#strict_dlna=no

# Support for streaming .jpg and .mp3 files to a TiVo supporting HMO.
#enable_tivo=no

# Which method to use for registering in TiVo: 'bonjour' (default) or
# legacy 'beacon'
#tivo_discovery=bonjour

# SSDP notify interval, in seconds.
#notify_interval=895

# Path to the MiniSSDPd socket, for MiniSSDPd support.
#minissdpdsocket=/run/minissdpd.sock

# Always set SortCriteria to this value, regardless of the SortCriteria
# passed by the client
# e.g. force_sort_criteria=+upnp:class,+upnp:originalTrackNumber,+dc:title
#force_sort_criteria=

# maximum number of simultaneous connections
# note: many clients open several simultaneous connections while streaming
#max_connections=50

# set this to yes to allow symlinks that point outside user-defined media_dirs.
#wide_links=no

冒頭の方でdebianの場合、man pageもreferしてみろ、と書いてあるので、念のために実行。

pi@raspberrypi:~ $ man minidlna

# 以下実行結果
SYNOPSIS
     minidlnad [-f config_file] [-d] [-v] [-u user] [-i interface] [-p port] [-s serial] [-m model_number] [-t notify_interval] [-P pid_filename]
               [-w url] [-S] [-L] [-R]

     minidlnad [-h | -V]

DESCRIPTION
     The minidlnad daemon is a DLNA/UPnP-AV server sharing media files (video, music and pictures) to clients on your network. Clients are typically
     multimedia players such as vlc, totem and xbmc, and devices such as portable media players, smartphones, televisions, video game entertainment sys‐
     tems and blu-ray players.

     By default, minidlnad listens on all the network interfaces (except loopback) for clients. This behavior can be changed on the command-line using
     the -i option, or in the configuration file through the network_interface option (see minidlna.conf(5) for details).

OPTIONS
     Most of the options below can also be set in a configuration file, as described in minidlna.conf(5).

     -d      Activate debug mode (do not daemonize).

     -f config_file
             Specify the location of the configuration file. Uses /etc/minidlna.conf by default.

     -h      Show help and exit.

     -i interface
             Network interface to listen on. Can be specified more than once.

     -L      Do not create playlists.

     -m model_number
             Model number the daemon will report to clients in its XML description.

     -P pid_filename
             PID file to use; the default is /run/minidlna/minidlna.pid.

     -p port
             Port number to listen on.

     -R      Forces a full rescan of the media files. First it will remove all cached data and database. Any bookmarks will be lost.

     -r      Do a non-destructive rescan of the media files on start-up.

     -S      Stay foreground. Can be used when minidlnad is being managed by systemd

     -s serial
             Serial number the daemon will report to clients in its XML description.

     -t notify_interval
             Notify interval, in seconds; defaults to 895 seconds.

     -u user
             Specify which user minidlnad should run as, instead of root; user can either be a numerical UID or a user name.

     -V      Show the program version and exit.

     -v      Verbose output.

     -w url  Sets the presentation url; the default is http address.

FILES
     /etc/minidlna.conf
             Default system-wide configuration file. See minidlna.conf(5) for details on the syntax.

SEE ALSO
     minidlna.conf(5)

使うコマンドはminidlna -Rくらいかな。ざっくりと骨格を掴んだところで、詳細を設定していこう。

DLNA用のフォルダーを設定

まずは共有用のフォルダを用意しよう。僕の場合ラズパイの外付けHDDは/media/pi/HD-3という階層にあるので、この下のpublicのディレクトリに専用のフォルダを用意する。

pi@raspberrypi:~ $ mkdir /media/pi/HD-3/public/dlna
pi@raspberrypi:~ $ mkdir /media/pi/HD-3/public/dlna/movies
pi@raspberrypi:~ $ mkdir /media/pi/HD-3/public/dlna/pictures
pi@raspberrypi:~ $ mkdir /media/pi/HD-3/public/dlna/audio
pi@raspberrypi:~ $ mkdir /var/log/minidlna

各ファイルタイプのフォルダとログを吐き出すためのフォルダを作成した。

DLNAのconfの設定

pi@raspberrypi:/ $ sudo vi /etc/minidlna.conf

メディアのフォルダの設定

30行目あたりのmedia_dirを先ほど作成したファイルに設定する。

# followed by a comma, as so:
#   * "A" for audio    (eg. media_dir=A,/var/lib/minidlna/music)
#   * "P" for pictures (eg. media_dir=P,/var/lib/minidlna/pictures)
#   * "V" for video    (eg. media_dir=V,/var/lib/minidlna/videos)
#   * "PV" for pictures and video (eg. media_dir=PV,/var/lib/minidlna/digital_camera)

#追加ここから
media_dir=APV,/media/pi/HD-3/public/dlna/movies
media_dir=P,/media/pi/HD-3/public/dlna/pictures
media_dir=A,/media/pi/HD-3/public/dlna/audio
#ここまで

moviesは全てのファイルフォーマットを配置できるようにしてみた。特に理由はない。

ログの設定

40行目あたりのlog_dirに先ほど作成したlogファイルのパスを指定する。

またログに記載する内容、レベルを56行目あたりのlog_levelで指定する。infoではかなり情報が出力されたので、warnくらいがちょうどいいと思う。

# Path to the directory that should hold the log file.

# 修正①ここから
log_dir=/var/log/minidlna
#ここまで

# Type and minimum level of importance of messages to be logged.
#
# The types are "artwork", "database", "general", "http", "inotify",
# "metadata", "scanner", "ssdp" and "tivo".
#
# The levels are "off", "fatal", "error", "warn", "info" or "debug".
# "off" turns of logging entirely, "fatal" is the highest level of importance
# and "debug" the lowest.
#
# The types are comma-separated, followed by an equal sign ("="), followed by a
# level that applies to the preceding types. This can be repeated, separating
# each of these constructs with a comma.
#
# The default is to log all types of messages at the "warn" level.

# 修正②ここから
log_level=general,artwork,database,inotify,scanner,metadata,http,ssdp,tivo=warn
#ここまで

※追記当初log_dir=/media/pi/HD-3/public/dlna/logとしていたのだが、rebootの際にHDDのマウントよりも先にログファイルを作成するため、piのディレクトリに"HD-3"のファイルが先に作成され、その後マウントされたHDDのファイルが"HD-31"になってしまう現象が発生した。→当然パスが変わるため、sambaなどで参照できなくなる。

log_dir=/var/log/minidlna

とすることで回避した。

自動更新の設定

100行目あたりのinotify=yesを有効にする。

# Automatic discovery of new files in the media_dir directory.
inotify=yes

DLNAのdefault設定

実行するユーザーを設定する。

pi@raspberrypi:/ $ sudo vi /etc/default/minidlna

rootユーザーを利用するようにする。

# User and group the daemon should run as
#USER="dlna"
#GROUP="dlna"
USER="root"
GROUP="root"

セキュリティ云々を心配する方はユーザーを作成し、そのユーザーで実行できるように設定が必要である。

動作確認

上記設定ができたところでminidlnaの再起動を行う。

pi@raspberrypi:/ $ sudo systemctl restart minidlna

そして実際にmoviesフォルダやpictureフォルダにファイルを配置してみよう。

webで確認

http://192.168.0.110:8200/にアクセスすることで、簡易的にminidlnaが認識しているファイル数を確認することができる。

20200809-1.png

ここでそもそもアクセスできない場合は、minidlnaが起動していないか、作成したフォルダにminidlnaがアクセスできないなどが考えられる。

TVで確認

これはお使いのTVごとに異なるので、調べて設定が必要。

僕の機種の場合は設定>ホームネットワーク設定>接続サーバー診断を行うと接続することができた。

20200809-2.jpg

▼実際に配置したmp4ファイルをTVで再生した図

20200809-3.jpg

スプラトゥーン楽しい。