Pydub - 结合 split_on_silence 与最小长度/文件大小
Pydub - combine split_on_silence with minimum length / file size
我有两个脚本,一个会分割一定长度的音频,另一个会在每次有无声段落时分割音频。是否可以在静音时拆分音频,但只有在经过一定时间后才能拆分音频?我需要不少于 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")
解决方法是改用mp3splt:
http://mp3splt.sourceforge.net/mp3splt_page/documentation/man.html
-t 时间[>MIN_TIME]
时间模式。此选项将创建无限数量的较小文件,这些文件具有由 TIME 指定的固定时间长度(其格式与上述相同)。将长文件拆分成更小的文件(例如 CD 的时间长度)很有用。调整选项 (-a) 可用于通过静音检测调整分割点。 >MIN_TIME可用于指定最后一段的理论最小磁道长度;它允许避免创建非常小的文件作为最后一段。使用 MIN_TIME - "TIME>MIN_TIME".
时确保引用参数
然后,它可以像这样在python中使用:
import os
os.system("mp3splt inputfile.mp3")
我的建议是使用 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()
查找范围并自行决定在何处分割原始音频
上发布了这个
我有两个脚本,一个会分割一定长度的音频,另一个会在每次有无声段落时分割音频。是否可以在静音时拆分音频,但只有在经过一定时间后才能拆分音频?我需要不少于 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")
解决方法是改用mp3splt: http://mp3splt.sourceforge.net/mp3splt_page/documentation/man.html
-t 时间[>MIN_TIME] 时间模式。此选项将创建无限数量的较小文件,这些文件具有由 TIME 指定的固定时间长度(其格式与上述相同)。将长文件拆分成更小的文件(例如 CD 的时间长度)很有用。调整选项 (-a) 可用于通过静音检测调整分割点。 >MIN_TIME可用于指定最后一段的理论最小磁道长度;它允许避免创建非常小的文件作为最后一段。使用 MIN_TIME - "TIME>MIN_TIME".
时确保引用参数然后,它可以像这样在python中使用:
import os
os.system("mp3splt inputfile.mp3")
我的建议是使用 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()
查找范围并自行决定在何处分割原始音频