[RaspberryPi] minidlna-transcode 설치

라즈베리 파이로 DLNA 뷰어를 통해 동영상을 보려니 코덱 문제로 음성이 안나오는 것들이 있어서 minidlna-transcode로 트랜스코딩을 시도해 보았다. 결론만 말하면 실패. 컴퓨팅 파워 문제인지 툭툭 끊긴다.

원리는 설정된 특정 코덱을 사용하는 동영상은 보내기 전에 설정한 스크립트에서 ffmpeg를 통해 인코딩을 해서 쏘는 것으로 보인다. 설치하다보니 Debian에서 ffmpeg가 avconv로 이름이 바뀌었다는 것도 알게 됐다.

minidlna는 라즈베리안의 경우 현재 1.1.2버전을 apt-get으로 그냥 설치할 수 있다. 트랜스코딩없이 쓴다면 그냥 패키지 설치하는게 맘 편할 수 있겠다. 내가 시도한 방법은 소스 컴파일 설치이다.

  1. 소스 컴파일 설치

먼저 git으로 clone해온다.

$ git clone -b transcode https://bitbucket.org/stativ/readymedia-transcode.git

컴파일에 필요한 패키지를 설치한다.

$ sudo apt-get install libexif-dev libjpeg-dev libid3tag0-dev libflac-dev libvorbis-dev libsqlite3-dev libavformat-dev libavutil-dev libavcodec-dev  libmagickwand-dev autoconf autopoint gettext libav-tools libav-dev mpv

아래 패키지는 이름이 그럴듯해서 그냥 설치해봤는데 확실치 않다. mpv의 경우, mencoder가 또 저걸로 바뀌었대서 깔았는데, mencoder를 이용하진 않아서 잘 모르겠다.

$ sudo apt-get install libavcodec-extra libavcodec-extra-56 mpv

컴파일한다.

$ ./autogen.sh
$ ./configure
$ make

그럼 다음과 같은 컴파일 에러가 난다.

minidlnad-upnphttp.o: In function `SendResp_dlnafile':
/home/pi/src/readymedia-transcode/upnphttp.c:2041: warning: the use of `tmpnam' is dangerous, better use `mkstemp'

tmpnam()이 위험하니까 mkstemp()를 이용하라는 거니까 찾아서 바꿔준다.

$ vi upnphttp.c

char tmp[L_tmpnam];
mkdtemp(tmp);

다시 컴파일하면 잘 된다. 설치를 sudo make install로 해줘도 되는데(사실 이렇게 한 후에 checkinstall을 사용했다-_-), 패키지를 만들어 설치하면 나중에 제거가 편하다. checkinstall이 없으면 설치(sudo apt-get install checkinstall)한 후에 아래 명령을 준다.

$ sudo checkinstall

아래와 같은 게 나오면 ‘Y’

아래와 같은게 나오면 난 MiniDLNA Version 2015-06-18 Compiled June 18, 2017 라고 입력했다.

그럼 아래와 같은게 나오는데, 여기서 3번, 버전만 숫자로 시작해야해서 아래처럼 2015-06-18 로 입력했다.

3 - Version: [ transcode ]
3 - Version: [ 2015-06-18 ]

 

2. 설정

이제 소스 내에 있는 설정 파일을 /etc/ 아래로 복사해서 설정을 시작하자.

$ sudo cp minidlna.conf /etc/minidlna.conf

sudo를 매번 입력하기 귀찮으니 sudo -i 를 입력해서 root shell로 작업하자.

# vi /etc/minidlna.conf

설정 파일에서 아래 “media_dir”, “friendly_name”을 각자 환경에 맞게 적자. “media_dir”은 예상하듯이, 미디어가 있는 경로, “friendly_name”은 DLNA 기기에서 나타나는 이름이다.

media_dir=V,/mnt/NAS/Videos
friendly_name=DasomOLI DLNA

“audio_codecs”나 “video_codecs” 에 적혀있는 코덱에 해당하는 미디어 파일이 재생되면, 그 아래의 “transcode_audio_transcoder”혹은 “transcode_video_transcoder” 에 적혀 있는 스크립트를  실행한다. 원하는 코덱을 transcoding하고 싶다면 해당 코덱을 적어주자. 어떤 것을 적어야 하는지는 avconv -formats 라고 입력하면 찾아볼 수 있다.

