Setting up MiniDLNA on a Raspberry Pi

If you’re familiar with DLNA, which is a media streaming protocol that lots of devices support (PC, games consoles, smart tv’s and tablets and cellphones), you can probably guess what MiniDLNA is, and what it does.

I have a personal OwnCloud instance running on a Raspberry Pi, and I use it as you would use iCloud or Google Drive etc. My phone automatically uploads videos and pictures I take to my home server. When I’m home, I can watch the videos or pictures via my Smart TV thanks to installing MiniDLNA on the Raspberry Pi.

Here’s how to do it… (Assuming you’re using Raspbian, though commands should be similar for other distros).

First, Update and Upgrade

~# sudo apt-get update && sudo apt-get upgrade

You should always make sure your Pi’s up to date before doing anything.

Install MiniDLNA

~# sudo apt-get install minidlna

Edit the MiniDLNA config

~# sudo nano /etc/minidlna.conf

Tell MiniDLNA where to find your Audio, Videos and Photos. This can either be a single directory, or you can specify each directory type.

Find #media_dir=/var/lib/minidlna

Leave the line commented if you have different directories. For specifying different directories, enter the following after the commented line:
media_dir=A,/path/to/your/audio/files
media_dir=V,/path/to/your/video/files
media_dir=P,/your/photo/collection/path

It would be a very good idea not to have MiniDLNA save the media database and logs to the SD card if you’re using it, in case you ever have SD failures. I have a USB storage disk mounted at /media/usb_storage1 (you need to read another guide about mounting usb drives).

If you have another storage disk to save to, find the following line:

#db_dir=/var/cache/minidlna

And add after it:

db_dir=/your/storage/path/minidlna/db

Find #log_dir=/var/log

And add after:

log_dir=/your/storage/path/minidlna/log

It’s probably worth setting a better name for the MiniDLNA player

Find friendly_name=

and give it a name –  eg:

friendly_name=Serenity

The Friendly Name will be what you see on your DLNA client – so in this instance, you would see “Serenity” as the name of the server. Because this is MiniDLNA, the icon for the media player will be the default system icon (the Debian swirl on Raspbian Wheezy/Jessie). Changing the DLNA image is not easy to do and currently involves recompiling all sorts of bits. If you really must change the icon – you’d better do a bit of Googling for instructions and prepare for a long night in front of the keyboard!

Take a look at some of the other settings, but you shouldn’t need to change them unless you feel a need to.

Once you have saved the config, make sure the directories created for database and logs exist (minidlna may not be able to create them).

~# sudo mkdir -p /your/storage/path/minidlna/db
~# sudo mkdir -p /your/storage/path/minidlna/log
~# sudo chown minidlna:minidlna -R /your/storage/path/minidlna

Make sure MiniDLNA is installed to start with the system

~# sudo update-rc.d minidlna defaults

The above line should add minidlna to the startup logic.

Enable Crontab re-indexing

MiniDLNA has an ability to set watches on your content directories – but the limited nature of the Pi can greatly reduce performance when this is enabled. So I have my Pi set to re-index on a cron schedule. This does mean if you add photos, video or audio that it won’t appear in the DLNA catalog until you either run a re-index manually or the cron event triggers, but it does reduce the performance drain on your Pi of it constantly monitoring files and folders for changes.

~# sudo EDITOR=nano crontab -e

Add the following lines to your crontab file:

# MiniDLNA scheduled restart and re-index routines
0 4 * * * sudo service minidlna restart >/dev/null 2>&1
0 */1 * * * sudo /usr/bin/minidlnad -R >/dev/null 2>&1

The first cron line (starting ‘0 4’) will trigger a service restart of MiniDLNA at 04:00 hours every day. As you’re unlikely to be watching anything at this hour, it’s a good time for a service restart. I’ve found setting an enforced service restart helps clear down the service and clears sticky sessions with devices – generally improving system performance.

The second line (starting ‘0 */1’) will trigger a content re-index once every hour, on the hour. You can adjust this to how ever frequent you want this to be – I personally have it set to once every 12 hours as I don’t regularly use my Pi for media playing.

Make sure any iptables firewall rules are in place

If you use iptables then you’ll need to open DLNA ports (if you’re not running a firewall, consider setting up iptables). This guide will assume you have a firewall rules file for iptables in your /etc directory…

~# sudo nano /etc/your.iptables.firewall.rules

Add a code block similar to the following, which should allow DLNA usage on your LAN only (you really shouldn’t be streaming to the Internet). Make sure your local network mask is set correctly. Change 192.168.x.0 to match your local network mask (usually 192.168.0.0 or 192.168.1.0 – but your network mask may differ).

# Allow MiniDLNA only on LAN
-A INPUT -p tcp -m tcp -s 192.168.x.0/24 –dport 8200 -j ACCEPT
-A INPUT -p udp -m udp -s 192.168.x.0/24 –dport 8200 -j ACCEPT
-A INPUT -p tcp -m tcp -s 192.168.x.0/24 –dport 1900 -j ACCEPT
-A INPUT -p udp -m udp -s 192.168.x.0/24 –dport 1900 -j ACCEPT

Save your changes and reload your iptables rules

~# sudo iptables-restore < /etc/your.iptables.firewall.rules

Start (or Restart) MiniDLNA

~# sudo service minidlna restart

Test out your MiniDLNA Webpage

There isn’t much to the page, but it will show DLNA devices detected, and the amount of content indexed into the DLNA database.

http://192.168.x.x:8200/

Remember to change  .x.x to the IP of your PI.

If there are no files showing indexed, back in the bash prompt run:

~# sudo /usr/bin/minidlnad -R

This should trigger the re-indexing, and after a minute or two you should be able to refresh the webpage and see values recorded. If you don’t see anything – check your logs in the path you defined in the settings file.

Change user/group permissions if needed

By default, minidlna will run as itself (minidlna user, minidlna group). If your audio/video/photo content belongs to a different user/group, you should add minidlna to the group that can access the content.

On my Pi the OwnCloud data belongs to a group called ‘owncloud’, so I had to add minidlna to the membership with:

usermod -a -G owncloud minidlna

Once this was done, MiniDLNA’s re-index command was able to read content and I can now watch my home videos and browse holiday photos with ease.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.