if jailbroken, ssh in or use iphone explorer or similar. navigate to /var/mobile/Media/iTunes_control/Music. Inside that are a ton of folders with names like F00, F01. inside those are your music files with random names like aaqk.m4a, agjs.m4a, etc. Drag those to a location on your computer. then make sure itunes is setup to organize your music and to copy it to the itunes folder. assuming that your files are all tagged correctly, drag the folder copied off the iphone into itunes, and it will add them all to your library.
if not jailbroken, google is your friend.