{"id":174502,"date":"2024-05-08T18:32:25","date_gmt":"2024-05-08T10:32:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174502.html"},"modified":"2024-05-08T18:32:31","modified_gmt":"2024-05-08T10:32:31","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%ae%9e%e7%8e%b0%e6%9c%80%e7%ae%80%e5%8d%95%e7%9a%84%e5%90%ac%e6%ad%8c%e8%af%86%e6%9b%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/174502.html","title":{"rendered":"\u5982\u4f55\u7528python\u5b9e\u73b0\u6700\u7b80\u5355\u7684\u542c\u6b4c\u8bc6\u66f2"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27052052\/7554e398-9feb-4e6f-88b3-fe5f885cdb6e.webp\" alt=\"\u5982\u4f55\u7528python\u5b9e\u73b0\u6700\u7b80\u5355\u7684\u542c\u6b4c\u8bc6\u66f2\" \/><\/p>\n<p><p>\u5728Python\u4e2d\u5b9e\u73b0\u6700\u7b80\u5355\u7684\u542c\u6b4c\u8bc6\u66f2\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7279\u5b9a\u5e93\u548cAPI\u6765\u5b8c\u6210\u3002<strong>\u6838\u5fc3\u6280\u672f\u5305\u62ec\u97f3\u9891\u6307\u7eb9\u751f\u6210\u3001\u97f3\u9891\u6570\u636e\u5e93\u5339\u914d\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9API<\/strong>\u3002\u5176\u4e2d\uff0c\u97f3\u9891\u6307\u7eb9\u751f\u6210\u662f\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u6700\u5173\u952e\u7684\u4e00\u73af\u3002\u97f3\u9891\u6307\u7eb9\u662f\u6307\u4ece\u97f3\u9891\u6587\u4ef6\u4e2d\u63d0\u53d6\u7684\u4e00\u7ec4\u7528\u4e8e\u8868\u793a\u8be5\u97f3\u9891\u5185\u5bb9\u7684\u72ec\u7279\u6570\u636e\u3002\u901a\u8fc7\u6bd4\u8f83\u97f3\u9891\u6307\u7eb9\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u636e\u5e93\u4e2d\u67e5\u627e\u4e0e\u4e4b\u76f8\u5339\u914d\u7684\u6b4c\u66f2\u3002\u8fd9\u4e00\u6280\u672f\u4e0d\u4ec5\u4f7f\u7528\u5728\u542c\u6b4c\u8bc6\u66f2\u4e0a\uff0c\u8fd8\u5e7f\u6cdb\u5e94\u7528\u4e8e\u97f3\u4e50\u7248\u6743\u68c0\u6d4b\u7b49\u9886\u57df\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f1a\u5229\u7528\u5e93\u5982<code>pydub<\/code>\u6216<code>librosa<\/code>\u5bf9\u97f3\u9891\u6587\u4ef6\u8fdb\u884c\u9884\u5904\u7406\uff0c\u7136\u540e\u901a\u8fc7\u7279\u5b9a\u7b97\u6cd5\uff08\u5982FFT\u5feb\u901f\u5085\u7acb\u53f6\u8f6c\u6362\uff09\u63d0\u53d6\u97f3\u9891\u7279\u5f81\u751f\u6210\u97f3\u9891\u6307\u7eb9\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8fd9\u4e9b\u97f3\u9891\u6307\u7eb9\u4e0e\u97f3\u4e50\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5339\u914d\uff0c\u627e\u5230\u6700\u8fd1\u4f3c\u7684\u97f3\u9891\u6587\u4ef6\uff0c\u8fdb\u800c\u8bc6\u522b\u51fa\u5f53\u524d\u6240\u64ad\u653e\u7684\u6b4c\u66f2\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u8bc6\u66f2\u7a0b\u5e8f\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u5904\u7406\u97f3\u9891\u6240\u9700\u7684Python\u5e93\u3002<code>librosa<\/code>\u662f\u5904\u7406\u97f3\u9891\u548c\u97f3\u4e50\u7684Python\u5305\uff0c\u5b83\u63d0\u4f9b\u4e86\u6784\u5efa\u97f3\u4e50\u4fe1\u606f\u68c0\u7d22\u7cfb\u7edf\u6240\u9700\u7684\u5168\u90e8\u5de5\u5177\u3002\u540c\u65f6\uff0c<code>NumPy<\/code>\u4e5f\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u7528\u4e8e\u8fdb\u884c\u9ad8\u6548\u7684\u79d1\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install librosa numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pydub<\/code>\u540c\u6837\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u97f3\u9891\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u8bfb\u3001\u5199\u3001\u5207\u5272\u3001\u8fde\u63a5\u97f3\u9891\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install pydub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u97f3\u9891\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u97f3\u9891\u9884\u5904\u7406\u662f\u8bc6\u522b\u8fc7\u7a0b\u4e2d\u7684\u7b2c\u4e00\u6b65\u3002\u8fd9\u4e00\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u97f3\u9891\u7684\u52a0\u8f7d\u3001\u683c\u5f0f\u8f6c\u6362\u3001\u5206\u6bb5\u3001\u53bb\u566a\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import librosa<\/p>\n<h2><strong>\u52a0\u8f7d\u97f3\u9891\u6587\u4ef6<\/strong><\/h2>\n<p>audio_path = &#039;your_song.mp3&#039;<\/p>\n<p>y, sr = librosa.load(audio_path, sr=None)<\/p>\n<h2><strong>\u8fd9\u91cc\u7684y\u662f\u97f3\u9891\u6ce2\u5f62\u6570\u636e\uff0csr\u662f\u91c7\u6837\u7387<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Librosa\u52a0\u8f7d\u97f3\u9891\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u4fdd\u6301\u539f\u6709\u7684\u91c7\u6837\u7387\uff08\u901a\u8fc7\u8bbe\u7f6e<code>sr=None<\/code>\uff09\u6216\u5bf9\u5176\u8fdb\u884c\u4e0b\u91c7\u6837\u4ee5\u51cf\u5c0f\u6570\u636e\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u63d0\u53d6\u97f3\u9891\u7279\u5f81\u5e76\u751f\u6210\u6307\u7eb9<\/h3>\n<\/p>\n<p><p>\u63d0\u53d6\u97f3\u9891\u7279\u5f81\u662f\u751f\u6210\u97f3\u9891\u6307\u7eb9\u7684\u5fc5\u8981\u6b65\u9aa4\u3002\u97f3\u9891\u7279\u5f81\u53ef\u4ee5\u662f\u9891\u8c31\u3001\u8282\u594f\u3001\u8c03\u6027\u7b49\u97f3\u4e50\u57fa\u7840\u8981\u7d20\u7684\u6570\u5b66\u8868\u8fbe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import librosa<\/p>\n<h2><strong>\u63d0\u53d6\u6885\u5c14\u9891\u8c31\u7279\u5f81<\/strong><\/h2>\n<p>melspec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)<\/p>\n<p>log_melspec = librosa.amplitude_to_db(melspec)<\/p>\n<h2><strong>\u6885\u5c14\u9891\u8c31\u53ef\u4ee5\u7528\u4f5c\u97f3\u9891\u7684&quot;\u6307\u7eb9&quot;<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06\u97f3\u9891\u8f6c\u6362\u4e3a\u6885\u5c14\u9891\u8c31\uff0c\u8fd9\u79cd\u8868\u793a\u65b9\u6cd5\u5bf9\u4e8e\u4eba\u8033\u7684\u611f\u77e5\u4e0e\u97f3\u4e50\u5185\u5bb9\u7684\u8868\u5f81\u975e\u5e38\u6709\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5339\u914d\u97f3\u9891\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u5f97\u5230\u97f3\u9891\u6307\u7eb9\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u5728\u97f3\u9891\u6570\u636e\u5e93\u4e2d\u627e\u5230\u5339\u914d\u9879\u3002\u8fd9\u4e00\u6b65\u9aa4\u901a\u5e38\u9700\u8981\u9884\u5148\u6784\u5efa\u4e00\u4e2a\u5305\u542b\u5927\u91cf\u97f3\u4e50\u6307\u7eb9\u7684\u6570\u636e\u5e93\uff0c\u5e76\u4e3a\u5176\u8bbe\u8ba1\u9ad8\u6548\u7684\u67e5\u8be2\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u97f3\u9891\u6307\u7eb9\u6570\u636e\u5e93\u548c\u67e5\u8be2\u7b97\u6cd5<\/p>\n<p>def find_match(audio_fingerprint):<\/p>\n<p>    # \u5b9e\u73b0\u6570\u636e\u5e93\u7684\u67e5\u8be2\u903b\u8f91<\/p>\n<p>    # \u8fd4\u56de\u5339\u914d\u5ea6\u6700\u9ad8\u7684\u97f3\u9891ID<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9API<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u81ea\u5efa\u6570\u636e\u5e93\u5339\u914d\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5229\u7528\u7b2c\u4e09\u65b9API\u5982ACRCloud\u3001Shazam\u7b49\u6765\u5b9e\u73b0\u5feb\u901f\u7684\u542c\u6b4c\u8bc6\u66f2\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import acrcloud<\/p>\n<p>config = {<\/p>\n<p>    &#039;host&#039;: &#039;identify-eu-west-1.acrcloud.com&#039;,<\/p>\n<p>    &#039;access_key&#039;: &#039;your_access_key&#039;,<\/p>\n<p>    &#039;access_secret&#039;: &#039;your_access_secret&#039;<\/p>\n<p>}<\/p>\n<h2><strong>\u4f7f\u7528ACRCloud\u7684\u97f3\u9891\u8bc6\u522b\u670d\u52a1<\/strong><\/h2>\n<p>acrcloud.recognize_audio(&#039;path_to_your_audio_file&#039;, config)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u9700\u8981\u6784\u5efa\u590d\u6742\u97f3\u9891\u6307\u7eb9\u6570\u636e\u5e93\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u5feb\u901f\u4e14\u51c6\u786e\u7684\u97f3\u4e50\u8bc6\u522b\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong>\uff0c\u7528Python\u5b9e\u73b0\u6700\u7b80\u5355\u7684\u542c\u6b4c\u8bc6\u66f2\u6d89\u53ca\u5230\u97f3\u9891\u9884\u5904\u7406\u3001\u7279\u5f81\u63d0\u53d6\u3001\u97f3\u9891\u6307\u7eb9\u751f\u6210\u3001\u6570\u636e\u5e93\u5339\u914d\u6216\u7b2c\u4e09\u65b9API\u8c03\u7528\u7b49\u591a\u4e2a\u73af\u8282\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u8d44\u6e90\u60c5\u51b5\u9009\u62e9\u6700\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u5c3d\u7ba1\u5728\u6784\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5907\u3001\u5339\u914d\u6548\u7387\u9ad8\u7684\u8bc6\u66f2\u7cfb\u7edf\u8fc7\u7a0b\u4e2d\u5b58\u5728\u8bb8\u591a\u6311\u6218\uff0c\u4f46\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\u7684\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u9010\u6b65\u5b9e\u73b0\u4e00\u4e2a\u57fa\u672c\u7684\u542c\u6b4c\u8bc6\u66f2\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4f7f\u7528Python\u5982\u4f55\u5b9e\u73b0\u97f3\u9891\u5904\u7406\u548c\u5206\u6790\uff1f<\/strong><br \/>\n\u8981\u5b9e\u73b0\u542c\u6b4c\u8bc6\u66f2\u7684\u529f\u80fd\uff0c\u9996\u5148\u9700\u8981\u5bf9\u97f3\u9891\u6587\u4ef6\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\u3002Python\u4e2d\u6709\u8bb8\u591a\u5f3a\u5927\u7684\u97f3\u9891\u5904\u7406\u5e93\uff0c\u4f8b\u5982Librosa\u548cPydub\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u8bfb\u53d6\u97f3\u9891\u6587\u4ef6\uff0c\u63d0\u53d6\u7279\u5f81\uff0c\u5982\u9891\u8c31\u56fe\u6216MFCC\uff0c\u7136\u540e\u7528\u8fd9\u4e9b\u7279\u5f81\u8fdb\u884c\u6b4c\u66f2\u8bc6\u522b\u7b97\u6cd5\u7684\u8bad\u7ec3\u548c\u6d4b\u8bd5\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5229\u7528<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b97\u6cd5\u6784\u5efa\u4e00\u4e2a\u6709\u6548\u7684\u6b4c\u66f2\u8bc6\u522b\u6a21\u578b\uff1f<\/strong><br \/>\n\u8981\u6784\u5efa\u6b4c\u66f2\u8bc6\u522b\u6a21\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\uff0c\u4f8b\u5982\u652f\u6301\u5411\u91cf\u673a\uff08SVM\uff09\u6216\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\uff08DNN\uff09\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u5927\u89c4\u6a21\u7684\u6b4c\u66f2\u6570\u636e\u96c6\uff0c\u5e76\u5c06\u5176\u5212\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002\u7136\u540e\uff0c\u63d0\u53d6\u97f3\u9891\u7279\u5f81\uff0c\u5982Mel\u9891\u8c31\u56fe\u6216\u6885\u5c14\u9891\u7387\u5012\u8c31\u7cfb\u6570\uff08MFCC\uff09\uff0c\u5e76\u5c06\u5176\u7528\u4f5c\u8f93\u5165\u7279\u5f81\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528\u6240\u9009\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u5bf9\u6a21\u578b\u8fdb\u884c\u8bad\u7ec3\uff0c\u5e76\u4f18\u5316\u6a21\u578b\u7684\u53c2\u6570\u4ee5\u63d0\u9ad8\u51c6\u786e\u6027\u3002\u6700\u540e\uff0c\u5229\u7528\u6d4b\u8bd5\u96c6\u5bf9\u6a21\u578b\u8fdb\u884c\u8bc4\u4f30\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u8c03\u6574\u548c\u6539\u8fdb\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5c06\u6b4c\u66f2\u8bc6\u522b\u529f\u80fd\u4e0e\u7528\u6237\u754c\u9762\u76f8\u7ed3\u5408\uff0c\u5b9e\u73b0\u7b80\u5355\u6613\u7528\u7684\u542c\u6b4c\u8bc6\u66f2\u8f6f\u4ef6\uff1f<\/strong><br \/>\n\u8981\u5c06\u6b4c\u66f2\u8bc6\u522b\u529f\u80fd\u4e0e\u7528\u6237\u754c\u9762\u76f8\u7ed3\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684GUI\u5e93\uff0c\u5982Tkinter\u6216PyQt\u3002\u9996\u5148\uff0c\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u76f4\u89c2\u4e14\u6613\u4e8e\u64cd\u4f5c\u7684\u7528\u6237\u754c\u9762\uff0c\u5305\u62ec\u4e00\u4e2a\u4e0a\u4f20\u97f3\u9891\u6587\u4ef6\u7684\u6309\u94ae\u3002\u7136\u540e\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u4f7f\u7528Python\u4ee3\u7801\u8c03\u7528\u6b4c\u66f2\u8bc6\u522b\u7b97\u6cd5\u6765\u5904\u7406\u4e0a\u4f20\u7684\u97f3\u9891\u6587\u4ef6\uff0c\u5e76\u8fd4\u56de\u8bc6\u522b\u7ed3\u679c\u3002\u6700\u540e\uff0c\u5c06\u8bc6\u522b\u7ed3\u679c\u663e\u793a\u5728\u7528\u6237\u754c\u9762\u4e0a\uff0c\u4ee5\u65b9\u4fbf\u7528\u6237\u67e5\u770b\u5e76\u4eab\u53d7\u8fd9\u4e2a\u542c\u6b4c\u8bc6\u66f2\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u6700\u7b80\u5355\u7684\u542c\u6b4c\u8bc6\u66f2\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7279\u5b9a\u5e93\u548cAPI\u6765\u5b8c\u6210\u3002\u6838\u5fc3\u6280\u672f\u5305\u62ec\u97f3\u9891\u6307\u7eb9\u751f\u6210\u3001\u97f3\u9891\u6570\u636e\u5e93\u5339\u914d [&hellip;]","protected":false},"author":3,"featured_media":174510,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174502"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=174502"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174502\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174510"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}