Sunday, February 24, 2008

Converting MP4 video for the PSP (h264/AVC) on Linux

After my earlier post about converting video for the PSP proved to be pretty popular, I decided to do a new, easier, better and improved guide. Well, here it is, as promised. It took me a while because I had a lot of other things on my head the last few months. Well, better late then never, so they say. This time we're going to do MPEG-4 AVC (aka H.264) instead of plain old MPEG-4 ASP I used in the last post. A lot of things have happened since I wrote that other guide more than a year ago. First of all, I've learned a lot more about the PSP's compression techniques, mostly thanks to Robert Swain's great blog. Secondly, newer firmwares released by Sony have loosened up the restrictions for Video on the PSP. So first I'll show you a updated list of the restrictions for video on the PSP in AVC (AFAIK):
  • MPEG-4 File Format
  • MPEG-4 AVC Video codec
    • H.264 Main Profile up to Level 3.0
    • Any framerate up to 30 fps
    • Up to 2 reference frames
    • Arithmetic Coding (CABAC)
    • Width/Height must be a multiple of 16
    • Allowed Resolutions:
      • Any resolution up to 320x240
      • 352x240 (VCD)
      • 352x480 (CVD)
      • 480x272 (Native)
      • 720x480 (DVD)
    • No 8x8 transform
    • No reference b-frames
  • MPEG-4 AAC Audio codec
    • 48000 kHz
