HOW-TO: MPlayer on OSX with UTF8 subtitles


After the debacle of GG fansubs purposely adding large comment strings to their .ass subtitle files so that it would crash, or otherwise confuse, the VLC player, I have moved to using Mplayer as my default video player.

Subtitle support in VLC is borked for anything other than plain subtitle text. And as most fansub groups are moving towards softsub and the matroska container it is a good one to use as it’s support for .ass, while not being total, is far superior to that of VLC as it presently stands.

I am still keeping VLC as a secondary player, as it contains some good features for streaming video content.

The problem with using Mplayer is that as a Linux application it can be pretty arcane at times with some rather difficult to know configuration issues. But once it is set up it is a breeze.

In this How-to I will show how to set up Mplayer on OSX so that it can show subtitles in ANY language.

UTF8 on the command line

One of the issues with setting up Mplayer for UTF8 subtitles is that you must be able to enter UTF8 characters on the command line during the configuration. Unfortunately, even though OSX ships out-of-the-box with UTF8 compliance in the UI, it is missing for command line input.
To set this straight you have to do a few command line tasks.
Firstly find out what locales are available in OSX. [A locale is a definition for the location the computer is at and contains stuff like how dates and decimals are formatted]

$> locale -a

You should get a long list of locales. They should be in the form of the language followed by the region, i.e. nl_BE is the one for Dutch language in Belgium.
As you can see they all have a sub option that defines the character format that should be used, i.e. ja_JP.SJIS defines Japanese language, Japan region, with SJIS character encoding. You can see that all of them have a sub option .UTF-8.
Select the one for your language and region. I decided on en_US.UTF-8 as although I am a Brit I am using a US keyboard.
Next you must determine what bash initialization script gets called.
Run an ls -la in your home directory and look for files beginning with a period. [These files are invisible in the Finder unless you set them to visible]
If you have a .bash_profile use this for the following set. If you DO NOT have a .bash_profile but have a .profile use this for the following step.

$> echo “export LC_CTYPE=en_US.UTF-8” >> .bash_profile

Open, or create, the .inputrc file from your home directory in TextEdit and add the following lines.

set convert-meta off
set meta-flag on
set input-meta on
set output-meta on

What this does is it sets up the bash shell to accept UTF8 input.
Now we must set up terminal to display UTF8 characters properly.
Bring up the Terminal Window Settings dialog by clicking “Terminal | Window Settings” in the menu.
Select Emulation from the drop down and make sure that Escape non-ASCII characters is unchecked.
Select Display from the drop down and make sure that Character Set Encoding is set to Unicode (UTF-8).
Now click the Use Settings as Default button to set up terminal to always be in UTF8 compliant mode.
Next we have to tell OSX itself that we should be using UTF8 for command-line input.
Run the following command in your home directory

$>ls -la | grep ^d

This command will show all the directories in your home directory. If a directory called .MacOSX exists then skip the first command in the following section, else perform both the following commands from your home directory

$>mkdir .MacOSX
$>echo “{ LC_CTYPE = “en_US.UTF-8″; }” >> .MacOSX/environment.plist

To register these last setting changes you have to log out and long back in again.
You should now be able to enter UTF8 characters on the command line and also use some command line utilities to view UTF8 encoded content on the command line, such as cat.
But note that others require either a switch or some configuration, for instance you MUST use the switch -w to get ls to display UTF8 characters. [I suggest adding this switch to an alias ;-)]

Configuring MPlayer

Download the OFFICIAL mac binary of MPlayer here.
Install Mplayer from the disk image in the usual manner.

Command line access

Now it is nice to be able to run Mplayer from the command line. Purely because it contains some usefully utilities for determining what audio and subtitle tracks a particular video container contains.
But primarily because it is not that simple to change audio and subtitle tracks in the UI itself. [Remember this is a Linux utility…]
To do this open your terminal and execute one of the following commands

$>sudo ln -s /usr/local/bin/mplayer /Applications/MPlayer\

If you have an Intel based Mac, or the following if you have a PPC based mac

$>sudo ln -s /usr/local/bin/mplayer /Applications/MPlayer\

You will have to enter your password to create the link.
Close your terminal and open a new one. [You should be able to start mplayer from the command line now]

UTF8 in Subtitles

Finally we get to set up subtitles in Mplayer to use UTF8 encoding. ASS, the presently popular subtitle standard, allows far more than just the formating of subtitles. It allows for the positioning of subtitles and also you can embed fonts into the format.
We will be specifying that ASS, if available, is our default subtitle format and that we should use embedded fonts, if they exist, and also that we should use japanese as the default audio track and english as the default subtitle track. Phew. Sounds like a lot. But this can be done in only two files. And one of those is just a soft link. 😉
Open TextEdit and type the following;


Save this file to a file called mplayercofig.txt to your home directory.
In a terminal window do the following commands in your home directory.

$>mkdir .mplayer
$>mv mplayercofig.txt .mplayer/config
$>cd .mplayer

now that you are in the .mplayer directory we need to create a link to a UTF8 trueType font that will be used by Mplayer if an embedded font is not contained by the subtitles.
Open Font Book select All Fonts and find the font called STFangsong.
You can use three system fonts as the default font [STFangsong, STHeiti, STKaiti] the choice is up to you. I use the sans-serif STKaiti.
With the font selected hit the cmd-i key combination to bring up that fonts information.
Using ctrl-c select and copy the location information from the information pane.
In your terminal type the following, using ctrl-v to paste the font location

$>ln -s /Library/Fonts/华文楷体.ttf subfont.ttf

You will notice that your Kanji appeared on the command line when you pasted, and also that the link has been created. [try typing open subfont.ttf and the Font Book should open with the font you are using selected!]

Container Inspection Script: Finding out what you’ve got!

One of the issues that can occur is that some fansubbers have multiple English subtitles. For instance, one for official names and one for fan names of characters.
With the default configuration you just set up you get the first English subtitle track available in the container. This might not be the one you want.
So I wrote a little script to find the audio and subtitle tracks available in the container.
Here it is

echo Subtitle tracks available for $1
echo ——————————–
mplayer -vo null -ao null -frames 0 -v “$1” | grep sid
echo Audio tracks available for $1
echo ——————————–
mplayer -vo null -ao null -frames 0 -v “$1” | grep aid
echo To start Mplayer using a track of a different id type the following
echo [Where X and Y are the id of the tracks you require]
echo mplayer -sid X -aid Y $1

Save this as an executable file on your PATH and you can access it anywhere to determine the contents of the container.
Starting Mplayer from the command line with the arguments as described will give you the audio/subtitle track you want. However you will not get the nice UI that you get normally.


Although a bit of a pain to set up, once you have this done it is a breeze to use Mplayer and it looks good too. 😉

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s