The Humble Geek

Stuff I write.

Crossing the Streams to the Playstation 3 (part 2)

Lets say you have downloaded some TV shows. I won't ask where you got them, but I will show you how to play them on the PS3 through Mediatomb. This will require the patches mentioned in the part 1 post as video playing is pretty stupid without them.

First off, Mediatomb needs to know what to do with .mkv files on your computer. The PS3 has no idea how to play Matroska containers unfortunately. It would be a simple and free (as in speech) for Sony to implement, but that's too hard for them to do without money or IP rights in their favor. The following settings will tell Mediatomb to stream MKV files as MPEG files and use ffmpeg to transcode.

/etc/mediatomb/config.xml

<extension-mimetype ignore-unknown="no">
    <map from="mkv" to="video/x-matroska"/>
    ... other maps already exist, don't delete them ...
</extension-mimetype>
... skip down to the <profiles> tag and add this block...
<profile name="video-matroska" enabled="yes" type="external">
    <mimetype>video/mpeg</mimetype>
    <accept-url>no</accept-url>
    <first-resource>yes</first-resource>
    <hide-original-resource>yes</hide-original-resource>
    <agent command="ffmpeg-tr" arguments="%in %out %range" />
    <buffer size="10485760" chunk-size="524288" fill-size="0"/>
</profile>

The following are a few "ffmpeg-tr" script file examples. Ffmpeg has a few options as I have come to find out that the PS3 is a bit picky at video encodings. If you do not have a flawlessly encoded file the PS3 will most likely not play the file.

The first example will require a fast machine (at least 4 cores) as it reencodes the video into a fresh H.264 stream and leaves the audio intact. Use this if the video file you have is skipping or not even playing on the PS3.

/usr/local/bin/ffmpeg-tr

#!/bin/bash

START_SECONDS=`echo "$3" | awk '{split($0,a,"-"); print a[1]}'`
MILLISECONDS=`echo "$START_SECONDS" | awk '{split($0,a,"."); print a[2]}'`
START_TIME=`echo $START_SECONDS | awk '{print strftime("%H:%M:%S", $0,1)}'`
START="$START_TIME.$MILLISECONDS"
END_SECONDS=`echo "$3" | awk '{split($0,a,"-"); print a[2]}'`
if [ -z $END_SECONDS ]
then
MILLISECONDS=`echo "$END_SECONDS" | awk '{split($0,a,"."); print a[2]}'`
END_TIME=`echo $END_SECONDS | awk '{print strftime("%H:%M:%S", $0,1)}'`
END="$END_TIME.$MILLISECONDS"
fi

exec /usr/bin/ffmpeg -threads 4 -r 24000/1001 -i "$1" -vcodec libx264 -b 8000k -preset faster -level 41 -r 24000/1001 -vsync 1 -acodec copy -async 1 -f mpegts - > "$2"

If your video file is in good shape, then you can get away without encoding anything. Most .mkv files are already in video formats the PS3 will play (h.264 video, AC3/Dolby audio) so you can get away with a direct copy of the data. This requires very little computing power so any computer can do this.

/usr/local/bin/ffmpeg-tr

#!/bin/bash

START_SECONDS=`echo "$3" | awk '{split($0,a,"-"); print a[1]}'`
MILLISECONDS=`echo "$START_SECONDS" | awk '{split($0,a,"."); print a[2]}'`
START_TIME=`echo $START_SECONDS | awk '{print strftime("%H:%M:%S", $0,1)}'`
START="$START_TIME.$MILLISECONDS"
END_SECONDS=`echo "$3" | awk '{split($0,a,"-"); print a[2]}'`
if [ -z $END_SECONDS ]
then
MILLISECONDS=`echo "$END_SECONDS" | awk '{split($0,a,"."); print a[2]}'`
END_TIME=`echo $END_SECONDS | awk '{print strftime("%H:%M:%S", $0,1)}'`
END="$END_TIME.$MILLISECONDS"
fi

exec /usr/bin/ffmpeg -threads 4 -i "$1" -vcodec copy -vbsf h264_mp4toannexb -acodec copy -copyts -copytb -f mpegts - > "$2"

You can also mix and match between re-encoding audio only or both video and audio.

