This is what I did,
1. Copy the iTunes folder (in ~/Music) to an external drive. Do not modify anything on or in the folder.
2. Re-install OS X. Leave iTunes closed. Don't plug in iPhone.
3. Navigate to the iTunes folder (in ~/Music) and replace it with the one you copied earlier. (If there is no iTunes folder, just copy the old one in).
4. Open iTunes, all your music/videos/apps should be there. Authorize the computer.
5. Plug in iPhone. Sync.