Pydub - 将split_on_silence与最小长度/文件大小相结合

时间:2016-06-09 12:00:45

标签: python pydub

我有两个脚本,其中一个分割一定长度的音频,另一个在每次有静音段时分割音频。是否有可能在静音时分割音频,但仅在经过一段时间后?我需要在不超过5分钟的时间内分成几块视频。

忽略沉默分裂脚本:

from pydub import AudioSegment
#from pydub.utils import mediainfo
from pydub.utils import make_chunks
import math

#lac_audio = AudioSegment.from_file("Kalimba.mp3", "mp3")
#flac_audio.export("audio.mp3", format="mp3")
myaudio = AudioSegment.from_file("Kalimba.mp3" , "mp3")
channel_count = myaudio.channels    #Get channels
sample_width = myaudio.sample_width #Get sample width
duration_in_sec = len(myaudio) / 1000#Length of audio in sec
sample_rate = myaudio.frame_rate

print "sample_width=", sample_width 
print "channel_count=", channel_count
print "duration_in_sec=", duration_in_sec 
print "frame_rate=", sample_rate
bit_rate =16  #assumption , you can extract from mediainfo("test.wav") dynamically


wav_file_size = (sample_rate * bit_rate * channel_count * duration_in_sec) / 8
print "wav_file_size = ",wav_file_size


file_split_size = 10000000  # 10Mb OR 10, 000, 000 bytes
total_chunks =  wav_file_size // file_split_size

#Get chunk size by following method #There are more than one ofcourse
#for  duration_in_sec (X) -->  wav_file_size (Y)
#So   whats duration in sec  (K) --> for file size of 10Mb
#  K = X * 10Mb / Y

chunk_length_in_sec = math.ceil((duration_in_sec * 10000000 ) /wav_file_size)   #in sec
chunk_length_ms = chunk_length_in_sec * 1000
chunks = make_chunks(myaudio, chunk_length_ms)

#Export all of the individual chunks as wav files

for i, chunk in enumerate(chunks):
    chunk_name = "chunk{0}.mp3".format(i)
    print "exporting", chunk_name
    chunk.export(chunk_name, format="mp3")

忽略长度的拆分脚本:

from pydub import AudioSegment
from pydub.silence import split_on_silence

sound = AudioSegment.from_mp3("my_file.mp3")
chunks = split_on_silence(sound, 
    # must be silent for at least half a second
    min_silence_len=500,

    # consider it silent if quieter than -16 dBFS
    silence_thresh=-16

 )

for i, chunk in enumerate(chunks):
    chunk.export("/path/to/ouput/dir/chunk{0}.wav".format(i), format="wav")

2 个答案:

答案 0 :(得分:4)

我的建议是使用pydub.silence.split_on_silence(),然后根据需要重新组合细分,以便您拥有的文件大小与您定位的大小相同。

类似

from pydub import AudioSegment
from pydub.silence import split_on_silence

sound = AudioSegment.from_file("/path/to/file.mp3", format="mp3")
chunks = split_on_silence(
    sound,

    # split on silences longer than 1000ms (1 sec)
    min_silence_len=1000,

    # anything under -16 dBFS is considered silence
    silence_thresh=-16, 

    # keep 200 ms of leading/trailing silence
    keep_silence=200
)

# now recombine the chunks so that the parts are at least 90 sec long
target_length = 90 * 1000
output_chunks = [chunks[0]]
for chunk in chunks[1:]:
    if len(output_chunks[-1]) < target_length:
        output_chunks[-1] += chunk
    else:
        # if the last output chunk is longer than the target length,
        # we can start a new one
        output_chunks.append(chunk)

# now your have chunks that are bigger than 90 seconds (except, possibly the last one)

或者,您可以使用pydub.silence.detect_nonsilent()查找范围并自行决定将原始音频切片的位置

注意:我也在similar/duplicate github issue

上发布了此内容

答案 1 :(得分:0)

解决方法是使用mp3splt代替: http://mp3splt.sourceforge.net/mp3splt_page/documentation/man.html

-t TIME [&gt; MIN_TIME] 时间模式。此选项将创建无限数量的较小文件,其时间长度由TIME指定(具有与上述格式相同的格式)。将长文件拆分为较小的文件(例如CD的时间长度)很有用。调整选项(-a)可用于通过静音检测调整分割点。 &gt; MIN_TIME可用于指定最后一个段的理论最小轨道长度;它允许避免创建非常小的文件作为最后一个段。确保在使用MIN_TIME时引用参数 - &#34; TIME&gt; MIN_TIME&#34;。

然后,它可以在python中使用:

import os
os.system("mp3splt inputfile.mp3")