Pydub sound crashes when splitting / joining mp3

I am experimenting with pydub, which I really like, however I am having a problem when splitting / combining an mp3 file.

I need to create a series of small pieces of audio on a server that will be sent sequentially to a web browser and played using an element <audio/>. I need the sound reproduction to be “seamless” without audible associations between the individual fragments. At the moment, however, the connections between the individual bits of sound are quite obvious, sometimes there is a short silence, and sometimes a strange sound failure.

In my proof of concept code, I took one big mp3 and split it into 1 second chunks as follows:

song = AudioSegment.from_mp3('my.mp3')
song_pos = 0
while song_pos < 100:
    p1 = song_pos * 1000
    p2 = p1 + 1000

    segment = song[p1:p2] # 1 second of audio

    output = StringIO.StringIO()
    segment.export(output, format="mp3")
    client_data = output.getvalue() # send this to client

    song_pos += 1

client_data http-:

socket.send("HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: audio/mp3\r\n\r\n")

socket.send(client_data)

- , , ?

+4
3

:

, MP3, ffmpeg, ( ).

, , wave, gzip . (, flac), , , , .

, , , , . 100 (), . MP3 , . , 1 - "" ( ) - (, 20 50 , "".

() , " " ( , , ) .

, , :

( ) . - (CBR), 1- , . , 256 / CBR, 256 .

+1

, , , . ,

p2 = p1 + 1001

, . .

, , , . raw_data AudioSegment() .

0

- , , ; , .

I am not familiar with this software, but when coding Niels Werner's sentences, you can try:

song = AudioSegment.from_mp3('my.mp3')
song_pos = 0

# begin with a millisecond of blank
segment = AudioSegment.silent(duration=1)
# append all your pieces to it
while song_pos < 100:
    p1 = song_pos * 1000
    p2 = p1 + 1000
    #append an item to your segment with several milliseconds of crossfade
    segment.append(song[p1:p2], crossfade=50)
    song_pos += 1

# then pass it on to your client outside of your loop
output = StringIO.StringIO()
segment.export(output, format="mp3")
client_data = output.getvalue() # send this to client

depending on how low / high the frequency of what you are connecting to, you need to adjust the crossfade time for mixing; low frequency will require more fading.

-1
source

Source: https://habr.com/ru/post/1671690/


All Articles