Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python extract the best part of music?

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to extract the best part of music from Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Brief introduction of principle

I don't know if you have this experience, but most of the time, the best part of a song is usually the part that repeats the most. So we can propose our algorithm according to this feature:

1. Go through the whole song.

two。 Compare the portion of the selected length with other parts and calculate the similarity to see if it is duplicated.

3. Look for segments with large repetition times and long intervals.

two。 Code writing

In order to avoid building wheels, we found similar projects that others had already done.

We only need to analyze the core part of the source code, that is, the source code for similar sections, to see if it meets the requirements of our project:

As you can see, this part of the code is to do the second step of our algorithm, to calculate the similarity between fragments.

The similarity function used in the detection is as follows:

This is mainly because the song consists of a collection of frames of 12 basic notes. V1 and v2 are the note vectors of any two pieces of music. If the two pieces of music are very similar, then the formula on the right will be close to 0. If the score of the formula 1-right is very high, it means that the two pieces of music are very similar.

Let's see how to use this project to find the best part of the music, which is actually very simple.

2.1 items required for installation

You can install the project through pip. If you have not installed the Python-related environment, it is recommended to install it first. The PIP installation instructions are as follows:

Writing code with pip install pychorus2.2

In fact, this package is quite easy to use, if we just want to extract the best part of the song:

From pychorus import find_and_output_choruschorus_start_sec = find_and_output_chorus ("your music file", "the target path of the extraction result", the best part of how many seconds it takes)

That's right, two lines of code solved. If you want to know some detailed details, such as outputting a similar matrix or visualizing the results, it is recommended to read the instructions for the project in github. Let's test the effect next.

3. Effect test

Take "narcissism" as an example, let's try the power of this extractor. Write code:

# extract the best part of music from pychorus import find_and_output_choruschorus_start_sec = find_and_output_chorus ("narcissistic .mp3", "narcissistic _ high.wav", 40)

Excellent! Extracted what I wanted in mind. You can also try to extract the best part of your favorite music according to our tutorial today.

4. Batch extraction

Just now, just finished the extraction of the best part of a single song, if you want to extract the best part of the music under the entire folder, you can do this:

Text version code:

# Python practical Treasure Book # extract the best part of music # 2020/06/11import osimport sysfrom pychorus import find_and_output_chorusdef extract_all_file (files_path): "" batch extract the best part of music Args: files_path (str): folder path "" # folder path modpath = os.path.dirname (os.path.abspath (sys.argv [0]) For filepath in os.listdir (files_path): # path processing datapath = os.path.join (modpath Files_path + filepath) # output folder whether targets = f "{modpath}\ output\" if not os.path.exists (targets): os.makedirs (targets) # extract the best part of music to find_and_output_chorus (datapath, f "{targets} {filepath.split ('.') [0]} _ high.wav" under the current output folder. 40) extract_all_file ("F:\ push\ 20200611\ music\") "how to extract the best part of music by Python" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report