本文主要讲解了在http等协议传输过程中的base64编码~
目录
1.什么是Base64?
2.什么是 Base64 编码?
3.编码规则
4.Base64 编码的作用
5.python中base64库
5.1.将一个mp3文件编码为Base64字符串
5.2.在Flask api设置时,以base64编码的发送文件路径
语音转Base64Demo
Base64 是一种二进制到文本的编码系统,它使用一组 64 个不同的 ASCII 字符将二进制数据传输为基于文本的格式。它通常用于对二进制数据进行编码,例如图像或音频文件。
一般来说,编码是指将数据从一种格式转换为另一种格式的过程。
Base64 编码是通过使用一组 64 个不同的 ASCII 字符表示二进制数据来实现的。当需要在本机不支持二进制数据的系统之间交换数据时,此编码特别有用。
大写字母 (A-Z)、小写字母 (a-z)、数字 (0-9) 和两个附加字符(通常为“+”和“/”)组成了 Base64 编码中使用的 64 个 ASCII 字符集。当二进制数据的长度不是三个字节的倍数时,“=”字符也用于填充。
import base64
with open("audio.mp3", "rb") as f:
encoded_string = base64.b64encode(f.read()).decode()
使用模板
output = {
"audio": [
{
"@TITLE": base64.b64encode(open(out_path, 'rb').read()).decode()
}
]
}
return jsonify(output)
实例
import sys
from flask import Flask, request, jsonify
from flask.views import MethodView
from flask_cors import CORS
import argparse
import base64
import librosa
import numpy as np
import matplotlib.pyplot as plt
import io
import logging
import soundfile
import torch
import commons
import wave
import utils
from models import SynthesizerTrn
from text.symbols import symbols
from text import text_to_sequence
from scipy.io.wavfile import write
import re
from scipy import signal
import time
# check device
if torch.cuda.is_available() is True:
device = "cuda:0"
else:
device = "cpu"
app = Flask(__name__)
CORS(app, resources={r'/*': {"origins": '*'}})
app.logger.setLevel(logging.ERROR)
class run_api(MethodView):
def __init__(self):
pass
def post(self):
#jf_text1 = request.files['text']
jf_text1 = request.json['text']
print("jf_text1:",jf_text1)
def get_text(text, hps):
text_norm = text_to_sequence(text, hps.data.text_cleaners)
if hps.data.add_blank:
text_norm = commons.intersperse(text_norm, 0)
text_norm = torch.LongTensor(text_norm)
return text_norm
fltstr = re.sub(r"[\[\]\(\)\{\}]", "", jf_text1)
stn_tst = get_text(fltstr, hps)
speed = 1
sid = 0
start_time=time.time()
with torch.no_grad():
x_tst = stn_tst.to(device).unsqueeze(0)
x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).to(device)
audio = net_g.infer(x_tst, x_tst_lengths, noise_scale=.667, noise_scale_w=0.8, length_scale=1 / speed)[0][
0, 0].data.cpu().float().numpy()
print("audio type:",audio,audio.shape)
audio_file = write(f'/jf-training-home/src/tts-v1/output/tts_out.wav', hps.data.sampling_rate, audio)
out_path = "path/out.wav"
output = {
"audio": [
{
"@TITLE": base64.b64encode(open(out_path, 'rb').read()).decode()
}
]
}
return jsonify(output)
app.add_url_rule("/", view_func=run_api.as_view("run_api"))
if __name__ == "__main__":
print("Flask server is running...")
path_to_config = "config.json"
path_to_model = "best.pth"
hps = utils.get_hparams_from_file(path_to_config)
if "use_mel_posterior_encoder" in hps.model.keys() and hps.model.use_mel_posterior_encoder == True:
print("Using mel posterior encoder for VITS2")
posterior_channels = 80 # vits2
hps.data.use_mel_posterior_encoder = True
else:
print("Using lin posterior encoder for VITS1")
posterior_channels = hps.data.filter_length // 2 + 1
hps.data.use_mel_posterior_encoder = False
net_g = SynthesizerTrn(
len(symbols),
posterior_channels,
hps.train.segment_size // hps.data.hop_length,
**hps.model).to(device)
_ = net_g.eval()
_ = utils.load_checkpoint(path_to_model, net_g, None)
app.run('0.0.0.0',8555,threaded=True,debug=True)
Audio to Base64 Encoder: Convert Audio to Base64 - B64Encode
选择音频文件后