iPhone语音备忘录转换成bilibili支持的音频稿件的项目,以及1月25的优化
- Aaron
- 随手记
- 2025-01-26
- 70热度
- 0评论
1,需要生成 mp3 wma flac 这三种 目标格式; 2、生成后,希望在网页中显示该文件链接,由用户自主点击下载; 3、重新给出服务器虚拟环境、调试模式等的安全风险解决方案; 4、给出关闭终端,程序仍运行的代码命令;5、希望将该项目集成在 www.insun.xin/m4a 下(就在这个服务器上)而不是直接用IP:5000访问
针对你提出的各个问题的详细解决方案:
5. 将项目集成在
针对你提出的各个问题的详细解决方案:
1. 支持生成 MP3、WMA、FLAC 三种目标格式
要让项目支持生成 MP3、WMA、FLAC 三种目标格式,需要对
app.py
文件进行修改,在选择格式的下拉菜单中添加相应选项,并在文件转换逻辑里处理这三种格式。收起
python
# -*- coding: utf-8 -*-
import os
from flask import Flask, request, send_file, render_template_string
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
@app.route('/', methods=['GET', 'POST'])
def upload_and_convert():
if request.method == 'GET':
html = '''
<!doctype html>
<html>
<head>
<title>上传 M4A 文件并转换</title>
</head>
<body>
<h1>上传 M4A 文件并转换</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<select name="format">
<option value="mp3">MP3</option>
<option value="wma">WMA</option>
<option value="flac">FLAC</option>
</select>
<input type="submit" value="上传并转换">
</form>
</body>
</html>
'''
return render_template_string(html)
elif request.method == 'POST':
file = request.files['file']
if file:
filename = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filename)
target_format = request.form.get('format')
output_filename = os.path.splitext(filename)[0] + '.' + target_format
try:
import ffmpeg
stream = ffmpeg.input(filename)
stream = ffmpeg.output(stream, output_filename)
ffmpeg.run(stream)
# 返回包含下载链接的页面
download_url = f'/download/{os.path.basename(output_filename)}'
html = f'''
<!doctype html>
<html>
<head>
<title>转换完成</title>
</head>
<body>
<h1>转换完成</h1>
<p>点击下面的链接下载转换后的文件:</p>
<a href="{download_url}">{output_filename}</a>
</body>
</html>
'''
return render_template_string(html)
except Exception as e:
return f"转换失败: {str(e)}"
finally:
if os.path.exists(filename):
os.remove(filename)
@app.route('/download/<path:filename>')
def download_file(filename):
file_path = os.path.join(UPLOAD_FOLDER, filename)
return send_file(file_path, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
2. 在网页中显示文件链接供用户下载
在上述修改后的
app.py
代码里,当文件转换完成后,会返回一个包含下载链接的 HTML 页面,用户点击链接就可以下载转换后的文件。3. 服务器虚拟环境、调试模式等的安全风险解决方案
虚拟环境
- 创建和使用虚拟环境:虚拟环境能隔离项目的依赖,避免不同项目之间的依赖冲突。使用以下命令创建和激活虚拟环境:
收起
bash
python3 -m venv venv
source venv/bin/activate
- 安装依赖:在虚拟环境中安装项目所需的依赖,避免全局安装带来的安全风险。
收起
bash
pip install flask ffmpeg-python
调试模式
- 关闭调试模式:调试模式会暴露服务器的详细错误信息,可能被攻击者利用。在生产环境中,要关闭调试模式。将
app.run(debug=True)
改为app.run(debug=False)
。 - 使用 WSGI 服务器:在生产环境中,建议使用专业的 WSGI 服务器(如 Gunicorn 或 uWSGI)来运行 Flask 应用。以 Gunicorn 为例,安装并运行应用:
收起
bash
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:5000 app:app
这里
-w 4
表示使用 4 个工作进程,-b 0.0.0.0:5000
表示绑定到所有网络接口的 5000 端口,app:app
表示运行 app.py
文件中的 app
应用实例。4. 关闭终端,程序仍运行的代码命令
可以使用
nohup
命令让程序在后台运行,即使关闭终端也不会停止。以下是修改后的 bash
文件:收起
bash
#!/bin/bash
# 创建并激活虚拟环境
python3 -m venv venv
source venv/bin/activate
# 安装依赖
pip install flask ffmpeg-python
# 开放 5000 端口
sudo firewall-cmd --zone=public --add-port=5000/tcp --permanent
# 重新加载防火墙规则
sudo firewall-cmd --reload
# 使用 nohup 让 Gunicorn 在后台运行,并将日志输出到 app.log 文件
nohup gunicorn -w 4 -b 0.0.0.0:5000 app:app > app.log 2>&1 &
# 输出进程 ID
echo $! > gunicorn.pid
# 等待一段时间,确保日志有内容写入
sleep 5
# 输出应用日志,方便查看启动情况
cat app.log
5. 将项目集成在 www.insun.xin/m4a
下
要将项目集成到
www.insun.xin/m4a
下,需要进行以下操作:配置 Nginx 反向代理
在服务器上安装 Nginx,并进行如下配置:
收起
bash
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
编辑 Nginx 配置文件
/etc/nginx/conf.d/www.insun.xin.conf
:收起
nginx
server {
listen 80;
server_name www.insun.xin;
location /m4a {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
重新加载 Nginx 配置:
收起
bash
sudo nginx -s reload
修改 Flask 应用的 URL 前缀
在
app.py
中添加 URL 前缀:收起
python
from flask import Flask, request, send_file, render_template_string, url_for
app = Flask(__name__)
app.config['APPLICATION_ROOT'] = '/m4a'
# ... 其他代码保持不变 ...
@app.route('/download/<path:filename>')
def download_file(filename):
file_path = os.path.join(UPLOAD_FOLDER, filename)
return send_file(file_path, as_attachment=True)
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=5000)
通过以上步骤,你就可以将项目集成到
www.insun.xin/m4a
下,用户可以通过该 URL 访问和使用项目。