Crossing the Streams to the Playstation 3 (part 1)

I wish I didn't have to write this article, but when there's a dozen audio formats, a dozen video formats, and a dozen media containers there's only one result: headaches. If you own a Playstation 3, a Linux computer, and have Mediatomb installed, you can take advantage of the UPnP feature on the PS3 to play your audio or video over the network. This part 1 of 2 posting will start with audio.

FLAC to PCM

The Playstation 3 is a funny thing when it comes to audio. If you only have Optical (TosLink) or Coax audio output you're stuck with 48kHz sample rate. If you have HDMI you can go higher. The example below will get you FLAC transcoding into 48kHz PCM that the Playstation 3 will play.

/etc/mediatomb/config.xml

<profile name="audio-flac" enabled="yes" type="external">
   <mimetype>audio/L16</mimetype>
   <accept-url>no</accept-url>
   <first-resource>yes</first-resource>
   <hide-original-resource>yes</hide-original-resource>
   <accept-ogg-theora>no</accept-ogg-theora>
   <sample-frequency>48000</sample-frequency>
   <audio-channels>2</audio-channels>
   <agent command="ffmpeg-flac" arguments="%in %out" />
   <buffer size="4194304" chunk-size="262144" fill-size="0"/>
</profile>

/usr/local/bin/ffmpeg-flac

#!/bin/bash

exec /usr/bin/ffmpeg -threads 2 -i "$1" -ar 48000 -acodec pcm_s16be -f alaw - > "$2"

Why 48kHz? I have some 96kHz media so I'd rather it go to the PS3 at the best possible rate. The PS3 will resample to 48kHz if you choose to go with 44.1kHz anyway so you might as well go with 48kHz. You can up this to 96kHz on an HDMI connection, but I don't have one to test with.

Extra Credit

Mediatomb development does not seem very active, but some folks have made patches to add features that make streaming more enjoyable. One annoying part of streaming on the PS3 is the default grey music icon for your tracks. This can be replaced by the album art with patch number one. You can't seek in tracks either, but that is also negated with patch number two.

Coming up: Transcoding matroska containers into the best possible format.

Go Go Texas Rangers

texas-rangers.jpg This is the year I can feel it. The year the Texas Rangers win the World Series. Each player is continuing to play well in the playoffs. They might not be a constant force in every game, but every player has stepped up. TV commentators have been forced to mention how well the Ranger's batting order plays and how there is no real "number 9" slot.

In a perfect world they will sweep the National League team, which will work out perfectly for my Game 4 tickets. The NL teams look like they will be good competition though so I will just hope for a good Game 4 to go to.

Go Rangers!

Put This In Your Pipe and Market It

Places of higher education teach ways in exploiting the ignorance of the common man. Who'd a thunk it? Read on, if you dare, to find out the results of the Humble Geek marketing contest.

Honorable Mention

Inserting advertising into TV reruns is a new fashion statement.

Second Place

Restricted to late night or weekend time slots, informercials provide marketing in 30 minute episodes. Cheap slogans are what make or break these gems. Who can forget "set it and forget it!"?

First Place

The pinnacle of marketing gimmicks must be the 30-second "As Seen on TV" advertisement. In under a minute you are bombarded with how your life is too hard and how you should spend $19.95 plus some shipping and handling for, not one, but two easy cut knives and a bonus cooking pamphlet. The First World has too many problems and we must be saved.

We Take Our Existance in Vain

You know what really grinds my gears? When you pull out of a drive-thru window at a restaurant and you get behind some old guy going 5 below the speed limit. --Old Chinese Proverb

  • Gladiators (Animal fighting)

Killing humans (and animals) in the name of sport.

  • Saloons (Clubs)

Killing brain cells in the name of finding a mate.

  • Reality TV

Killing common sense in the name of money.

  • Social Networking

Killing privacy in the name of information.

  • Government Privilege

Killing freedom in the name of power.

What do the bullet points have in common? A chronic disease found inside all living creatures that keeps us from being perfect: vanity. Heck, we even have a magazine that exclusively tailors to it!

Stop me if I'm being too obvious, but if we don't change now we are destined to have the film Idiocracy become a documentary.

- page 1 of 11