Stream media from your Raspberry Pi to tablets, consoles, and other devices using MiniDLNA.
Hardware
- Raspberry Pi (preferably running Raspbian)
- Stonking great USB hard drive full of music, movies, and other goodies.
Why this rocks
- Because you won't have to lug around your stonking great hard drive all over your house.
- Because you'll have access to your media library from all devices.
- Because the Raspberry Pi runs silent. No fan noises.
Assumed knowledge
- You know how to access the terminal on your Raspberry Pi, either through the GUI, or through SSH on a remote machine.
- Your Raspberry Pi can connect to your home network.
- You have plugged your USB drive into your Raspberry Pi.
Step: Auto-mount your External USB drive
Raspbian's window system might already auto-mount your USB drive. I.e. an icon for the drive might appear on your desktop. Take it from me though, this can cause a lot of hassle later as the window system tends to mount the drive after the media service is started, which means DLNA can't find the drive.
You want to configure Raspbian to auto-mount the drive before its window system starts. In short, this involves:
Creating a directory as a mount point, usually something like sudo mkdir /mnt/HDD
Finding the partition ID you want to mount with sudo blkid
Adding an entry to the /etc/fstab
file with sudo pico /etc/fstab
Because I have an NTFS drive, I added the following line to my /etc/fstab file
PARTUUID=5dd83921-01 /mnt/HDD ntfs-3g defaults,nls=utf8,nofail,comments=x-gvfs-show 0 0
Your configuration will almost certainly be different. I thoroughly recommend this article for more information on fstab, although I didn't follow it exactly.
The official documentation is very useful too.
Step: Install MiniDLNA
In terminal:
sudo apt install minidlna
Step: Edit /etc/minidlna.conf
This is the standard location of MiniDLNA's configuration settings. To edit them, you can use:
sudo pico /etc/minidlna.conf
All you need is 2 settings:
1. Set user
user=pi
Why? Because your HDD is likely mounted under the default user account, 'pi', it's easiest to run MiniDLNA as the same user.
(The created account minidlna doesn't have access to external drives.)
2. Specify files and folders
Unfortunately, this means that you'll need to create a writeable location for the database and log files. So in a terminal, create a directory in your home folder. e.g.:
mkdir /home/pi/minidlna
and change db_dir
and log_dir
in minidlna.conf
to use it.
Next, set the media folders in the config file. They will most likely be at paths like:
/mnt/HDD/Music
Tip: If you have multiple folders, just specify the smallest folder at this stage to make testing easier.
Test manual start
In terminal:
minidlnad
Crank up a media player on another device connected to the same network (e.g. VLC).
Step: Configure Autostart
This is the hardest part.
MiniDLNA used to start at boot, but Raspbian now uses a different method to auto-start (systemd instead of init) and MiniDLNA has not adapted its startup scripts.
You don't want to have to manually start MiniDLNA every time you start your Raspberry Pi, so we need to write a systemd service for it and get it to run automatically.
Create the service file, give it the correct permissions, and edit it:
sudo touch /etc/systemd/system/minidlna.service
sudo chmod a+rx /etc/systemd/system/minidlna.service
sudo pico|leafpad /etc/systemd/system/minidlna.service
Here's what's in mine: ('#' denotes comments and can be omitted)
Description=minidlna, DLNA/UPNP-AV media server
RequiresMountsFor=/mnt/HDD
[Service]
Type=simple
# The -S flag runs minidlnad for systemd
ExecStart=/usr/sbin/minidlnad -S
# Run as default user 'pi'
User=pi
[Install]
WantedBy=multi-user.target
Every time you edit a systemctl service you need to reload it:
sudo systemctl daemon-reload
Test systemctl service start
Stop all minidlna processes and test starting via systemd:
sudo killall minidlna
sudo systemctl restart minidlna.service
Wait for a while, and you should be able to access your media library again. Now you can enable the service to be processed at startup:
sudo systemctl enable minidlna.service
Reboot, and MiniDLNA should auto-start!
Appendix
Useful systemctl commands
- [sudo] systemctl start|stop|restart <minidlna.service>
- [sudo] systemctl enable|disable <minidlna.service> (enable/disable autostart)
- [sudo] systemctl status <minidlna.service> (useful troubleshooting information).
Further Reading
- Raspberry Pi Media Server - MiniDLNA (Instructables.com) - My primary reference. Unfortunately does not cover systemd.
- Systemd (RaspberryPi.org) - How to create a new systemd service.
I had an error after "sudo systemctl restart minidlna.service" which seemed to indicate that the PID file could opened to be written to. Any thoughts?
ReplyDeleteTry changing the owner of the PID file with the 'chown' command. In the example above, I would change the owner to user 'pi'. Or delete the PID file altogether and let it be created again by your new configuration.
DeleteThanks for your prompt reply. I could not find minidlna.pid. Changing ownership of minidlna.service did not help either. Following the instructions again (using copy and paste) I noticed that after "killall ..." I got "no process found". I tried the restart command and got no message but checking status revealed the same error regarding .pid. I then deleted minidlna.service, rebooted and discovered that minidlna was running anyway - I believe this is because there is an entry for it in init.d. As I am not skilled in Unix and the service is working I am happy to leave as it is. However if you wish to pursue in case your post needs to be changed I can supply daemon and minidlna log files if you email me and will be happy to try any changes you suggest. Thanks.
ReplyDeleteWhat's most important is that you got it working for yourself. Well done! On my Raspbian-Pi, systemd replaced start-stop-daemon (init.d) so I had to work out how to get minidlna working with systemd. If init.d is not broken for you, don't fix it.
DeleteOK. Thanks for the help and post.
ReplyDelete