当前位置:   article > 正文

【Python】音频合并与切分——采用moviepy包实现_moviepy音频切片

moviepy音频切片

 注: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”

  1. import os
  2. import numpy as np
  3. from moviepy.editor import *
  4. def audio_split_by_duration(file_name, split_duration=60):
  5. """
  6. Split the audio file into segments with the given duration.
  7. The split segments are with the same extension name as the original file.
  8. Author: AbaloneVH Version: 2024/03/04 14:10
  9. Parameters
  10. -----------
  11. file_name: name of the file.
  12. split_duration: duration for each segment. The duration of the last segment may be less than it. Default: 60 s.
  13. Examples
  14. -----------
  15. >> audio_split_by_duration(“D:/Desktop/a.mp3”, split_duration=120) # split_duration is 120 s.
  16. """
  17. audio = AudioFileClip(file_name) # load the audio file
  18. name, ext = os.path.splitext(file_name)
  19. total_duration = audio.duration # total duration of the audio file
  20. split_points = list(range(0, int(total_duration), split_duration))
  21. split_points.append(int(total_duration))
  22. file_list = []
  23. for i in range(len(split_points) - 1):
  24. start_time = split_points[i]
  25. end_time = split_points[i + 1]
  26. split_audio = audio.subclip(start_time, end_time) # split the audio file
  27. split_audio.write_audiofile(f"{name}_{i}"+ext) # write the split segments
  28. file_list.append(f"{name}_{i}"+ext)
  29. audio.close()
  30. return file_list
  31. def audio_split_by_size(file_name, split_size=1024):
  32. """
  33. Split the audio file into segments with the given size.
  34. The split segments are with the same extension name as the original file.
  35. Author: AbaloneVH Version: 2024/03/04 14:10
  36. Parameters
  37. -----------
  38. file_name: name of the file.
  39. split_size: maximal size of each segment. Unit: KB. Default: 1024 KB, i.e., 1 MB.
  40. Examples
  41. -----------
  42. >> audio_split_by_size(“D:/Desktop/a.mp3”, split_size=1024*2) # split_size = 2MB
  43. """
  44. audio = AudioFileClip(file_name) # load the audio file
  45. name, ext = os.path.splitext(file_name)
  46. file_size = os.path.getsize(file_name)/1024 # unit: KB
  47. num_seg = int(np.ceil(file_size/split_size)) + 1
  48. total_duration = audio.duration
  49. split_duration = int(np.ceil(total_duration/num_seg))
  50. split_points = list(range(0, int(total_duration), split_duration))
  51. split_points.append(int(total_duration))
  52. file_list = []
  53. for i in range(len(split_points) - 1):
  54. start_time = split_points[i]
  55. end_time = split_points[i + 1]
  56. split_audio = audio.subclip(start_time, end_time) # split the audio file
  57. split_audio.write_audiofile(f"{name}_{i}"+ext) # write the split segments
  58. file_list.append(f"{name}_{i}"+ext)
  59. audio.close()
  60. return file_list
  61. def audio_concat(file_list, file_concat_name):
  62. """
  63. Concatenate the audio files into one
  64. Author: AbaloneVH Version: 2024/03/04 14:10
  65. Parameters
  66. -----------
  67. file_list: name list of the audio files to be concatenated
  68. file_concat_name: name of the concatenated audio file
  69. Examples
  70. -----------
  71. >> audio_concat([“D:/Desktop/a.mp3”, “D:/Desktop/b.mp3”], file_concat_name=“D:/Desktop/ab.mp3”)
  72. """
  73. audio_clips = []
  74. for sub_file__name in file_list:
  75. audio_clips.append(AudioFileClip(sub_file__name))
  76. audio_all = concatenate_audioclips(audio_clips)
  77. audio_all.write_audiofile(file_concat_name)
  78. return file_concat_name
  79. # Example
  80. # 1. Split "P1.mp3" into segments, the size of each segment is less than 2 MB
  81. fileName = "P1.mp3" # name of the audio file to be split
  82. fileList = audio_split_by_size(fileName, split_size=1024*2)
  83. # 2. Concatenate the first two segments into one audio file "P_all.mp3"
  84. fileConcatName = "P_all.mp3" # name of the concatenated audio file
  85. print(audio_concat(fileList[:2], fileConcatName))

参考资料【Python】如何高效把一个mp3切割成多个30秒的片段(附实操)_python mp3切片-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/539715
推荐阅读
相关标签
  

闽ICP备14008679号