Encoding for the PS3, and potentially other MP4 compatible devices

Wednesday, August 12, 2009 Posted by Psyk 0 comments
Playing around with encoding settings for playback on the PS3 led to alot of trial and error and tweaking. It seems to be easier to find a way on how "not" to do it than how to do it as there's so many utilities and tools out there.

I stuck with mencoder/mplayer as I've used it over the years and extremely comfortable with how it behaves and works. The only real challenge is that it can't really mux properly into an MP4 container that other devices can properly read (i.e. it can read it's own 'corrupted' MP4 but nothing else can!). If that's mencoder's only blemish on an otherwise powerful video encoding tool, then that's really nothing at all.

So the process is to use mencoder, faac for some additional flexibility in bitrates, normalize for levelling out the audio and then MP4Box to mux it all together into a universally compliant MP4 container.

If you're running Fedora, you'll need to install these applications as follows:
yum install mplayer mencoder gpac normalize faac


Firstly, create edit your ~/.mplayer/mencoder.conf and add the following:
 [PS3]  
 vf=pullup,softskip,pp=fd,scale=:-10,hqdn3d,harddup  
 ovc=x264=yes   
 x264encopts=level_idc=41:crf=18:bframes=16:b_pyramid=yes:partitions=all:threads=auto:frameref=4:mixed_refs=yes:weight_b=yes  
 oac=pcm=yes  
 of=rawvideo=yes  
 sws=9  
 aspect=16/9  


Then create a file called "enc-to-ps3.sh" in your favourite editer (e.g. gedit). This will be the executable filename and you can choose any name you like.

Add the following lines:
mplayer -fps 29.97 -nocorrect-pts -vo null -vc null -ao pcm:fast:waveheader:file="$1"_temp.wav "$1" && \
normalize -a 0.5 -v "$1"_temp.wav && \
faac -b 256 -c 48000 --mpeg-vers 4 -o "$1"_temp.aac "$1"_temp.wav && \
mencoder -profile PS3 "$1" -ofps 29.97 -o "$1"_temp.264 && \
MP4Box -fps 29.97 -add "$1"_temp.264 -add "$1"_temp.aac "$1".mp4 && \
rm "$1"_temp.wav && \
rm "$1"_temp.264 && \
rm "$1"_temp.aac 


Make "enc-to-ps3.sh" executable by running:
chmod +x enc-to-ps3.sh



So what does it do?
mplayer -vc null -vo null -ao pcm:fast:waveheader:file=temp_"$1"_temp.wav "$1" && \

This line rips the audio from your video track and writes it out to a wave file. The name of the 'temp' file is your original file prefixed and suffixed with "temp" for easy identification.

normalize -v temp_"$1"_temp.wav && \

Quickly normalize the audio to prevent clipping and clean up the sound. I have a lot of clips where the audio is too load/soft and this helps adjust that.

faac -b 256 -c 48000 --mpeg-vers 4 --tns -o temp_"$1"_temp.aac temp_"$1"_temp.wav && \

This line encodes the resulting normalized WAV audio file into AAC at 256kb bitrate.

mencoder -profile PS3 "$1" -o temp_"$1"_temp.264 && \

Mencoder is used here to encode the original video using the profile settings we created earlier. This is the lengthiest piece of the encoding process depending on your source, so grab a coffee and come back in a little while. The output is a raw h.264 (x264) stream.

MP4Box -add temp_"$1"_temp.264 -add temp_"$1"_temp.aac "$1".mp4 && \

Using MP4Box from the gpac utilities, the resulting h.264 stream and AAC audio file are then muxed together into an MP4 file using the detected frame rate of the original video to keep things in sync.

And then your done.

Usage is as easy as:
enc-to-ps3.sh <name_of_video>


The resulting file should be playable on the PS3...

The script is quite rudimentary, but the basis of keeping it like this allows understanding of the process and then empower you to tweak settings if you so desire.
Labels: