Python 超简单3行代码提取音乐高潮(批量提取实战教程)-Python 实用宝典

# Python 超简单3行代码提取音乐高潮(批量提取实战教程)

## 1.原理简介

1.遍历整首歌曲。

2.将选定长度的部分与其他部分比较并计算相似度，以查看是否重复。

3.寻找重复次数较大、且间隔长的片段。

## 2.代码编写

https://github.com/vivjay30/pychorus

```def compute_similarity_matrix_slow(self, chroma):
"""
虽然慢，但是直接的方法来计算区段相似矩阵
"""
num_samples = chroma.shape
time_time_similarity = np.zeros((num_samples, num_samples))
for i in range(num_samples):
for j in range(num_samples):
# 检测每一段的相似度
time_time_similarity[i, j] = 1 - (np.linalg.norm(chroma[:, i] - chroma[:, j]) / sqrt(12))
return time_time_similarity ```

### 2.1 安装所需要的项目

`pip install pychorus`

### 2.2 编写代码

```from pychorus import find_and_output_chorus
chorus_start_sec = find_and_output_chorus("你的音乐文件", "输出高潮部分文件", 要多少秒的高潮部分) ```

## 3.效果检验

```# 提取音乐高潮部分
from pychorus import find_and_output_chorus
chorus_start_sec = find_and_output_chorus("孤芳自赏.mp3", "孤芳自赏_high.wav", 40)```

## 4.批量提取

```# Python 实用宝典
# 提取音乐高潮部分
# 2020/06/11

import os
import sys
from pychorus import find_and_output_chorus

def extract_all_file(files_path):
"""
批量提取音乐高潮

Args:
files_path (str): 文件夹路径
"""

# 文件夹路径
modpath = os.path.dirname(os.path.abspath(sys.argv))

for filepath in os.listdir(files_path):

# 路径处理
datapath = os.path.join(modpath, files_path + filepath)

# output文件夹是否存在
targets = f"{modpath}\\output\\"
if not os.path.exists(targets):
os.makedirs(targets)

# 提取音乐高潮至当前output文件夹下
find_and_output_chorus(
datapath, f"{targets}{filepath.split('.')}_high.wav", 40
)

extract_all_file("F:\\push\\20200611\\music\\")```

Python 批量下载网易云音乐歌单

Python 制作音乐高潮副歌提取器

Python Django快速开发音乐高潮提取网(1)

Python Django快速开发音乐高潮提取网(2)

Python Django快速开发音乐高潮提取网(3)

Python 超方便超快速剪辑音乐

Python 提取音乐频谱并可视化

​Python实用宝典 (pythondict.com)
{"ft_lang_label":"__label__zh","ft_lang_prob":0.8603979,"math_prob":0.6144209,"size":2196,"snap":"2022-40-2023-06","text_gpt3_token_len":1392,"char_repetition_ratio":0.11313868,"word_repetition_ratio":0.026143791,"special_character_ratio":0.23224044,"punctuation_ratio":0.18855219,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96105456,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-27T16:36:59Z\",\"WARC-Record-ID\":\"<urn:uuid:4b416987-4ae2-47e2-ae93-ed9275bbb28c>\",\"Content-Length\":\"431354\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e6697fe8-c25f-4526-8615-9287673f1d4a>\",\"WARC-Concurrent-To\":\"<urn:uuid:19e56901-d0cc-43e2-a6ca-ab0a3143a7f8>\",\"WARC-IP-Address\":\"129.226.118.171\",\"WARC-Target-URI\":\"https://pythondict.com/life-intelligent/python-high-music/\",\"WARC-Payload-Digest\":\"sha1:CSOXLHRG2TMRBYJYBFO3L7LHIT6ZFDZF\",\"WARC-Block-Digest\":\"sha1:QRZM3ZLSBGH4P66VZV3CRAIOSAFSVBJY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030335054.79_warc_CC-MAIN-20220927162620-20220927192620-00499.warc.gz\"}"}