transcode_audio_codecs=flac/vorbis/dts
transcode_audio_transcoder=/usr/local/share/minidlna/transcodescripts/transcode_audio
transcode_video_transcoder=/usr/local/share/minidlna/transcodescripts/transcode_video

transcoding 스크립트는 /usr/local/share/minidlna/transcodescripts/ 아래에 있다. 옵션을 변경하고 싶다면 해당 스크립트를 수정한다. 옵션을 변경하지 않더라도 수정을 해주어야 하는데 ffmpeg 대신 avconv를 사용해야 하기 때문이다.

# vi /usr/local/share/minidlna/transcodescripts/transcode_audio

#!/bin/sh

SOURCE=$1
STARTPOSITION=$2
DURATION=$3

#ffmpeg -ss $STARTPOSITION -t $DURATION -i "$SOURCE" -loglevel quiet -acodec pcm_s16le -f s16le -ar 44100 pipe:1
avconv -ss $STARTPOSITION -t $DURATION -i "$SOURCE" -loglevel quiet -acodec libmp3lame -f mp3 -ar 44100 -ab 224k pipe:1

# vi transcodescripts/transcode_video

#!/bin/sh

SOURCE=$1
STARTPOSITION=$2
DURATION=$3

avconv -ss $STARTPOSITION -t $DURATION -i "$SOURCE" -loglevel quiet -threads auto -async 2 -target pal-dvd pipe:1

# vi transcodescripts/transcode_video-hq

#!/bin/sh

SOURCE=$1
STARTPOSITION=$2
DURATION=$3

avconv -ss $STARTPOSITION -t $DURATION -i "$SOURCE" -loglevel quiet -threads auto -async 2 -vcodec mpeg2video -b:v 20000k -f mpegts pipe:1

위와 같이 ffmpeg가 적힌 곳에 avconv를 써주면 된다.

이제 실행하려면 다음과 같이 한다. -R 옵션은 DB update를 해주는 거니까 매번 하진 말자.

/usr/local/sbin/minidlnad -R -f /etc/minidlna.conf

 

3. 자동 실행

매번 위처럼 실행하긴 귀찮으니까, 부팅될 때마다 자동 실행 되도록 하자. /etc/init.d/안에 minidlna 파일을 새로 생성해서 아래와 같이 내용을 채운다. Process가 2개 되는 경우가 보여서 참고로 보았던 글에 있는 스크립트를 좀 수정했다.  잘안되면, 뭐 또 수정해야지..

# vi /etc/init.d/minidlna

#!/bin/bash
# Mini DLNA
### BEGIN INIT INFO
# Provides:          scriptname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

case "$1" in
'start')
        /usr/local/sbin/minidlnad -f /etc/minidlna.conf
        echo Started
        ;;
'stop')
        PIDS=`/bin/pidof minidlnad`
        if [ "${PIDS}" != "" ];
        then
                for PID in ${PIDS};
                do
                        kill -SIGTERM ${PID}
                done
        else
                echo Already Stopped
        fi
        ;;
'restart')
        PIDS=`/bin/pidof minidlnad`
        if [ "${PIDS}" != "" ];
        then
                for PID in ${PIDS};
                do
                        kill -SIGTERM ${PID}
                done
        fi
        /usr/local/sbin/minidlnad -f /etc/minidlna.conf
        echo Restarted
        ;;
'status')
        PID=`/bin/pidof minidlnad`
        if [ "${PID}" != "" ];
        then
                echo Running. Process ${PID}
        else
                echo Stopped
        fi
        ;;
'rescan')
        PIDS=`/bin/pidof minidlnad`
        if [ "${PIDS}" != "" ];
        then
                for PID in ${PIDS};
                do
                        kill -SIGTERM ${PID}
                done
        fi
        /usr/local/sbin/minidlnad -R -f /etc/minidlna.conf
        echo Rescanning
        ;;
*)
        echo "Usage: $0 { start | stop | restart | status | rescan }"
        ;;
