I’ll keep this post simple as I don’t fully understand all of the different flags, however I did thoroughly go through various other posts on the x264 codec and the best flags to use for encoding for PSP.

The results below are designed for creating videos at full 720×480 resolution for the PSP with high quality and not super fast encoding in mind. See my earlier post on Encoding video for the PSP for info on alternative methods.

I used a 2-pass encode, but you can use 1-pass if you don’t feel the need, just remove the “-pass 2” code.

ffmpeg -y -i %1 -s 720x480 -r 25 -an -pass 1 -vcodec libx264 -b 1400kb -bt 2000kb -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -refs 2 -me_method umh -me_range 17 -subq 1 -trellis 0 -coder 1 -bf 7 -b_strategy 1 -threads 0 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 %2.mp4

Use the above for the first pass, and the below for the second pass:

ffmpeg -y -i %1 -s 720x480 -r 25 -acodec libfaac -ab 160k -ar 48000 -pass 2 -vcodec libx264 -b 1400kb -bt 2000kb -flags +loop -flags2 +mixed_refs+wpred-fastpskip -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -level 30 -refs 2 -me_method umh -me_range 17 -subq 7 -trellis 2 -coder 1 -bf 7 -b_strategy 1 -threads 0 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 %2.mp4

Note: The “-y” option will automatically overwrite the output file if it uses an existing file name, so remove that if you want to be warned about this by ffmpeg. “-y” forces “yes” as an answer.

I wrote a basic .bat batch script for ms-dos/windows automating this a bit, so you only have to type “ffmpegdos input.file output.name”, these are represented by %1 and %2 respectively in the above ffmpeg examples. You can replace %1 with the actual input file name, and %2 with the actual output filename if you want to just run it manually.

The contents of the .bat file are as follows:

@ECHO OFF
echo about to begin encode process...
pause

echo are you sure you wish to overwrite %2.mp4?
pause
D:InstallFFMpegffmpeg -y -i %1 -s 720x480 -r 25 -an -pass 1 -vcodec libx264 -b 1400kb -bt 2000kb -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -refs 2 -me_method umh -me_range 17 -subq 1 -trellis 0 -coder 1 -bf 7 -b_strategy 1 -threads 0 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 %2.mp4
D:InstallFFMpegffmpeg -y -i %1 -s 720x480 -r 25 -acodec libfaac -ab 160k -ar 48000 -pass 2 -vcodec libx264 -b 1400kb -bt 2000kb -flags +loop -flags2 +mixed_refs+wpred-fastpskip -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -level 30 -refs 2 -me_method umh -me_range 17 -subq 7 -trellis 2 -coder 1 -bf 7 -b_strategy 1 -threads 0 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 %2.mp4
echo DONE!

Just copy paste the above into an empty text file using notepad or similar and save it as ffmpegdos.bat, to run it just open a “Command Prompt” and type ffmpegdos input.file output.name. The .mp4 extension is automatically appended to your output filename.

Good luck!

Encoding video for PSP

December 24, 2008

This is a mostly OS X centric post, but it’s just as relevant for linux and windows as it centres around the use of ffmpeg. (If you want specific ffmpeg info read my other post.)

I recently decided to start using my ancient PSP-1000 to watch my own videos, it usually takes me a long time to actually use gadgets I have to their full potential.

With the practical death of UMD videos and the ~recent introduction of hi-bitrate and hi-resolution mp4 codecs to the PSP and PS3 I thought it was time to give it a go.

BUT I didn’t want to have one set of videos just for the PSP and one set for every other media device. So how to preserve the native resolution and aspect ratio of an original DVD, yet still watch it on the PSP and then still watch it in it’s native DVD resolution as an mp4?

Not easy… It’s taken a lot of searching and 100’s of encodes to figure out some of the optimal settings, and that’s what I’d like to share here. Obviously a lot of credit goes out to all the other people who looked at this before me and found their solutions, cudos.

Firstly, let me clarify that the highest resolution that the PSP handles at the moment is 720 x 480, it does not support PAL native resolutions as far as I can tell.

With this in mind there’s a number of important settings that need to go into encoding AVC (h264/h.264) mp4 video files for the PSP. The most important is that you always need to encode using the MP4 Baseline.

For resolutions like 320×240 or 480×242(native) use the h264 baseline level 1.3 or 2.1 (for ffmpeg this means level=13/level=21).

To encode at 720×480 you can only use h264 baseline level 3.0, nothing else works that I have tried.

Next is something I don’t really get, but the results I suppose speak for themselves. When setting up other codec options, you can only use a maximum of 3 reference frames, and only a maximum of 2 if you are using B frames. However B frames seem to be able to go up to 7 or more so far in my tests, and a combination of 7 B-frames and 2 reference frames rather than 3 reference frames and 0 B-frames seems optimal. See the image below:

picture-2b

You can’t use 8×8 quantize or pyramid B-frames or you’ll break the encode.

Pretty much everything else you can play with.

Now for the more OS X centric part. I’ve found the program HandBrake (version 0.9.1 for OS 10.4.x or higher for 10.5.x) to be excellent.

With this program not only can you encode an mp4 to 720×480 you can also retain the native aspect ratio of the DVD for playback on other devices with a simple check of box.

This means that PAL DVD’s that are squashed into a 720xXXX frame size usually expand to 1024xXXX meaning that you can do that with the 720×480 container, so for instance when I play back the video on my PS3 it plays it back as 1024×480 which looks a lot nicer than squashing it down to 720xXXX. However the PSP will just stretch the video to fill the screen, so if that’s going to be a problem don’t use that option. You can add padding in the crop option to add black borders which will eliminate this, however the final video needs to be exactly 720×480 regardless of the pixel aspect ratio, so keep that in mind.

So in summary, for HandBrake, make sure on the advanced tab that you have:

level=30

as part of your options or your file won’t play on your PSP. (this should work for all ffmpeg encodes too).

Good luck! I may expand the details of this if anyone finds it useful.

(Audio should be AAC 48.000kHz 128kbps, but I think that’s flexible).