understandable linux tips, tricks and tutorials

In this howto, I’ll show how to create and install ringtones on your iPhone. There are a bunch of howtos around the internets on how to do this using Windows, and possibly on Linux with jailbroken phones. However, I haven’t seen one showing how to do it using just Linux, without having to jailbreak your phone. So here goes.

Pre-Requisites

To do this, you’ll need:

  • A working installation of libimobiledevice – howto here.
  • You’ll need to have the following applications installed:
    • audacity;
    • faac;
    • ffmpeg;
    • id3v2;
    • mplayer;
  • An mp3 file you want to make into a ringtone.

Create the Ringtone

A ringtone for the iPhone has to be 30 seconds or less, so the first thing you need to do to create a ringtone is to cut your .mp3 file down to 30 seconds or less if your file is over 30 seconds long. To do this, open audacity and then open the .mp3 file you want to use. Use your mouse to select 30 seconds (or less) of the track:


Then press ctrl+t to trim the rest of the track. Then use the Export… option from the File menu to create a new .mp3 file of your clip. One thing you need to write down now is the length of the clip in milliseconds. You can find this below the wave representation of the clip at the bottom of the window. The middle number should show you the length of the clip – make sure you have 3 decimal places (change the format to milliseconds to get 3 decimal places).

Once you have your 30 second (or less) .mp3 file, we need to create it to an .m4a file. You can skip this step altogether if your version of audacity has the ability to save to .m4a format, in which case, rather than saving your clip to .mp3 as above, save to .m4a. However, there seems to be some sort of incompatibility between audacity and the latest ffmpeg libraries, which means if you’ve got the latest ffmpeg, you may not be able to save as an .m4a file. However, never fear, the good old commandline will save you. This is a simple script that will convert your .mp3 file to an .m4a file. Just copy the following into a text editor like kate and save it as mp32aac.sh:

#!/bin/bash
# Azakus 2007
# Requires mplayer, faac, and id3v2
echo "Input name of mp3 file, excluding .mp3"
echo -n ">"
read IN
OUT="`ls | grep $IN.mp3`"
echo "Dumping mp3 to wav"
mplayer -vo null -vc null -ao pcm:fast:file=$IN.wav $OUT
echo "Converting id3v1 tags to id3v2 for easier transition to m4a tags" 
id3v2 -C $OUT
#"Coverting id3 tag from mp3 to m4a tag" --requires id3v2 tags.
TITLE="`id3v2 -l $OUT | grep TIT2 | awk '{ORS=" "} {for (i = 4; i <= NF; i++) print $i}'`"
ARTIST="`id3v2 -l $OUT | grep TPE1 | awk '{ORS=" "} {for (i = 4; i <= NF; i++) print $i}'`"
ALBUM="`id3v2 -l $OUT | grep TALB | awk '{ORS=" "} {for (i = 4; i <= NF; i++) print $i}'`"
TRACK="`id3v2 -l $OUT | grep TRCK | awk '{ORS=" "} {for (i = 6; i <= NF; i++) print $i}'`"
YEAR="`id3v2 -l $OUT | grep TYER | awk '{ORS=" "} {for (i = 3; i <= NF; i++) print $i}'`"
faac -b 128 -c 44100 -w --title "$TITLE" --artist "$ARTIST" --year "$YEAR" --album "$ALBUM" --track "$TRACK" $IN.wav
rm $IN.wav
echo "AAC transcode complete!"

…or download it from here. Once you’ve saved it, you’ll need to make it executable:

chmod +x mp32aac.sh

If you want to be able to use it simply by typing mp32aac, copy it to /usr/local/bin:

sudo cp mp32aac /usr/local/bin

Then to convert the .mp3 file you’ve just created, type mp32aac.sh and at the > prompt enter the name of your .mp3 clip without the “.mp3″ bit:

matt2@linux-f215 ==>mp32aac.sh
Input name of mp3 file, excluding .mp3                                                                                                 
>Arco_Ring                                                                                                                             
Dumping mp3 to wav                                                                                                                     
MPlayer dev-SVN-r30099-4.4-openSUSE Linux 11.2 (i686)-Packman (C) 2000-2009 MPlayer TeamCan't open joystick device /dev/input/js0: No such file or directory                                                                                                                  
Can't init input joystick                                                                                                              
mplayer: could not connect to socket                                                                                                   
mplayer: No such file or directory                                                                                                     
Failed to open LIRC support. You will not be able to use your remote control.                                                          
 
Playing Arco_Ring.mp3.                                                                                                                 
Audio only file format detected.                                                                                                       
==========================================================================                                                             
Opening audio decoder: [mp3lib] MPEG layer-2, layer-3                                                                                  
AUDIO: 44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)                                                                  
Selected audio codec: [mp3] afm: mp3lib (mp3lib MPEG layer-2, layer-3)                                                                 
==========================================================================                                                             
[AO PCM] File: Arco_Ring.wav (WAVE)                                                                                                    
PCM: Samplerate: 44100Hz Channels: Stereo Format s16le                                                                                 
[AO PCM] Info: Faster dumping is achieved with -vc null -vo null -ao pcm:fast                                                          
[AO PCM] Info: To write WAVE files use -ao pcm:waveheader (default).                                                                   
AO: [pcm] 44100Hz 2ch s16le (2 bytes per sample)                                                                                       
Video: no video                                                                                                                        
Starting playback...                                                                                                                   
A:  29.9 (29.9) of 29.0 (29.0)  0.3%                                                                                                   
 