esac
exit 0

위 파일에 실행 권한을 주자.

# chmod a+x /etc/init.d/minidlna

실행되도록 rc.d를 업데이트 하자.

# sudo update-rc.d minidlna defaults

 

참고:

https://www.htpcbeginner.com/install-minidlna-on-ubuntu-ultimate-guide/

[RaspberryPi] Torrent 머신 만들기

Torrent 파일을 등록해놓으면, 집에 있는 서버에 해당 파일을 자동으로 다운로드 받아놓는다. transmission을 설치하면 된다.

# apt-get install transmission-cli transmission-common transmission-daemon
# service transmission-daemon stop
# vi /etc/transmission-daemon/settings.json

설정 파일의 다음 항목을 수정한다.

"download-dir": "/mnt/NAS/TorrentDownload",
"rpc-username": "dasomoli",
"rpc-password": "password",
"rpc-whitelist-enabled": false,
"umask": 2,

업로드 속도를 제한하고 싶다면 다음을 수정한다. 예를 들어, 10 KB/sec 로 제한한다면

"speed-limit-up": 10,
"speed-limit-up-enabled": true,

기본 포트를 바꾸고 싶다면 다음을 수정한다.

"rpc-port": 9000

# service transmission-daemon reload
# usermod -aG debian-transmission dasomoli

download-dir은 다운로드 받을 디렉토리, rpc-username은 웹 인터페이스로 들어갈 때 로그인 할 ID, rpc-password는 그 Password이다. rpc-whitelist-enabled를 false 로 설정하면, 말 그대로 whitelist를 사용하지 않으므로, 어디에서든 접속이 가능하다.

접속은 기본은 9091 포트로 접속하면 된다. 위에선 9000으로 바꿨다. 하지만, 포트 번호를 치긴 귀찮으므로 apache2의 proxy를 이용해서 접속하자.

# vi /etc/apache2/sites-available/torrent.dasomoli.org.conf

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName torrent.dasomoli.org
        ServerAdmin dasomoli@gmail.com

        ProxyPass / http://localhost:9000/
        ProxyPassReverse / http://localhost:9000/

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/torrent.log
        CustomLog ${APACHE_LOG_DIR}/torrent.access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

이제 proxy 모듈을 enable하고, site를 enable한 후, apache2를 재시작하자.

# a2enmod proxy
# a2enmod proxy_http
# a2ensite torrent.dasomoli.org.conf
# service apache2 restart

# sudo service transmission-daemon start

이제 http://torrent.dasomoli.org 로 접속하면 된다.
당연히 미리 DNS설정은 되어 있어야 한다.

부팅 시에 Web UI 가 연결이 안되는 경우가 있는데, 이 때는 transmission-daemon을 reload 하면 된다. 수많은 해결(이라고 주장하는) 방법이 있지만, 가장 간단하면서 잘 동작하는 방법은 그냥 raspi-config에서 “Network at Boot:”의 “Wait for network”을 체크하는 것이다.

가장 간단하지만 잘 동작하는 허무한 해결책…

참고: https://askubuntu.com/questions/157455/how-to-change-the-default-permissions-of-files-created-by-transmission-daemon

[RaspberryPi] NAS 설치 시 참고글

NAS를 어떻게 구축할까, OpenMediaVault를 쓸까, Raspbian 위에 그냥 만들까, 그러려면 뭘 깔아야 하나 찾다가 찾은 글.

http://www.spacek.xyz/mle/?p=357

뭘 설치했는지 꽤 잘 나와 있다. 좀 해보다 별로면 OpenMediaVault로 이미지 바꿔보지 뭐.

쓸만한 글을 찾을 때마다 이 글을 업데이트하겠다.

 

드디어 왔다. 8TB HDD

쉬는 날엔 역시 프로자랑꾼 놀이~
드디어 왔다. 내 첫 직구 8TB 하드! 안에 든 하드는 WD의 NAS용 RED 등급.

따로 전원 들어가는 USB 허브를 따로 사야 하나 했더니 12V 1.5A 어댑터가 같이 있네? ㅋ

라즈베리파이에 NAS 고고~