首页 > 其他分享 >Suno 歌曲生成 API 对接说明

Suno 歌曲生成 API 对接说明

时间:2024-07-30 23:26:54浏览次数:9  
标签:url 对接 生成 ai API 歌曲 Suno https suno

Suno 歌曲生成 API 对接说明

随着 AI 的应用变广,各类 AI 程序已逐渐普及。AI 已逐渐深入到人们的工作生活方方面面。而 AI 涉及的行业也越来越多,从最初的写作,到医疗教育,再到现在的音乐。

Suno 是一个专业高质量的 AI 歌曲和音乐创作平台,用户只需输入简单的文本提示词,即可根据流派风格和歌词生成带有人声的歌曲。该 AI 音乐生成器由来自 Meta、TikTok、Kensho 等知名科技公司的团队成员开发,目标是不需要任何乐器工具,让所有人都可以创造美妙的音乐。

Suno 最新已将音乐生成模型升级到 V3 版本,可生成 2 分钟的歌曲。

然而 Suno 官方是并没有提供 API 的,AceDataCloud 提供了一套 Suno 的 API,模拟对接了 Suno 官方,可以方便快捷地生成想要的音乐。

申请和使用

要使用 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 来通过 prompt 生成歌词,API 是 Suno Lyrics Generation API

接下来我们要根据歌词、标题、风格自定义生成歌曲,就可以指定如下内容:

  • lyric:歌词文本
  • custom:填写为 true,代表自定义生成,该参数默认为 false,代表使用 prompt 生成。
  • 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 请求之后,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 字段,其他的字段都和上文类似,通过该字段即可实现任务的关联。

标签:url,对接,生成,ai,API,歌曲,Suno,https,suno
From: https://blog.csdn.net/Acedata1/article/details/140780842

相关文章

  • Python面试题:如何使用Django Rest Framework构建RESTful API
    使用DjangoRestFramework(DRF)构建RESTfulAPI是一个常见且强大的方法。以下是一个详细的指南,帮助你从头开始创建一个基本的Django项目,并使用DRF构建一个RESTfulAPI。环境准备安装Django和DjangoRestFramework:pipinstalldjangodjangorestframewor......
  • 如何利用API接入第三方支付
    在当今的电子商务环境中,集成第三方支付系统变得尤为重要。PassToPay为开发者提供了一个强大而灵活的工具,使他们能够轻松地接入各种支付方式。本教程将逐步展示如何使用PassToPay的支付接口来实现第三方支付集成。准备工作首先,你需要在PassToPay官网注册一个账户,并获取API密钥......
  • graphrag api调用
    """参考:https://microsoft.github.io/graphrag/posts/get_started/1.初始化家目录:python-mgraphrag.index--init--root./ragtest2.初始化索引:python-mgraphrag.index--root./ragtest脚本需要放置在ragtest目录下运行"""importosimportrefromp......
  • 部署 Blender 脚本以用作 Web 服务器上的 api
    我在Nextjs中有一个网站和一个混合器脚本,它获取图像、纹理图像并将它们合并在一起,同时应用一些视觉效果(如深度)、渲染结果并将渲染结果的png图像返回到前端以供使用网站中的img标签。我制作了一个pythonFlask应用程序,安装了搅拌机,并制定了将搅拌机作为子进程运行的路线,......
  • 我用文心快码 Baidu Comate关联了自己的API文档,一键生成代码
    为了让大家快速掌握文心快码BaiduComate智能代码助手的高效使用技巧,我们为你准备了以下简易实操步骤,让你轻松地基于业务API文档生成符合业务规范的新代码。以某银行订单系统的支付业务为例:Step1:上传银行支付系统的API文档Step2:参考关联的API文档,BaiduComate智能代......
  • 如何检查多个依赖项中是否至少有一个在 Fastapi 中传递
    我有一个端点应该适用于两个不同的用户组,如果用户不属于任一组,我想给出正确的错误消息。对于这些组,我创建了也在其他端点中使用的依赖项:defis_teacher(email:str=Depends(get_email),db=Depends(get_db))->bool:teacher=...ifnotteacher:......
  • 为什么在 CDS BETA 后出现 CDS API 格式错误?
    [对于上下文,我使用的是macOS和Python]安装cdsapi后,基本上按照官方网站的用户指南中的说明进行cdsapi设置:https://cds-beta.climate.copernicus.eu/how-to-api,并运行此示例代码进行数据访问,返回此错误{示例代码}importcdsapi客户端=cdsapi.Cli......
  • 雅虎财经 API 未检索数据
    `importstreamlitasstfromdatetimeimportdateimportyfinanceasyffromprophetimportProphetfromprophet.plotimportplot_plotlyfromplotlyimportgraph_objsasgoSTART="2014-01-01"TODAY=date.today().strftime("%Y-%m-%d"......
  • python身份证号码+姓名一致性核验、身份证号码真伪查询API集成
    身份证号码+姓名核验的方式,顾名思义是身份证二要素核验,一般情况下,身份证真伪查询需要上公安户籍系统查询,但此种方式仅适合个人查询,企业要想随时随地实现身份证实名认证的功能,便需要集成身份证实名认证接口功能。翔云人工智能开放平台提供身份证号实名认证接口,实时联网,上传身份证......
  • python API增值税发票四要素核验、数电票查验、医疗票查验
    长期以来,对发票进行高效的管理一直困扰着众多企业财务,手动录入效率慢、出错率高、纸质发票易丢失等。今天,翔云为广大企业提供了发票查验接口与财政票据查验接口服务,可针对增值税发票管理系统开具发票,医疗票据、非税收入等财政类票据进行真伪查验。翔云发票识别接口,使得企业财务无......