feat: use av instead of ffmpeg
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -159,4 +159,6 @@ cython_debug/
|
|||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
|
.DS_Store
|
||||||
/models
|
/models
|
||||||
|
/logs
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
Cython
|
|
||||||
librosa==0.9.1
|
librosa==0.9.1
|
||||||
matplotlib
|
matplotlib
|
||||||
numpy
|
numpy
|
||||||
@@ -15,3 +14,4 @@ transformers
|
|||||||
pypinyin
|
pypinyin
|
||||||
cn2an
|
cn2an
|
||||||
gradio
|
gradio
|
||||||
|
av
|
||||||
|
|||||||
44
server.py
44
server.py
@@ -1,28 +1,15 @@
|
|||||||
from flask import Flask, request, Response
|
from flask import Flask, request, Response
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import ffmpeg
|
|
||||||
import base64
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import json
|
|
||||||
import math
|
|
||||||
import torch
|
import torch
|
||||||
from torch import nn
|
|
||||||
from torch.nn import functional as F
|
|
||||||
from torch.utils.data import DataLoader
|
|
||||||
|
|
||||||
import time
|
|
||||||
import commons
|
import commons
|
||||||
import utils
|
import utils
|
||||||
from models import SynthesizerTrn
|
from models import SynthesizerTrn
|
||||||
from text.symbols import symbols
|
from text.symbols import symbols
|
||||||
from text import cleaned_text_to_sequence,_symbol_to_id, get_bert
|
from text import cleaned_text_to_sequence, get_bert
|
||||||
from text.cleaner import clean_text
|
from text.cleaner import clean_text
|
||||||
from scipy.io import wavfile
|
from scipy.io import wavfile
|
||||||
|
|
||||||
# Get ffmpeg path
|
|
||||||
ffmpeg_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ffmpeg")
|
|
||||||
|
|
||||||
# Flask Init
|
# Flask Init
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['JSON_AS_ASCII'] = False
|
app.config['JSON_AS_ASCII'] = False
|
||||||
@@ -98,24 +85,23 @@ def main():
|
|||||||
fmt = request.args.get("format", "wav")
|
fmt = request.args.get("format", "wav")
|
||||||
if None in (speaker, text):
|
if None in (speaker, text):
|
||||||
return "Missing Parameter"
|
return "Missing Parameter"
|
||||||
if fmt not in ("mp3", "wav"):
|
if fmt not in ("mp3", "wav", "ogg"):
|
||||||
return "Invalid Format"
|
return "Invalid Format"
|
||||||
except:
|
except:
|
||||||
return "Invalid Parameter"
|
return "Invalid Parameter"
|
||||||
|
|
||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
audio = infer(text, sdp_ratio=sdp_ratio, noise_scale=noise, noise_scale_w=noisew, length_scale=length, sid=speaker)
|
audio = infer(text, sdp_ratio=sdp_ratio, noise_scale=noise, noise_scale_w=noisew, length_scale=length, sid=speaker)
|
||||||
wav = BytesIO()
|
|
||||||
wavfile.write(wav, hps.data.sampling_rate, audio)
|
with BytesIO() as wav:
|
||||||
torch.cuda.empty_cache()
|
wavfile.write(wav, hps.data.sampling_rate, audio)
|
||||||
if fmt == "mp3":
|
torch.cuda.empty_cache()
|
||||||
process = (
|
if fmt == "wav":
|
||||||
ffmpeg
|
return Response(wav.getvalue(), mimetype="audio/wav")
|
||||||
.input("pipe:", format='wav', channel_layout="mono")
|
wav.seek(0, 0)
|
||||||
.output("pipe:", format='mp3', audio_bitrate="320k")
|
with BytesIO() as ofp:
|
||||||
.run_async(pipe_stdin=True, pipe_stdout=True, pipe_stderr=True, cmd=ffmpeg_path)
|
utils.wav2(wav, ofp, fmt)
|
||||||
)
|
return Response(
|
||||||
out, _ = process.communicate(input=wav.read())
|
ofp.getvalue(),
|
||||||
return Response(out, mimetype="audio/mpeg")
|
mimetype="audio/mpeg" if fmt == "mp3" else "audio/ogg"
|
||||||
return Response(wav.read(), mimetype="audio/wav")
|
)
|
||||||
|
|||||||
16
utils.py
16
utils.py
@@ -8,6 +8,7 @@ import subprocess
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy.io.wavfile import read
|
from scipy.io.wavfile import read
|
||||||
import torch
|
import torch
|
||||||
|
from av import open as avopen
|
||||||
|
|
||||||
MATPLOTLIB_FLAG = False
|
MATPLOTLIB_FLAG = False
|
||||||
|
|
||||||
@@ -291,3 +292,18 @@ class HParams():
|
|||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__dict__.__repr__()
|
return self.__dict__.__repr__()
|
||||||
|
|
||||||
|
def wav2(i, o, format):
|
||||||
|
inp = avopen(i, 'rb')
|
||||||
|
out = avopen(o, 'wb', format=format)
|
||||||
|
if format == "ogg": format = "libvorbis"
|
||||||
|
|
||||||
|
ostream = out.add_stream(format)
|
||||||
|
|
||||||
|
for frame in inp.decode(audio=0):
|
||||||
|
for p in ostream.encode(frame): out.mux(p)
|
||||||
|
|
||||||
|
for p in ostream.encode(None): out.mux(p)
|
||||||
|
|
||||||
|
out.close()
|
||||||
|
inp.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user