Monday, January 25, 2010

DLNA on Sony Bravia TVs

I have a Sony Bravia TV which is DLNA-capable. So i tested this feature this week-end, but is is really complicated to find a working solution.

They are many Media Servers enabling streaming video for DNLA devices; much are for Windows, and much does not support transcoding, a useful feature that enabled you to convert on the fly a video type into another video type. This blog entry shows many DLNA softwares, and i've tried Nero, Fuppes, Media Tomb, Mezzmo, MiniDLNA, PS3 Media Server, Twonkey and one that is not listed but looks very promising: Wild Media Server.

Since i needed transcoding features to stream some xvid videos into my TV, i eliminated a few of them plus those really complicated to setup like Media Tomb. After having tried them all, the only software capable of streaming divx files on my TV was Mezzmo and Wild Media Server. It is great, but since i run Linux and not Windows, I wanted a Linux solution.

MiniDLNA is very simple and easy to configure and works great with Sony TVs, and it works on LInux and Windows... As long as you only stream DVD files (since my Sony TV only accepts MPEG-2 videos)... MiniDLNA does not support transcoding, so I applied the same idea as Wild Media Server: before launching the Media Server, start the transcoding and stream the transcoded file. With FFMPEG, available on both Windows and Linux, you can transcode a video file. So the trick is to find good parameters so that the transcoding process is faster than the streaming, considering that the DLNA communication between my computer and my TV is wireless.

I finally made a Groovy Script working on Linux (and also can work on Windows too) which uses both FFMPEG and MiniDLNA.
The script requires that you have ffmpeg, minidla and Groovy installed and on your PATH.
You can launch the script like this:

dlna.sh /path/to/your/divx.avi

The script will compute the parameters for a 16:9 aspect ratio, adding padding on top and bottom if necessary. Then it will start the transcoding process to convert the movie to an MPEG-2 video with AC3 sound. Then the script will starts 5 seconds after the MiniDLNA server, pointing to the resulting video. You'll see something like this on the console after the startup:

kha@box:~/workspace/perso/mycila-ujd$ dlna.sh /media/lacie/My\ movie.avi
/media/lacie/My\ movie.avi:
- video bitrate: 1050 kb/s
- video size: 640x272 (40:17 => 2.3529413)
- audio freq: 48000 Hz
- audio bitrate: 163 kb/s
- padding top: 44 px
- padding bottom: 44 px
- dest size: 640x360 (16:9 => 1.7777777)
- threads: 2

