您的位置:首页 > 教育 > 锐评 > 路由器设置虚拟主机_优购物官方网站今日直播喜来健_百度一下百度主页_请输入搜索关键词

路由器设置虚拟主机_优购物官方网站今日直播喜来健_百度一下百度主页_请输入搜索关键词

2025/4/1 1:58:19 来源:https://blog.csdn.net/cf8833/article/details/146444066  浏览:    关键词:路由器设置虚拟主机_优购物官方网站今日直播喜来健_百度一下百度主页_请输入搜索关键词
路由器设置虚拟主机_优购物官方网站今日直播喜来健_百度一下百度主页_请输入搜索关键词

说明:
我希望用python,将name.mp3这段录音文件,添加背景音乐,bg.mp3,然后生成新的文件
step1: 添加依赖

pip install pydub

step2:下载ffmpeg

1.打开windows powershell  ,管理员运行
2.winget install ffmpeg
3.大约160M,等待下载成功
4.关闭windows powershell 
5.重新打开windows powershell 
6.验证
7.where ffmpeg
8.where ffprobe
9.ffmpeg --version
10.ffprobe --version
如果没报错,说明安装成功

step3:在这里创建文件C:\Users\wangrusheng> python hello.py

from pydub import AudioSegment
import osAudioSegment.converter = r"C:\Users\wangrusheng\AppData\Local\Microsoft\WinGet\Links\ffmpeg.exe"# 输入文件路径
name_path = r"C:\Users\wangrusheng\Documents\name.mp3"
bg_path = r"C:\Users\wangrusheng\Documents\bg.mp3"
output_path = r"C:\Users\wangrusheng\Documents\combineds.mp3"# 加载音频文件
name_audio = AudioSegment.from_mp3(name_path)
bg_audio = AudioSegment.from_mp3(bg_path)# 计算主音频长度
duration = len(name_audio)# 延长背景音乐至与主音频同长(循环)
bg_repeated = bg_audio
while len(bg_repeated) < duration:bg_repeated += bg_audio  # 循环叠加
bg_repeated = bg_repeated[:duration]  # 截取相同长度# 降低背景音量(可选,示例降低7分贝)
bg_repeated = bg_repeated - 7# 混合音频(保留主音频,叠加背景)
combined = name_audio.overlay(bg_repeated)# 导出文件
combined.export(output_path, format="mp3")print(f"合并完成,文件已保存至:{output_path}")

step4:重新打开windows powershell ,管理员运行

PS C:\WINDOWS\system32> pwdPath
----
C:\WINDOWS\system32PS C:\WINDOWS\system32> cd ..
PS C:\WINDOWS> cd ..
PS C:\> cd C:\Users\wangrusheng
PS C:\Users\wangrusheng> python --version
Python 3.12.9
PS C:\Users\wangrusheng> python hello.py
合并完成,文件已保存至:C:\Users\wangrusheng\Documents\combined.mp3
PS C:\Users\wangrusheng>

step5:去这个目录,C:\Users\wangrusheng\Documents\combined.mp3,打开文件,播放音频

///分割线

step1:fastapi

from fastapi import FastAPI
from pydub import AudioSegment
import osapp = FastAPI()# 固定路径配置
AudioSegment.converter = r"C:\Users\wangrusheng\AppData\Local\Microsoft\WinGet\Links\ffmpeg.exe"
NAME_PATH = r"C:\Users\wangrusheng\Documents\name.mp3"
BG_PATH = r"C:\Users\wangrusheng\Documents\bg.mp3"
OUTPUT_PATH = r"C:\Users\wangrusheng\Documents\combineds.mp3"@app.get("/combine-audio")
def combine_audio():try:# 加载音频文件name_audio = AudioSegment.from_mp3(NAME_PATH)bg_audio = AudioSegment.from_mp3(BG_PATH)# 计算并循环背景音乐duration = len(name_audio)bg_repeated = bg_audiowhile len(bg_repeated) < duration:bg_repeated += bg_audiobg_repeated = bg_repeated[:duration]# 调整音量并混合bg_repeated = bg_repeated - 7combined = name_audio.overlay(bg_repeated)# 导出文件combined.export(OUTPUT_PATH, format="mp3")return {"status": "success", "output_path": OUTPUT_PATH}except Exception as e:return {"status": "error", "message": str(e)}@app.get("/")
def health_check():return {"status": "ready"}

step2:在windows power shell里面 ,安装fastapi

PS C:\Users\wangrusheng> pip install fastapi uvicorn

step3:运行

PS C:\Users\wangrusheng> uvicorn hello:app --reload
[32mINFO[0m:     Will watch for changes in these directories: ['C:\\Users\\wangrusheng']
[32mINFO[0m:     Uvicorn running on [1mhttp://127.0.0.1:8000[0m (Press CTRL+C to quit)
[32mINFO[0m:     Started reloader process [[36m[1m41936[0m] using [36m[1mStatReload[0m
[32mINFO[0m:     Started server process [[36m39540[0m]
[32mINFO[0m:     Waiting for application startup.
[32mINFO[0m:     Application startup complete.
[32mINFO[0m:     127.0.0.1:55553 - "[1mGET /combine-audio HTTP/1.1[0m" [32m200 OK[0m

step4:在postman里面

get : http://localhost:8000/combine-audio{"status": "success","output_path": "C:\\Users\\wangrusheng\\Documents\\combineds.mp3"
}

step5:去本地路径打开,新生成的mp3文件

end

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com