失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python批量剪辑音频pydub

python批量剪辑音频pydub

时间:2024-07-09 07:05:04

相关推荐

python批量剪辑音频pydub

使用 os、pydub 模块

# 设置默认注释:settings->editor->file and code Templatesimport os # 文件系统操作对象from pydub import AudioSegmentdef audio_sound(path, length):for file_name in os.listdir(path):# print('.' in file_name)if '.' in file_name:file_path = os.path.join(path, file_name)print(file_path)# 读取文件有很多方式,有直接from_file(),也有from_mp3()、from_wav(),下面的两个读取语句是等价的:# sound = AudioSegment.from_file("mp3/正常.m4a", "m4a")# sound = AudioSegment.from_mp3("mp3/15test.mp3")sound = AudioSegment.from_file(file_path)print(len(sound))print('时长:{} s'.format(len(sound) / 1000))chunk_num = int(len(sound) / 1000 / length)start_time = 0end_time = length * 1000save_file_path = os.path.join(path, file_name[:-4])if not os.path.exists(save_file_path):os.makedirs(save_file_path)for n in range(chunk_num):# print(start_time, end_time)# 切割文件part = sound[start_time:end_time]# 保存路径save_name = os.path.join(save_file_path, '{}{}.wav'.format(n+1, file_name[:-4]))# 保存文件part.export(save_name, format="wav")start_time += lengthend_time += lengthif __name__ == '__main__':# 音频所在目录dir_path = 'mp3'# 每段剪辑长度 3saudio_length = 3# 执行audio_sound(dir_path, audio_length)

如果觉得《python批量剪辑音频pydub》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。