Question

I'm writing an application for making ringtones for the iPhone. It's just for fun. Here's what I've done till now. (Mind you, i'm a beginner in Python !) So I open my mp3 file in binary mode. read the whole file. Convert it into a list. Use list slicing to split the file. Save this new split into a new mp3 file. This works fine. I however want to the ringtones to have a max of 30 seconds play time and I want the user to choose what portion of the file he wants as the ringtone. Can anyone guide me in the right direction ? Thanks

Here's my code till now :

f = open("Bruno Mars - Locked Out Of Heaven [OFFICIAL VIDEO].mp3", 'rb').read()
mp3 = list(f)
fo = open("newFile.mp3", "wb")
print(mp3[0:1300000]) 
fo.write(bytes(mp3[0:1300000]))

Here's what I got after some coding:

import os
f = open("Bruno Mars - Locked Out Of Heaven [OFFICIAL VIDEO].mp3", 'rb').read()
fileSize = os.path.getsize("Bruno Mars - Locked Out Of Heaven [OFFICIAL VIDEO].mp3")
print("Size of the whole file",fileSize)
mp3 = list(f)
bitRate = int(input("Enter the bit rate of your file"))
size_mbps = bitRate*(15/2048)
print("MB per minute :",size_mbps)
second_size = int((size_mbps/60)*(10**6))
print("Size of each second :",second_size)
start_length = int(input("Enter the start time (in seconds)"))
end_length = int(input("Enter the end time (in seconds)"))
start_size = int(second_size*start_length)
end_size = int(second_size*end_length)
fo = open("newFile.mp3", "wb")
fo.write(bytes(mp3[start_size:end_size]))

It works fine but I need to tweak it a little more. Any input on this code ?

Was it helpful?

Solution

If you want to use a higher-level language like c++ to implement a custom MP3 decoder, take a look a this question, time length of an mp3 file.

Otherwise, there are other libraries that do just what you need:

PyMad -

import mad

mf = mad.MadFile("foo.mp3")
track_length_in_milliseconds = mf.total_time()

audioread -

audio = audioread.audio_open('/path/to/mp3')
print f.channels, f.samplerate, f.duration

Mutagen -

from mutagen.mp3 import MP3
audio = MP3("example.mp3")
print audio.info.length

There are many more, but for now you can start with these.

OTHER TIPS

You have to be a bit careful since mp3 frames do not always stand on their own, which is one of the reasons high end music tools will first convert to WAV or something like it, but as you remarked, it does work up till a certain level.

You could probably benefit from looking at the code in the following project (note that it is GPL licensed though):

http://sourceforge.net/projects/pymp3cut/

the interesting bits:

http://pymp3cut.cvs.sourceforge.net/viewvc/pymp3cut/pymp3cut/pmpcmp3.py?revision=1.2&view=markup

Once you extracted the actual music part, the rough idea is that you base yourself on the bitrate to get the relation between the framesize, seconds, filesize and byte content, which then tells you where to split the bytes to get the starting time and duration that you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top