Skip to content

Suno 混音、采样与片段替换 ​

手头有两首歌,要把两首融合,用 MASHUP;从一首里截取素材重新创作,用 SAMPLE;只想改原曲中的一段,用 REPLACE_SECTION。三个示例独立,选择一个运行即可。需要开通对应的 suno_mashup、suno_sample 或 suno_replace_section。

开始前 ​

请在服务端用 Bash,准备 curl(7.76+)和 jq。在一个新的空目录内,依次运行本页命令;每个新任务单独用一个目录,以免混用任务 ID 或覆盖成品。在控制台创建有相应模型权限、分组和余额的 API Key,不要使用工作台登录令牌。

bash
export ABU_API_ORIGIN="https://api.abuai.chat"  # 自建部署改为根地址,不带 /v1 或末尾 /
read -rsp '输入 API Key:' ABU_API_KEY; printf '\n'

先展开下面的代码,在同一个 Bash 会话中运行一次。每个示例都会新建独立任务目录,不会覆盖已有任务。等待中断时进入该任务目录运行 wait_suno,成功后只重跑保存结果的命令,不要重新提交。

提交、轮询和下载函数(先运行)

以下函数在当前 Bash 会话中使用。提交只运行一次;函数会把新任务 ID 存入 suno-id.txt。若提交超时且没有收到 ID,先核对平台调用记录,不要自动重发。

bash
submit_suno() (
  set -e
  if [[ -e suno-id.txt || -e submit.json ]]; then
    echo '本目录已有提交记录。请先查原任务;新任务另建目录。' >&2; exit 1
  fi
  jq -es 'length == 1 and (.[0] | type == "object" and length > 0)' request.json >/dev/null || exit 1
  curl -sS --fail-with-body --max-time 120 "$ABU_API_ORIGIN/suno/submit/$1" \
    -H "Authorization: Bearer ${ABU_API_KEY:?请先输入 API Key}" \
    -H 'Content-Type: application/json' --data-binary @request.json -o submit.json || { cat submit.json >&2; exit 1; }
  id=$(jq -er 'if .code == "success" then (.data | strings | select(length > 0)) else error(tostring) end' submit.json) || exit 1
  printf '%s\n' "$id" | tee suno-id.txt
)

wait_suno() (
  set -e
  id=$(jq -Rer 'select(length > 0) | @uri' suno-id.txt) || exit 1
  deadline=$((SECONDS + 1200))
  while (( SECONDS < deadline )); do
    curl -sS --fail-with-body --max-time 30 "$ABU_API_ORIGIN/suno/fetch/$id" \
      -H "Authorization: Bearer ${ABU_API_KEY:?请先输入 API Key}" -o status.json || { cat status.json >&2; exit 1; }
    state=$(jq -er 'if .code == "success" then .data.status else error(tostring) end' status.json)
    printf '状态:%s\n' "$state"
    case "$state" in
      SUCCESS) exit 0 ;;
      FAILURE) jq -r '.data.fail_reason // .data' status.json >&2; exit 1 ;;
      NOT_START|SUBMITTED|QUEUED|IN_PROGRESS) sleep 5 ;;
      *) cat status.json >&2; exit 1 ;;
    esac
  done
  echo '本地等待超时;保留任务 ID,稍后只运行 wait_suno。' >&2; exit 1
)

save_audio() (
  set -e
  ext=$(jq -er '.audio_format // "mp3" | select(. == "mp3" or . == "wav" or . == "m4a")' request.json) || exit 1
  count=$(jq -er 'select(.code == "success" and .data.status == "SUCCESS") | .data.data | arrays | length | select(. > 0)' status.json) || { echo '成功结果没有音频数组,请检查 status.json。' >&2; exit 1; }
  for ((i=0; i<count; i++)); do
    url=$(jq -er --argjson i "$i" '.data.data[$i].audio_url | strings | select(startswith("https://"))' status.json) || exit 1
    label=$(jq -r --argjson i "$i" '.data.data[$i] | .stem_type // .title // .music_id // "音频"' status.json)
    file="audio-$((i+1)).$ext"
    curl -fsSL --proto '=https' --max-time 300 "$url" -o "$file.part" || exit 1
    test -s "$file.part" && mv "$file.part" "$file" || exit 1
    printf '%s → %s\n' "$file" "$label"
  done
)