We no longer have to use the command-line tool ffmpeg, thanks to a great piece of software called Avidemux. If you're using Ubuntu 8.04 LTS, Avidemux 2.4.1 comes in the repository so all you have to do is install the 'avidemux' pakage through your prefered method. Older versions of Ubuntu come with Avidemux 2.3 which, unfortunately, can't handle the PSP format properly. So in that case I recommend you to go over at GetDeb and download an Ubuntu package of the latest version of Avidemux there. If you're not using Ubuntu, check http://fixounet.free.fr/avidemux/download.html to see if there are any packages of Avidemux 2.4 or higher for your Linux distribution. To install the package in Ubuntu, just double-click it and let gdebi do the rest. Another tool that can help a lot when working with video, and a great video player in general, is mplayer. In Ubuntu you can install it from the repository. (sudo apt-get install mplayer) After installing Avidemux, run it. In Ubuntu it should be in the [Applications]->[Audio & Video] menu. Avidemux' interface is pretty straightforward. The first step is to load the video that you want to convert. To do that just click the [Open] button in the toolbar, select your video and click Ok. It is possible Avidemux will ask you whether you want to index or rebuild the frames for you video. Just click Yes and let it do it's work. After opening your video, you can play it and browse trough it like any other media player. Now let's convert this video! Avidemux comes with a preset that will automatically configure it to convert to PSP-compatible video. All you have to do is go to the [Auto] menu and select [PSP (H.264)]. Then a little dialog window will show up. Target type can be PSP 480*272 or PSP full res (720x480). Full res is only really useful if you have a PSP Slim and want to play your movie on TV through the Video-out cable. (The PSP's native resolution is 480x272, while standard NTSC resolution is 720x480.) Converting at the higher resolution will, as you probably know or guessed, result in a bigger file. (Around 700 MB for a whole movie vs. 400 MB for a movie at lower resolution.) If you know the aspect ratio of your movie you can configure this as well. If you don't, just leave both at 1:1. After confirming this dialog, if you're in a hurry, you could just click Save, choose a destination and Avidemux would produce a file that's perfectly playable on your PSP. However, the default encoder options used by Avidemux will produce large files with comparably lower quality, so I really recommend you go through this guide. Also, you might want to take a look at the filters to make sure the video will look exactly the way you want it to. First let's optimize the codec options. Click on [Configure] on the left panel under the Video codec dropdown box. The first and probably most important step in improving the encoding quality and reduce size is to change the Encoding Mode from One Pass to Two Pass. This will make Avidemux encode the video two times, the first pass serves to analyse the video for scene changes etc. so it can encode the video more efficiently in the second pass. If you are encoding for 480x272 resolution, set the bitrate to 384 kbit/s, that will be plenty with all optimizations enabled. If you are going for full res (720x480) you should choose a higher resolution, like 632 kbit/s 768 kbit/s. Now go through all the tabs and set the options exactly like you see them in the pictures below. I'm not going to explain what every option does, but just trust me when I say that this will configure the encoder for super-mega-uber-optimized encoding. The Motion & Misc tab. The Partitions & Frames tab. The only changes on the Rate Control tab are Scene Cut Threshold: 40 and Max IDR frame interval: 300 You don't have to change anything on the More tab, just click OK now. After this tedious process it is a good idea to save the current configuration as a project, so you don't have to open up this page every time you decide to encode some videos. Click [File]->[Save Project]. Avidemux doesn't dictate any file extension, but for your own sanity it might be a good idea to make up one of your own. I just tend to call them 'foo.project'. (Yes, not very creative, I know.) Now that you have saved the current project, whatever happens, you won't have to go and configure all those settings again. You can open projects through [File]->[Load/Run Project...]. One of the coolest things of Avidemux (well, at least for videophiles like me) is the ease with which you can configure filters. In the main Avidemux window click on [Filters] under the Video section on the sidebar and the Video Filter Manager will pop-up, it looks something like this. On the left you see all available filters, on the right you see the filters currently applied to the video. Now Avidemux tries to act somewhat intelligent when you choose an 'auto'-preset. Basicly it will resize the video so it fits in the PSP's resolution, and then add black borders to fill up the remaining space. That means that if your movie is too wide (like the cinematic 2.35:1 aspect), it will add black borders above and under the video. If it's too narrow (like good ol' 4:3 video) it will add borders left and right. However if your video uses a non-square pixel (anamorphic) resolution, your video might get deformed on the PSP if Avidemux can't figure out what the correct aspect of the source video should be. If you want to know if the video is anamorphic, open it with mplayer from the command line. Look out for a line containing something like 720x480 => 853x480. If the resolution is adjusted, (it shows to different resolutions) the video is anamorphic. (NOTE: if you have a widescreen monitor you have to edit /etc/mplayer/mplayer.conf and add/change the option "aspect = 16:10", assuming your monitor's aspect ratio is 16:10. Otherwise mplayer will assume your monitor is 4:3 and adjust all videos correspondingly.) Another problem is that when a video is allready letterboxed (ie. a widescreen movie that allready contains black borders), Avidemux will still add borders left and right, resulting in black borders all around the video. I will handle 2 different use cases here to help you understand everything better. Anamorphic Video This is most common with downloaded music clips, older television shows, etc. I found out I got an anamorphic video by playing it with mplayer from the command line first, one of the lines it outputted on the terminal was: VO: [xv] 352x240 => 352x270 Planar YV12 The resolution of this vid is 352x240, but it is in fact in 4:3 perspective. Mplayer figured this out and automatically adjusted the resolution. Actually, I could allready have guessed it was anamorphic by just looking at the video in any video player, see it was a normal 4:3 video, and noting that the resolution is not 4:3. (352/4*3 = 264) Now Avidemux's isn't as smart as mplayer, and will think the vid is 352x240 with square pixels. The PSP preset will just resize it in the same preset so that it fits, and then add black borders. First of all, with 4:3 video black borders aren't necessary at all, and just make the video bigger, so first we remove the 'Add black borders' filter (just select the filter and click the minus button). Now we change the MPlayer resize filter (select the filter, click configure) so that it resizes the video to a 4:3 resolution. It might seem logical to just resize it to 352x264 but there's one detail: 264 isn't dividable by 16, so the PSP won't play it. Instead it's better to just resize 4:3 video to 320x240 (it's the highest real 4:3 resolution that will cleanly divide and fits in the PSP's native resolution). Cropping Widescreen Video Ok, so now we have a movie in it's original 2.35:1 widescreen format. The PSP prefix will just resize it to 480 x something and then add borders. If you want the video to be in it's original form, without any cropping, that's okay. However, the PSP's screen isn't a 42" TV. So it's sometimes a good idea to crop the movie so it will fit optimally on your PSP's display. Figure out the videos resolution before going to the filters. (You can find out everything about a video inside Avidemux by clicking the little properties button.) The first step is to remove the black borders filter, the resize filter and any other filters. Then you add a crop filter. Now you need to do some math. For example, my video is 620x256. I want to crop it to 16:9, so I do 256 / 9 * 16 = 455.111. So the cropped size is going to be 455x256. That means I need to crop a total of 620 - 455.111 = 164.889. Divide that by 2 and you get 82,44. In other words, I crop 83 pixels from one side and 82 pixels from the other. After you have applied the crop filter, all that is left is to resize the movie to the proper 480x272 resolution. Anyway, whenever you're happy with your settings all you need to do is save the video and voila. Remember that you can see what the resulting video will look like by clicking on the combobox that sais 'Input' and changing it to 'Output'. Well there you have it, now you can easily convert your favorite videos to your PSP. Have fun! [UPDATE 23/06/2008] Updated the list of restrictions on the PSP.

38 comments:

Anonymous said...

Well done! This works perfectly.

Thank you for taking the time to go through all of this for those of us too lazy to do it ourselves. You saved my arse tonight. :)

Yogarine said...

You're welcome. ;-)

Kurnia said...

I use Kubuntu 7.10 and this works perfectly. It's really made my day.

Anonymous said...

I followed these instructions. Then I copied the file to MP_ROOT/100MNV01. I renamed the file M4V23456.MP4. It shows up under video, but says unsupported. I started with an xvid/mp3/avi file. What am I doing wrong? Thanks.

Yogarine said...

You should just copy it to the VIDEO folder in the root of your memory stick. No need to rename it to M4V?????.MP4, that was only necessary with older firmwares.

Anonymous said...

this is fantastic, i was about to abandon linux after a couple of years for the lack of a psp click and go solution...

now somebody needs to port itunes and i can forget about win forever...

thank you !

Yogarine said...

Port iTunes? Did you have a look at what Rhythmbox has to offer? And then there's also Songbird, Banshee...

Anonymous said...

This is without a doubt, the best psp video conversion walkthrough like....evers. I tried it and got perfect files everytime. Thanks so much!

Anonymous said...

I can never get videos to convert for PSP on Linux. I followed this guide, and avidemux would just crash. When I simply use the PSP preset without all of your extra settings, it makes a video which I can play on my computer, but shows up as "Unsupported data" on my PSP. What could I be missing?

Yogarine said...

What Linux distro and version of Avidemux are you using?

lowlife said...

Dude... very nice! Thank you.

I'm using the OS X port of Avidemux, interface doesn't look as flashy as ubuntu but from my small tests so far the output looks great, seems fast and is exactly what I was looking for.. was earlier trying a program called handbrake but couldn't get it to output for PSP at 720x480... it also uses ffmpeg... do you happen to know what the command line trick is for 720x480? I can get 480x272 no problems, is it something to do with the baseline option? i've tried level=13 (1.3) and level=21 (2.1) also tampered with 30 (3.0), no joy so far.

Am thinking about converting my fave movies to 720x480 so i can use them on psp and ps3... however I guess you lose the anamorphic squeeze as the PSP won't unsqueeze that at 720x480 or?

Cheers!

lowlife said...

Yo,

Just answered my own question there and sorted out handbrake! :) Thanks again..

for 720 x 480 it has to be AVC h264 Baseline level 3 or higher, but 3.0 does the trick. (ie. in handbrake "level=30" in the bottom of the advanced tab.)

See here for why http://en.wikipedia.org/wiki/H.264#Levels

Anyway, I still think Avidemux did a neater job somehow and faster... so will continue using both.

Yogarine said...

Indeed, for 720x480 you need to use level 3.0 (which afaik is the maximum level the PSP can handle).
Anyway, it's always best to stick to 3.0, because I think it allows for some extra compression tweaks in the codec to further reduce the filesize/increase quality.

Eric Kincl said...

Great guide! 1,000,000 thanks!

A side note: It seems that avidemux (my version at least) defaults to the .avi file extension. PSP appears to barf with this extension, telling you it's corrupted data. All you have to do to fix this is rename the file extension to a .mp4. (This took me 2 hours to figure out!)

Anonymous said...

Thanks so much for this. Works like a charm, and now my daughter has a full Memory Stick of Dora the Explorer for tomorrow's flight.

Cheers,

Bret

Anonymous said...

Hey, Man!

You're the Light in the dark night :)

Thanks a lot!!!

Yogarine said...

You're welcome... ;-)

Anonymous said...

there is no mention or apparent ability to boost the audio...is the typical audio file already proving to be satisfactory in most instances (where so many other apps are not) ??

thx...Mike

Anonymous said...

Hey, nice guide!

It helped my convert two movies that I really wanted to watch on the go :D

The problem is, the audio starts to trail off in each movie, and I looked around the web, and I still can't find the solution to my problem D:

Can you help me?

Anonymous said...

Congratulations on your guide. Very very helpfull..
I just bought a TV-OUT cable for my psp slim, and i was searching everywhere for some way to convert movies to watch on tv with good resolution...
But i dont know if this can de done faster... it tooks me like 7-8 hours to encode one movie in 720x480 resolution...
The original was an avi file of 700MB with 6xx X 3xx resolution..
What i have to do to make the conversion faster? I got an asus f9s, with core 2 duo 2.0 (T7300), 4gb ram, and geforce 8400M G

Yogarine said...

"there is no mention or apparent ability to boost the audio...is the typical audio file already proving to be satisfactory in most instances (where so many other apps are not) ??"

Well in most cases the videos volume should be high enough. If it isn't then you should just check out the audio filters in Avidemux, you can increase the gain there.


"The problem is, the audio starts to trail off in each movie, and I looked around the web, and I still can't find the solution to my problem D:"

This could be a frame rate issue, check if the output frame rate is the same as the source movie. Also converting from certain codecs will give you trouble at times. (wmv is notorious for this) Sometimes the sync is of from the start and you can fix it by setting the time shift in the audio filters.


"But i dont know if this can de done faster... it tooks me like 7-8 hours to encode one movie in 720x480 resolution..."

Well there's very little you can do about that I'm afraid. Converting a movie to h264/AVC at the highest quality at that resolution just takes a lot of time. (On my Pentium 4 I need to let Avidemux work the whole night for a 480x272 movie.)
If speed is really important to you, you could just reduce some quality settings so it encodes faster. (Experiment with the options in the Motion & Misc and Partitions & Frames tabs. Try reducing Partition decision and Motion Estimation Method.) But then obviously you'll lose video quality.
The only solution to encode faster without giving up on video quality or bitrate is to buy a better CPU (or two). ;-)

