注:moviepy包也可以进行视频的合并和切分,暂时没有需求,所以不予实现。
如果觉得好用,请不要吝啬你的点赞哟~
另外,如果有其他需求,欢迎私信我,有条件的话,我会尽可能实现。
# 内容描述:采用moviepy包进行音频的合并和切分 # Author: AbaloneVH Version: 2024/03/04 14:10 # 前置要求:采用 pip install moviepy 安装该包 # 具体功能 # 1. 根据给定子音频时长切分音频。 # 切分后的音频名为原音频名加“_”和序号。除最后一个子音频外,所有子音频的时长都是给定的子音频时长。 # 示例:audio_split_by_duration(“D:/Desktop/a.mp3”, split_duration=120) # 子音频时长为 120 秒 # 2. 根据给定子音频大小切分音频。 # 切分后的音频名为原音频名加“_”和序号。除最后一个子音频外,所有子音频的大小均不超过给定子音频大小。 # 【目前的切分方式为按时长均匀切分,该优化方式可以根据需求进一步优化】 # 示例:audio_split_by_size(“D:/Desktop/a.mp3”, split_size=1024*2) # 子音频大小不超过 2 MB # 3. 根据给定的子音频列表,按顺序拼接给定的子音频。 # 需指定拼接后音频的名称。 # 示例:audio_concat([“D:/Desktop/a.mp3”, “D:/Desktop/b.mp3”], file_concat_name=“D:/Desktop/ab.mp3”) # 拼接”a.mp3“和”b.mp3“文件,得到的文件为”ab.mp3”
import os
import numpy as np
from moviepy.editor import *
def audio_split_by_duration(file_name, split_duration=60):
"""
Split the audio file into segments with the given duration.
The split segments are with the same extension name as the original file.
Author: AbaloneVH Version: 2024/03/04 14:10
Parameters
-----------
file_name: name of the file.
split_duration: duration for each segment. The duration of the last segment may be less than it. Default: 60 s.
Examples
-----------
>> audio_split_by_duration(“D:/Desktop/a.mp3”, split_duration=120) # split_duration is 120 s.
"""
audio = AudioFileClip(file_name) # load the audio file
name, ext = os.path.splitext(file_name)
total_duration = audio.duration # total duration of the audio file
split_points = list(range(0, int(total_duration), split_duration))
split_points.append(int(total_duration))
file_list = []
for i in range(len(split_points) - 1):
start_time = split_points[i]
end_time = split_points[i + 1]
split_audio = audio.subclip(start_time, end_time) # split the audio file
split_audio.write_audiofile(f"{name}_{i}"+ext) # write the split segments
file_list.append(f"{name}_{i}"+ext)
audio.close()
return file_list
def audio_split_by_size(file_name, split_size=1024):
"""
Split the audio file into segments with the given size.
The split segments are with the same extension name as the original file.
Author: AbaloneVH Version: 2024/03/04 14:10
Parameters
-----------
file_name: name of the file.
split_size: maximal size of each segment. Unit: KB. Default: 1024 KB, i.e., 1 MB.
Examples
-----------
>> audio_split_by_size(“D:/Desktop/a.mp3”, split_size=1024*2) # split_size = 2MB
"""
audio = AudioFileClip(file_name) # load the audio file
name, ext = os.path.splitext(file_name)
file_size = os.path.getsize(file_name)/1024 # unit: KB
num_seg = int(np.ceil(file_size/split_size)) + 1
total_duration = audio.duration
split_duration = int(np.ceil(total_duration/num_seg))
split_points = list(range(0, int(total_duration), split_duration))
split_points.append(int(total_duration))
file_list = []
for i in range(len(split_points) - 1):
start_time = split_points[i]
end_time = split_points[i + 1]
split_audio = audio.subclip(start_time, end_time) # split the audio file
split_audio.write_audiofile(f"{name}_{i}"+ext) # write the split segments
file_list.append(f"{name}_{i}"+ext)
audio.close()
return file_list
def audio_concat(file_list, file_concat_name):
"""
Concatenate the audio files into one
Author: AbaloneVH Version: 2024/03/04 14:10
Parameters
-----------
file_list: name list of the audio files to be concatenated
file_concat_name: name of the concatenated audio file
Examples
-----------
>> audio_concat([“D:/Desktop/a.mp3”, “D:/Desktop/b.mp3”], file_concat_name=“D:/Desktop/ab.mp3”)
"""
audio_clips = []
for sub_file__name in file_list:
audio_clips.append(AudioFileClip(sub_file__name))
audio_all = concatenate_audioclips(audio_clips)
audio_all.write_audiofile(file_concat_name)
return file_concat_name
# Example
# 1. Split "P1.mp3" into segments, the size of each segment is less than 2 MB
fileName = "P1.mp3" # name of the audio file to be split
fileList = audio_split_by_size(fileName, split_size=1024*2)
# 2. Concatenate the first two segments into one audio file "P_all.mp3"
fileConcatName = "P_all.mp3" # name of the concatenated audio file
print(audio_concat(fileList[:2], fileConcatName))