There are a lot of libs to work with mp3 tags, but I need just 2 functions - split mp3 file in 2 parts and the second one to merge 5 mp3.
Can you suggest anything? Thanks!
First, we write a helper function to get an extension of any file with Python, we'll use this to get the extension of the audio file automatically so we can pass it to PyDub. Second, we load the audio files using the AudioSegment. from_file() method which expects the audio path, and the extension.
Play Mp3 Files With Python Using the playsound Package One simple way to play an mp3 file using Python is with the help of playsound library.
I wrote a library (pydub) for pretty much this exact use case:
from pydub import AudioSegment sound = AudioSegment.from_mp3("/path/to/file.mp3") # len() and slicing are in milliseconds halfway_point = len(sound) / 2 second_half = sound[halfway_point:] # Concatenation is just adding second_half_3_times = second_half + second_half + second_half # writing mp3 files is a one liner second_half_3_times.export("/path/to/new/file.mp3", format="mp3")
If you'd like to add silence between parts of a sound:
two_sec_silence = AudioSegment.silent(duration=2000) sound_with_gap = sound[:1000] + two_sec_silence + sound[1000:]
Have a look at the MP3 file structure on Wikipedia. Use binary read mode in python to edit the MP3 file. s = open(file_name, 'rb').read()
will put the whole file into a string object representing the raw bytes in your file (e.g. \xeb\xfe\x80
). You can then search and edit the string, addressing the byte offsets with indeces using brackets: s[n]
. Finally, just do a binary write of the MP3 frames you want in your new file(s), appending the ID3 header to the set of frames that you want to make up each file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With