Exiting... (End of file)                                                                                                               
Converting id3v1 tags to id3v2 for easier transition to m4a tags                                                                       
Converting id3v1 tag to id3v2 in Arco_Ring.mp3... converted                                                                            
Freeware Advanced Audio Coder                                                                                                          
FAAC 1.28                                                                                                                              
 
Average bitrate: 128 kbps                                                                                                              
Quantization quality: 100                                                                                                              
Bandwidth: 22050 Hz                                                                                                                    
Object type: Low Complexity(MPEG-4) + M/S                                                                                              
Container format: MPEG-4 File Format (MP4)                                                                                             
Encoding Arco_Ring.wav to Arco_Ring.m4a                                                                                                
   frame          | bitrate | elapsed/estim | play/CPU | ETA                                                                           
 1290/1290  (100%)|  127.9  |    1.1/1.1    |   27.53x | 0.0                                                                           
 
AAC transcode complete!

You should now have your file with the .m4a extension. For this file to be used as a ringtone by your iPhone, you need to change the extension to .m4r, so:

mv your_file.m4a your_file.m4r

Putting The Ringtone On Your Phone

The first thing is you need to make sure your phone is connected to your computer via USB and mounted somewhere on your filesystem – see here for a howto. Then you need to navigate to the iTunes_Control/Ringtones folder within your iphone – I’d suggest using a filemanager to do this. Copy your .m4r file to that folder. Then navigate to the iTunes_Control/iTunes directory and open the file Ringtones.plist in a text editor. Mine looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Ringtones</key>
	<dict>
	</dict>
</dict>
</plist>

It is straight XML, and to add your ringtone, you simply need to add a few lines of XML, as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Ringtones</key>
	<dict>
		<key>YYUX.m4r</key>
		<dict>
			<key>GUID</key><string>148F52CB21CF093B</string>
			<key>Name</key><string>Arco_Ring</string>
			<key>Total Time</key><integer>29873</integer>
		</dict>
	</dict>
</dict>
</plist>

The first attribute is the name of your file. It currently shows YYUX.m4r – just change this to the name of your .m4r file. Within the following tags, you need 3 bits of information. The GUID – this is a hexadecimal string. Just cut and paste the jumble of letters and numbers between the tags. If you have more than one ringtone, just use it again, but change one character – remember it must either be a numeral, or a letter from A-E. The Name – this is the name of the ringtone that appears in your iPhone. The Length – this is the length of the ringtone in milliseconds. This is the number that I told you to write down from audacity, just take out the decimal point. That’s it. Save the file and unmount your phone. You should now be able to find your new ringtone under Custom on your phone in Settings -> Sounds -> Ringtone. That’s it – no iTunes, no jailbreak – just pure Linux goodness

11 Comments
  1. Do you really need all the ID3 stuff? Otherwise, you can convert any audio file to the right format using a single FFmpeg command, like this:

    ffmpeg -i some_input_audio_file -acodec aac -f mp4 my_ring_tone.m4r

    Note that by explicitly specifying the output format, you are free to use any output file extension you want.

  2. I had to restart my phone to get these Ringtones fully working but these instructions worked out great! Thanks.

  3. Cheers Dude, works a treat.

    When I get round to it I’ll write something to automate the provisioning of the file, I’ll post it back here when done.

  4. This is a very nice tutorial and thank you for doing this. I had someone ask me to make custom ringtones for their iphone and since I’m an androider I had no clue.

    I do have a suggestion for making this process easier with a gui.

    WinFF is a very handy GUI for ffmpeg and it allows you to add an mp3…select audio…choose MP4 audio…and then it transcode to m4v. using the ffmpeg command line without having to type it all in or use any .sh

    Check it out and maybe add that as an option in the tutorial.

  5. Thanks for the great instructions how to create the m4r file, edit the XML file and the locations where to find files.
    However I was unable to mount the device using ifuse according to instructions provided on the linked page. But this was solved as I have a jailbreaked phone and could SSH into it. WIthout the instructions about where to put file and what to edit I would not have succeeded.
    The ringtone could be selected immediately but before the ringtone was used when called I had to reboot the phone.

    jlinkels

  6. Verizon iPhone 4. I needed to create the iTunes_Control/Ringtones folder, which I did from the command line with mkdir. After which, the contact editor showed the new ringtone, but the calling the phone, it wasn’t being used. Rebooting the phone resolved this, and it picked up the next change without needing another reboot.

  7. Thanks for this tutorial, works great for ringtones on my IPhone 4. Do you know any solution to deal with sounds for SMS/e-mail messages, alarm clock etc? Suggest that its quite similar, but I couldnt find the according xml files. I also had to create the RingTones folder, but no trouble @cl.

  8. Edit: The new tone is sown up in the clock up but not for text messages etc.

  9. Everything can now be done with Audacity 2.0.0 itself. You can now export Audio directly to .m4a format and then rename the file to .m4r . Before that ensure you have installed ffmpeg + faac + libmp3lame0 libraries. Note this works on Ubuntu 12.04, not sure about other distro’s. Made simple :-)

  10. It seems like this tutorial will not work for iPhone 5, they have changed the database format from XML to SQLite.

  11. Hi,

    For newer versions of mplayer, you do not need to include the -vc flag in your mp32aac script (MPlayer SVN-r35234-4.7.2 (C) 2000-2012 MPlayer Team)

Leave a Reply

Site last updated October 20, 2012 @ 12:53 pm; This content last updated September 13, 2012 @ 1:15 am