Starting...
[minidlna, -d, -f, /unsafe/tmp/minidlna/minidlna.conf, -P, /unsafe/tmp/minidlna/minidlna.pid]
[ffmpeg, -i, /media/lacie/My\ movie.avi, -y, -threads, 2, -target, ntsc-dvd, -b, 4000k, -maxrate, 5000k, -bufsize, 2000k, -sameq, -qscale, 1, -s, 640x272, -aspect, 16:9, -padtop, 44, -padbottom, 44, -ac, 2, -ab, 192000, /unsafe/tmp/minidlna//media/lacie/My\ movie.avi.mpg]

DNLA started !
Press a key to exit...

You then just have to open your TV menu and open the movie !

23 comments:

  1. Cool to use a Groovy script :-)
    I've just acquired a Sony Bravia, and was wondering how to stream my pictures from my Mac... wondering if there's a good DLNA solution for Mac OS X!
    ReplyDelete
  2. Yeah I think for us Java guys writing Groovy scripts is faster than writing these obscure Linux shells always requiring searching on Google when you want to do really simple text processing ;)
    ReplyDelete
  3. Any ideas on configuring minidlna on freenas, so that I can get my freenas box working on my bravia? The built in UPnP service won't be supported by Sony!
    ReplyDelete
  4. Thanks for sharing! Looks very promising.
    Unfortunately, according to my Java environment, 1629 is not an integer...

    Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1629' with class 'java.lang.String' to class 'java.lang.Integer'
    at dlna.run(dlna.sh:10)
    at dlna.main(dlna.sh)


    I've set JAVA_HOME to /usr/lib/jvm/java-6-sun/

    Any suggestions?
    Thanks again.
    ReplyDelete
  5. I really don't know why... Groovy is able to cast integer strings as int. It have this versions:
    > groovy --version
    Groovy Version: 1.7.0 JVM: 1.6.0_16
    ReplyDelete
  6. Hm.

    > groovy --version
    Groovy Version: JVM: 10.0-b22

    ?
    Cheers
    PVR
    ReplyDelete
  7. Strange! There is no groovy version and the JVM version does not seem to match the one installed on your computer... Are you sure your environment settings and installations are ok ? It much seems to be an issue concerning your setup...
    ReplyDelete
  8. ... Maybe I should point out the machine is still a 8.04 Ubuntu.
    I just apt-get installed groovy and exported the JAVA_HOME variable. Any more variables I should attend to?
    Thanks so far Mathieu.
    ReplyDelete
  9. I have a GROOVY_HOME, JAVA_HOME also. JAVA_HOME points to my JDK 6 and GROOVY_HOME points to /opt/groovy: i just downloaded the groovy zip file and extracted it.
    I also have my PATH containing the binary folder $GROOVY_HOME/bin.
    Mathieu.
    ReplyDelete
  10. Hi. I made all things described here, but after i started dlna.sh i got that information on my tv's screen:
    "Server temporairly unavailable"


    whats wrong??
    ReplyDelete
  11. I got this error too:

    Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '1629' with class 'java.lang.String' to class 'java.lang.Integer'
    at dlna.run(dlna.sh:10)
    at dlna.main(dlna.sh)

    Solved it by changing script lines 10,11,12,14 and 15 to '[...] as BigInteger'.

    Now it works!
    ReplyDelete
  12. Hi,
    Got this error

    Caught: java.lang.IndexOutOfBoundsException: index is out of range 0..-1 (index = 0)
    at dlna.run(dlna.sh:15)

    Any ideas?
    ReplyDelete
  13. Also, after editing script from 'int' to 'BigInteger':

    still fails:
    Caught: java.lang.IndexOutOfBoundsException: index is out of range 0..-1 (index = 0)
    at dlna.run(dlna.sh:11)
    ed@ed-asus:~$ groovy --version
    Groovy Version: 1.6.4 JVM: 1.6.0_22

    Thanks for any ideas and for your efforts so far
    ed
    ReplyDelete
  14. your groovy version might be too old
    ReplyDelete
  15. Mathieu,
    Thanks for speedy reply
    installed newer version, but still get error:

    ed@ed-asus:~$ groovy --version
    groovy: JAVA_HOME not set and cannot find javac to deduce location, please set JAVA_HOME.
    ed@ed-asus:~$ export JAVA_HOME=/usr
    ed@ed-asus:~$ groovy --version
    Groovy Version: 1.7.6 JVM: 1.6.0_22
    ed@ed-asus:~$ dlna.sh /home/ed/Videos/dlna/archieplay.MOV
    Caught: java.lang.IndexOutOfBoundsException: index is out of range 0..-1 (index = 0)
    at dlna.run(dlna.sh:11)
    ed@ed-asus:~$
    ReplyDelete
  16. Hi,

    I do not support groovy and its installation on Linux - please see in Groovy mailing list or help.

    I'm pretty sure your java home is not in /usr ;-)

    For me on ubuntu my JAVA_HOME is /usr/lib/jvm/default-java
    ReplyDelete
  17. Hi again.

    Thanks for the suggestion, and I recognise you're not official support, etc. But....

    ed@ed-asus:~$ export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.22
    ed@ed-asus:~$ dlna.sh /home/ed/Videos/dlna/archieplay.MOV
    Caught: java.lang.IndexOutOfBoundsException: index is out of range 0..-1 (index = 0)
    at dlna.run(dlna.sh:11)
    ed@ed-asus:~$ export JAVA_HOME=/usr/lib/jvm/java-6-sun/

    ed@ed-asus:~$ dlna.sh /home/ed/Videos/dlna/archieplay.MOV
    Caught: java.lang.IndexOutOfBoundsException: index is out of range 0..-1 (index = 0)
    at dlna.run(dlna.sh:11)

    ed@ed-asus:~$ echo $GROOVY_HOME

    ed@ed-asus:~$ export GROOVY_HOME=/usr/bin
    ed@ed-asus:~$ echo $GROOVY_HOME
    /usr/bin
    ed@ed-asus:~$ dlna.sh /home/ed/Videos/dlna/archieplay.MOV
    Caught: java.lang.IndexOutOfBoundsException: index is out of range 0..-1 (index = 0)
    at dlna.run(dlna.sh:11)
    ed@ed-asus:~$

    Still puzzled, but keeping at it...
    thanks again
    ed
    ReplyDelete
  18. The problem with "index is out of range" at dlna.sh:11 caused by failing regexp. Apparently ffmpeg output format varies between systems/time. In my case (Ubuntu 10.10) it had extra commas in video dimensions output, and had no "DAR" at all. But the fix for this is obvious:

    --- dlna.sh.orig 2011-02-04 23:16:06.568135998 -0800
    +++ dlna.sh 2011-02-04 23:18:31.468136009 -0800
    @@ -8,9 +8,9 @@
    def rawInfos = ["ffmpeg", "-i", "${args[0]}"].execute().err.text

    def video_bitrate = (rawInfos =~ /bitrate:\s(\d+)\skb/)[0][1] as int
    -def video_width = (rawInfos =~ /.*Video.*\s(\d+)x\d+\s/)[0][1] as int
    -def video_height = (rawInfos =~ /.*Video.*\s\d+x(\d+)\s/)[0][1] as int
    -def video_dar = (rawInfos =~ /.*Video.*\sDAR\s(\d+:\d+)/)[0][1]
    +def video_width = (rawInfos =~ /.*Video.*\s(\d+)x\d+,\s/)[0][1] as int
    +def video_height = (rawInfos =~ /.*Video.*\s\d+x(\d+),\s/)[0][1] as int
    +def video_dar
    def audio_freq = (rawInfos =~ /.*Audio.*\s(\d+)\sHz/)[0][1] as int
    def audio_bitrate = (rawInfos =~ /.*Audio.*,\s(\d+)\skb/)[0][1] as int
    def ratio = (float) video_width / (float) video_height

    I had also to replace all the /unsafe/tmp paths with /tmp in order to make script work. But even with all things running I can't get the video on my Bravia: it gets me "can't access media server" message. I guess I have to dig into minidlna to figure it out.

    But thanks for a this good post and script, it really helps to start looking in the right direction in all this DLNA mess.
    ReplyDelete
  19. Hello. I've tried your script (I have kubuntu 10.10 and a Sony Bravia KDL-40NX705). It executes showing no errors at all. When I browse the tv it even shows the minidlna icon. However it doesn't seem to find the video. There's nothing when I browse my "videos" folder.
    ReplyDelete
  20. Do you have a look at this DLNA Server : Serviio.org

    Very fast FREE server, transcodes in the fly, supports lots of formats, very compatible with Sony Device
    ReplyDelete
  21. No... The downloads do not work at all... They don't start :-(
    ReplyDelete
  22. thank you. Question: my nokia n900 has minidlna up and running to my sony media player smp n100. Can I use the groovy script to watch movies from my n900? Music works great. Pictures and movie files are not recognized.
    ReplyDelete
  23. PS3 media server. DONE.
    ReplyDelete