簡體   English   中英

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")

我的建議是使用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()來查找范圍並自行決定切片原始音頻的位置

注意:我也在類似/重復的github問題上發布了這個問題

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

-t TIME [> MIN_TIME]時間模式。 此選項將創建無限數量的較小文件,其時間長度由TIME指定(具有與上述格式相同的格式)。 將長文件拆分為較小的文件(例如CD的時間長度)很有用。 調整選項(-a)可用於通過靜音檢測調整分割點。 > MIN_TIME可用於指定最后一段的理論最小軌道長度; 它允許避免創建非常小的文件作為最后一個段。 確保在使用MIN_TIME - “TIME> MIN_TIME”時引用參數。

然后,它可以在python中使用,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM