轻松创作高质量的AI音乐——Suno API

Suno 歌曲生成 API 对接指南

随着人工智能技术的飞速发展,各类 AI 程序已如雨后春笋般涌现。AI 不再是遥不可及的存在,它的身影深入了人类工作与生活的每一个角落。其应用领域也愈加广泛,从初期的写作,到现如今的医疗、教育,甚至扩展至音乐的创作。

Suno 是一个专注于高质量音乐创作的 AI 平台,用户只需输入简短的文本提示,即可根据不同的流派与风格生成带有歌声的歌曲。这个 AI 音乐生成器由 Meta、TikTok、Kensho 等知名科技公司的精英团队研发,其愿望是使每个人不需任何乐器,就能创造出动人的旋律。

Suno 最新已将音乐生成模型升级至 V3 版本,现可制作长达两分钟的歌曲。

不过,遗憾的是,Suno 官方并未提供 API 接口,AceDataCloud 提供了一套模拟 Suno 官方的 API,便捷地帮助用户生成所需的音乐。

注册试用链接

注册试用链接

申请与使用

要使用 Suno Audios API,请首先访问 Suno Audios Generation API 页面,点击「Acquire」按钮以获取请求所需的凭证:

如果你尚未登录或注册,系统会自动跳转到登录页面邀请你进行注册与登录,完成注册后将自动返回至当前页面。

首次申请时,你将享有免费的额度,以便于体验该 API 的功能。

基本使用

在创作歌曲时,你可以自由输入一段文字,例如,如果你想生成一首关于圣诞节的歌曲,可以输入 a song for Christmas,如下图所示:

生成的代码如下:

可点击「Try」按钮直接测试 API,稍等 1-2 分钟后,即可获得如下结果:

{
  "success": true,
  "data": [
    {
      "id": "2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5",
      "title": "Winter Wonderland",
      "image_url": "https://cdn1.suno.ai/image_2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5.png",
      "lyric": "[Verse]\nSnowflakes falling all around\nGlistening white\nCovering the ground\nChildren laughing\nFull of delight\nIn this winter wonderland tonight\nSanta's sleigh\nUp in the sky\nRudolph's nose shining bright\nOh my\nHear the jingle bells\nRinging so clear\nBringing joy and holiday cheer\n[Verse 2]\nRoasting chestnuts by the fire's glow\nChristmas lights\nThey twinkle and show\nFamilies gathering with love and cheer\nSpreading warmth to everyone near",
      "audio_url": "https://cdn1.suno.ai/2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5.mp3",
      "video_url": "https://cdn1.suno.ai/2f16f7bc-4135-42c6-b3c5-6d6c49dc8cd5.mp4",
      "created_at": "2024-05-10T16:21:37.624Z",
      "model": "chirp-v3",
      "prompt": "A song for Christmas",
      "style": "holiday"
    },
    {
      "id": "5dca232b-17cc-4896-a2d1-4b59178bf410",
      "title": "Winter Wonderland",
      "image_url": "https://cdn1.suno.ai/image_5dca232b-17cc-4896-a2d1-4b59178bf410.png",
      "lyric": "[Verse]\nSnowflakes falling all around\nGlistening white\nCovering the ground\nChildren laughing\nFull of delight\nIn this winter wonderland tonight\nSanta's sleigh\nUp in the sky\nRudolph's nose shining bright\nOh my\nHear the jingle bells\nRinging so clear\nBringing joy and holiday cheer\n[Verse 2]\nRoasting chestnuts by the fire's glow\nChristmas lights\nThey twinkle and show\nFamilies gathering with love and cheer\nSpreading warmth to everyone near",
      "audio_url": "https://cdn1.suno.ai/5dca232b-17cc-4896-a2d1-4b59178bf410.mp3",
      "video_url": "https://cdn1.suno.ai/5dca232b-17cc-4896-a2d1-4b59178bf410.mp4",
      "created_at": "2024-05-10T16:21:37.624Z",
      "model": "chirp-v3",
      "prompt": "A song for Christmas",
      "style": "holiday"
    }
  ]
}

如你所见,我们得到了两首歌的内容,包括标题、预览图、歌词、音频和视频链接等信息。

字段说明如下:

  • success:表示生成是否成功,成功则为 true,否者为 false
  • data:包含生成音乐的详细信息列表。
    • id:歌曲 ID
    • title:歌曲标题
    • image_url:封面图片
    • lyric:歌词
    • audio_url:音频文件链接,打开即为 mp3 格式。
    • video_url:视频文件链接,打开即为 mp4 格式。
    • created_at:创作时间
    • model:使用的模型,通常为最新的 v3 模型
    • style:风格

自定义生成

若想要自定义歌词生成,可以直接输入相应的歌词文本:

此时 lyric 字段可以传入如下内容:

[Verse]\nSnowflakes falling all around\nGlistening white\nCovering the ground\nChildren laughing\nFull of delight\nIn this winter wonderland tonight\nSanta's sleigh\nUp in the sky\nRudolph's nose shining bright\nOh my\nHear the jingle bells\nRinging so clear\nBringing joy and holiday cheer\n[Verse 2]\nRoasting chestnuts by the fire's glow\nChristmas lights\nThey twinkle and show\nFamilies gathering with love and cheer\nSpreading warmth to everyone near

注意,歌词中的 \n 是换行符。如果你不知如何生成歌词,可以使用 AceDataCloud 提供的歌词生成 API,借助 Suno Lyrics Generation API 来生成歌词。

接下来,我们可以根据已有的歌词、标题和风格自定义生成歌曲,相关内容可指定如下:

  • lyric:歌词文本
  • custom:填写 true,即进行自定义生成,该参数默认值为 false,代表使用 prompt 生成。
  • instrumental:无歌词的选项,默认为 false,当其为 true 时将忽略输入的 lyric 参数。
  • file:歌曲标题。
  • style:歌曲风格,选择性填写。

填写示例见下:

完成填写后,系统自动生成代码如下:

对应的代码为:

curl -X POST 'https://api.acedata.cloud/suno/audios' \
-H 'authorization: Bearer {token}' \
-H 'accept: application/json' \
-H 'content-type: application/json' \
-d '{
"lyric": "[Verse]\\nSnowflakes falling all around\\nGlistening white\\nCovering the ground\\nChildren laughing\\nFull of delight\\nIn this winter wonderland tonight\\nSanta's sleigh\\nUp in the sky\\nRudolph's nose shining bright\\nOh my\\nHear the jingle bells\\nRinging so clear\\nBringing joy and holiday cheer\\n[Verse 2]\\nRoasting chestnuts by the fire's glow\\nChristmas lights\\nThey twinkle and show\\nFamilies gathering with love and cheer\\nSpreading warmth to everyone near",
"custom": true
}'

经过测试,生成的效果类似。

继续生成

若想在已有歌曲的基础上继续生成,可以将参数 action 设置为 extend,并输入需要继续生成的歌曲 ID,歌曲 ID 可通过基本使用过程得到,如下图所示:

此时可见歌曲的 ID 为:

"id": "b9e9fa11-0bf3-47cd-a3d7-85735aee3e07"

注意,此处的歌词中 id 是生成后的歌曲 ID。如你对生成歌曲的流程尚不明晰,可以参考上文中的基本使用指南。

接下来,我们必须填上歌词、风格以便自定义生成歌曲,相关内容可指定如下:

  • lyric:歌词文本
  • custom:填写 true,即进行自定义生成,该参数默认值为 false,代表使用 prompt 生成。
  • style:歌曲风格,选择性填写。
  • continue_at:以秒为单位继续现有音频的时间。例如,213.5 表示将继续到 3 分 33.5 秒。

填写示例见下:

填写完毕后,系统会自动生成代码如下:

对应的 Python 代码如下:

import requests

url = "https://api.acedata.cloud/suno/audios"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "action": "extend",
    "prompt": "A song for Christmas",
    "audio_id": "b9e9fa11-0bf3-47cd-a3d7-85735aee3e07",
    "continue_at": 10,
    "style": "cute",
    "lyric": "la la la"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

点击运行,你将得到如下结果:

{
  "success": true,
  "task_id": "baf55c32-1207-4bdf-bd00-32a864f0474d",
  "data": [
    {
      "id": "5a3d0054-a6c5-43a9-a348-eae22d1f0efe",
      "title": "",
      "image_url": "https://cdn2.suno.ai/image_5a3d0054-a6c5-43a9-a348-eae22d1f0efe.jpeg",
      "lyric": "la la la",
      "audio_url": "https://cdn1.suno.ai/5a3d0054-a6c5-43a9-a348-eae22d1f0efe.mp3",
      "video_url": "https://cdn1.suno.ai/5a3d0054-a6c5-43a9-a348-eae22d1f0efe.mp4",
      "created_at": "2024-07-25T11:15:49.320Z",
      "model": "chirp-v3.5",
      "prompt": null,
      "style": "cute",
      "duration": 7.96
    },
    {
      "id": "9ae90c96-adcf-4aad-a591-361485168f13",
      "title": "",
      "image_url": "https://cdn2.suno.ai/image_9ae90c96-adcf-4aad-a591-361485168f13.jpeg",
      "lyric": "la la la",
      "audio_url": "https://cdn1.suno.ai/9ae90c96-adcf-4aad-a591-361485168f13.mp3",
      "video_url": "https://cdn1.suno.ai/9ae90c96-adcf-4aad-a591-361485168f13.mp4",
      "created_at": "2024-07-25T11:15:49.321Z",
      "model": "chirp-v3.5",
      "prompt": null,
      "style": "cute",
      "duration": 4.2
    }
  ]
}

从中可见,结果内容与之前一致,这便实现了歌曲的继续生成功能。

异步回调

鉴于 Suno 生成音乐需时相对较长,大约需 1-2 分钟,若 API 长时间不回应,HTTP 请求将持续保持连接,从而消耗额外的系统资源,因此本 API 提供了异步回调的支持。

整体流程是:在客户端发起请求时,额外指定一个 callback_url 字段。发起 API 请求后,系统将迅速返回一个结果,其中包含一个 task_id 字段,代表当前的任务 ID。当任务完成后,生成音乐的结果将通过 POST JSON 的形式发送至客户端所指定的 callback_url,其内容也包括 task_id 字段,这样任务结果即可通过 ID 进行关联。

以下,我们通过示例了解具体的操作方法。

首先,Webhook 回调是一个接收 HTTP 请求的服务,开发者应替换为自己搭建的 HTTP 服务器 URL。在此,为便于演示,使用一个公开的 Webhook 示例网站 https://webhook.site/,打开该网站即可获得 Webhook URL,如下图所示:

复制此 URL 作为 Webhook 使用,上述样例为 https://webhook.site/03e60575-3d96-4132-b681-b713d78116e2。

接下来,我们可以将 callback_url 字段设置为上述 Webhook URL,同时填入 prompt,如下图所示:

点击运行后,即可立即获得如下结果:

{
  "task_id": "44472ab8-783b-4054-b861-5bf14e462f60"
}

稍等片刻,便可在 https://webhook.site/03e60575-3d96-4132-b681-b713d78116e2 上观察到生成歌曲的结果,如下图所示:

内容如下:

{
  "success": true,
  "task_id": "44472ab8-783b-4054-b861-5bf14e462f60",
  "data": [
    {
      "id": "da4324e5-84b2-484b-b0e9-dd261381c594",
      "title": "Winter Whispers",
      "image_url": "https://cdn1.suno.ai/image_da4324e5-84b2-484b-b0e9-dd261381c594.png",
      "lyric": "[Verse]\nSnow falling gently from the sky\nChildren giggling as they pass by\nFire crackling\nCozy and warm\nChristmas spirit begins to swarm\n[Verse 2]\nTwinkling lights\nA sight to behold\nStockings hung\nWaiting to be filled with gold\nGifts wrapped with love\nPiled high\nExcitement in the air\nYou can't deny\n[Chorus]\nWinter whispers in the wind\nJoy and love it brings\nLet's celebrate this season\nWith the ones we're missing",
      "audio_url": "https://cdn1.suno.ai/da4324e5-84b2-484b-b0e9-dd261381c594.mp3",
      "video_url": "https://cdn1.suno.ai/da4324e5-84b2-484b-b0e9-dd261381c594.mp4",
      "created_at": "2024-05-11T07:33:05.430Z",
      "model": "chirp-v3",
      "prompt": "A song for Christmas",
      "style": "pop"
    },
    {
      "id": "b878a87b-a0db-4046-8ccd-ecd2fb3d4372",
      "title": "Winter Whispers",
      "image_url": "https://cdn1.suno.ai/image_b878a87b-a0db-4046-8ccd-ecd2fb3d4372.png",
      "lyric": "[Verse]\nSnow falling gently from the sky\nChildren giggling as they pass by\nFire crackling\nCozy and warm\nChristmas spirit begins to swarm\n[Verse 2]\nTwinkling lights\nA sight to behold\nStockings hung\nWaiting to be filled with gold\nGifts wrapped with love\nPiled high\nExcitement in the air\nYou can't deny\n[Chorus]\nWinter whispers in the wind\nJoy and love it brings\nLet's celebrate this season\nWith the ones we're missing",
      "audio_url": "https://cdn1.suno.ai/b878a87b-a0db-4046-8ccd-ecd2fb3d4372.mp3",
      "video_url": "https://cdn1.suno.ai/b878a87b-a0db-4046-8ccd-ecd2fb3d4372.mp4",
      "created_at": "2024-05-11T07:33:05.430Z",
      "model": "chirp-v3",
      "prompt": "A song for Christmas",
      "style": "pop"
    }
  ]
}

如你所见,结果中包含一个 task_id 字段,其余字段与上文类似,利用此字段便可关联任务。

PyCharm是一个集成开发环境(IDE),主要用于Python语言开发。如果你想利用它创建一个程序来通过Suno API生成音乐并将其下载,你可以按照以下步骤操作: 1. **安装所需库**: 首先,你需要安装`requests`库来发送HTTP请求以及处理API响应,如果尚未安装,可以在命令行中输入 `pip install requests`. 2. **设置Suno API**: 确保你有Suno API的访问权限,并获取到必要的访问密钥或令牌。通常,这涉及到注册一个开发者账号并在文档中找到对应的API端点和下载音乐的URL。 3. **编写代码**: 使用PyCharm编写一个脚本,示例如下: ```python import requests import os def download_sunny_music(api_key, song_id): # 创建请求头 headers = { 'Authorization': f'Token {api_key}', 'Content-Type': 'application/json' } # 构造下载URL base_url = "https://api.suno.com/music/download" url = f"{base_url}/{song_id}" # 发送GET请求 response = requests.get(url, headers=headers) # 检查请求状态码 if response.status_code == 200: # 下载文件 file_path = f'music/{song_id}.mp3' # 根据需求定制保存路径 with open(file_path, 'wb') as file: file.write(response.content) print(f"Music downloaded: {file_path}") else: print(f"Error downloading music: {response.text}") # 使用API密钥和歌单ID替换这里的变量 api_key = "your_api_key" song_id = "your_song_id" download_sunny_music(api_key, song_id) ``` 4. **运行和调试**: 在PyCharm中设置好项目结构,将上述代码放在适当的位置,然后运行这段代码。记得将`api_key`和`song_id`替换为你实际的API密钥和要下载的歌曲ID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值