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 ?
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()
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.