查询成功看 data.status == "SUCCESS",外层 code == "success" 仅表示查询成功。歌曲或分轨的链接在 data.data[].audio_url;下载媒体时不发送 API Key。下载中断只重跑 save_audio,原始结果和每首歌的 ID 保留在 status.json。

需要选择已有曲目时,下面的函数按任务 ID 查回成功结果,再让你按序号选作品。请使用原用户和组织的 Key;返回的是匹配的 task_id 和 music_id。在当前会话中先运行函数定义。

bash
choose_source() (
  set -e
  read -rp '来源任务 ID:' source_id
  path=$(jq -ner --arg id "$source_id" '$id | select(length > 0) | @uri')
  curl -sS --fail-with-body --max-time 30 "$ABU_API_ORIGIN/suno/fetch/$path" \
    -H "Authorization: Bearer ${ABU_API_KEY:?请先输入 API Key}" -o source.json || { cat source.json >&2; exit 1; }
  songs=$(jq -ce 'if .code == "success" and .data.status == "SUCCESS" then
    .data.data | if type == "array" then . else [.] end |
    map(select(.music_id | strings | length > 0)) | select(length > 0)
    else error(tostring) end' source.json)
  jq -r 'to_entries[] | [.key+1, (.value.title // "未命名"), .value.music_id] | @tsv' <<<"$songs" >&2
  read -rp '选择第几首(输入上面列出的序号):' n
  [[ "$n" =~ ^[1-9][0-9]*$ ]] || exit 1
  jq -en --arg task "$source_id" --argjson songs "$songs" --argjson n "$n" \
    '$songs[$n-1].music_id | strings | select(length > 0) | {task_id:$task, music_id:.}'
)

两首歌混成新作品 ​

依次选择 A、B 两首原曲。source_task_ids[i] 必须对应 mashup_music_id[i],两个数组都恰好两项;不要用单曲的 task_id / music_id。两首来源必须属于同一平台用户、组织和渠道账号。描述模式必须给非空 gpt_description_prompt。

bash
(
  set -e
  mkdir mashup
  cd mashup
  a=$(choose_source)
  b=$(choose_source)
  jq -en --argjson a "$a" --argjson b "$b" '{
    source_task_ids:[$a.task_id,$b.task_id], mashup_music_id:[$a.music_id,$b.music_id],
    custom:false, gpt_description_prompt:"让第一首的旋律与第二首的节奏逐渐交织", audio_format:"mp3"
  }' > request.json
  submit_suno MASHUP
  wait_suno
  save_audio
)

选一段作为新作品的采样 ​

试听原曲,示例使用 0 到 8 秒作为素材。chop_sample_start_s、chop_sample_end_s 是秒数,不是毫秒或片段长度。起点可以是 0,终点必须更大;把区间调到实际音频长度以内。

bash
(
  set -e
  mkdir sample
  cd sample
  ref=$(choose_source)
  jq -en --argjson ref "$ref" '$ref + {"chop_sample_start_s":0,"chop_sample_end_s":8,"custom":false,"gpt_description_prompt":"以选中片段为动机,改写为弦乐小品","audio_format":"mp3"}' > request.json
  submit_suno SAMPLE
  wait_suno
  save_audio
)

替换指定区间 ​

用 infill_start_s 和 infill_end_s 指定原曲中的区间;示例替换第 12~20 秒,infill_lyrics 是替换段的新歌词。这里不用 SAMPLE 的两个 chop_sample_* 字段,也不发送 custom。

bash
(
  set -e
  mkdir replace
  cd replace
  ref=$(choose_source)
  jq -en --argjson ref "$ref" '$ref + {"infill_start_s":12,"infill_end_s":20,"infill_lyrics":"渡口的灯亮起来","audio_format":"mp3"}' > request.json
  submit_suno REPLACE_SECTION
  wait_suno
  save_audio
)

每种动作都会产生新任务。成品在对应目录的 audio-1.mp3 等文件中,后续加工用新任务的 suno-id.txt 和 status.json 中对应的 music_id。

AbuAI - 让 AI 触手可及