Anonymous said...

about the speed... and if i just want to rum exactly the same avi in the psp?
i mean... a mp4 with same resolution and audio im mp3, like the original... can this be done? will it be faster?
can psp play the original avi without converting it? =P

Yogarine said...

That would work only if the video codec is plain mp4... (So if it's DivX or XviD it won't work.)
Either way you'd have to convert the audio to AAC. You can try following this tutorial, but before saving just setting the Video Codec to "Copy". Though there is very little chance it will play on the PSP.

Another option is converting the video to MP4 ASP. (It's the other option from the Auto Wizard.) In that case you don't have to bother about the advances encoding preferences, they should be okay by default.
Your video will look a lot uglier though (or be a lot bigger, depending on the bitrate)...

The cool thing about h264/AVC is that, at the maximum settings, you can cram a whole movie into a 400MB file without losing a lot of quality.

Anonymous said...

Hi!

First off I thought the guide was very informative and helpful. Although for me it didn't make it the whole way.

I had first been tinkering around in Avidemux on windows vista by myself. But, not being all that informed about all aspects of video I did fall short. First when I was trying to convert videos avidemux would crash when trying PSP h264.

With your guide the video would atleast successfully convert the file. And here is where the problem lies. Like a poster above I got "unsopprted data". I had as well renamed my file m4vXXXXX.mp4. Then I read your note about not having to rename files in that manner anymore, so I named it back. My PSP 3000 has firmware 4.20, and when the file wasn't named m4v*.mp4 then the file wouldn't show up at all when going into the vide section.

I am using Vista, not linux.
Avidemux version 2.4.4
and firmware 4.20 on my PSP.

I would be very grateful to any insight or better yet a solution to my problem.

Yogarine said...

What folder on your PSP are you placing the video in? You should place it in a folder called VIDEO on the root of your PSP, _not_ in the MP_ROOT folder. (That's only for firmwares older than 2.80.)

Anonymous said...

MP_ROOT\100MNV01\
Is the folder I place the videos in.

Anonymous said...

Like you said, creating a video folder in the root and placing it there under any name worked perfectly.

Thank you very much. :)

Hugo said...

Hi, it's me again, the guy who asked if the conversion could be faster... Just to you add to your tutorial, 640x480 is also a supported resolution =). i tested it! cya, and thanks!

Finisher said...

Avidemux 2.5.0 is released!
Some standard settings are changed for the PSP (two pass, uneven multi, average bitrate: 1000)

Anonymous said...

*pushed my chair away from desk.Stand applauding*

Excellent Post!!!

Anyone can get it to work. You gave explanation on how to get it to work AND look good AND to customize. Well Done.....!

Faisal Yousuf said...

Thank you so much for this excellent and detailed tutorial. It's exactly what I've looking for.

Regards,

Faisal
Jeddah, Saudi Arabia

Anonymous said...

"configure the encoder for super-mega-uber-optimized encoding". You must be joking! You didn't even enabled 8x8 DCT Transform or Mixed References !!!

Yogarine said...

@Anonymous
That's because the PSP doesn't support 8x8 DCT Transform or Mixed References. (Or at least didn't at the time of writing this guide.)

gameopera said...

Video produced using AviDemux v2.5.2's PSP auto config does not work (on the PSP). Turning off 8x8 transform and changing max ref from 3 to 2 gets it working. Was able to work this out thanks to your excellent post. Thank you.

bastafidli said...

When using Avidemux 2.5.3 you need to install the following patch

http://avidemux.org/admForum/viewtopic.php?id=7926

That will provide the correct profile with all the fixes mentioned above + more.

LS said...

Thanks a lot for posting this guide. It worked well for me. I'm using Ubuntu 10.04, Avidemux 2.5.2 and PSP 3000. Right now I'm converting a whole movie plus subtitles. Thanks a lot again dude! :)

Vospi said...

Sorry, but this specs are awfully wrong. you can go all the way up to 16 b-frames, 3 reframes (which makes A LOT of difference!) and use more different resolutions than named (368x272 for example).

I know it because I've coded a bunch of movies for PSP and did a research before. doom9 helps a lot.

tips windows said...

cara baru convert video ke mp3
terima kasih banyak infonya