Spaces:
Runtime error
Runtime error
import os | |
from pathlib import Path | |
from tempfile import mkstemp, mkdtemp | |
import gradio as gr | |
from moviepy.editor import VideoFileClip, AudioFileClip | |
import shlex | |
import datetime | |
import subprocess | |
import time | |
end_time=1000 | |
allowed_medias = [".mp3", ".wav", ".ogg"] | |
def execute_command(cmdstring, cwd=None, timeout=None, shell=False): | |
"""执行一个SHELL命令 | |
封装了subprocess的Popen方法, 支持超时判断,支持读取stdout和stderr | |
参数: | |
cwd: 运行命令时更改路径,如果被设定,子进程会直接先更改当前路径到cwd | |
timeout: 超时时间,秒,支持小数,精度0.1秒 | |
shell: 是否通过shell运行 | |
Returns: return_code | |
Raises: Exception: 执行超时 | |
""" | |
global end_time | |
if shell: | |
cmdstring_list = cmdstring | |
else: | |
cmdstring_list = shlex.split(cmdstring) | |
if timeout: | |
end_time = datetime.datetime.now() + datetime.timedelta(seconds=timeout) | |
# 没有指定标准输出和错误输出的管道,因此会打印到屏幕上; | |
sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE, shell=shell, bufsize=4096) | |
# subprocess.poll()方法:检查子进程是否结束了,如果结束了,设定并返回码,放在subprocess.returncode变量中 | |
while sub.poll() is None: | |
time.sleep(0.1) | |
if timeout: | |
if end_time <= datetime.datetime.now(): | |
raise Exception("Timeout:%s" % cmdstring) | |
return str(sub.returncode) | |
def update(file,dclass): | |
if dclass == "": | |
raise gr.Error("请输入舞蹈类型.") | |
if dappear == "": | |
raise gr.Error("请输入人物外观.") | |
file_path = Path(file.name) | |
info = {} | |
info["size"] = os.path.getsize(file_path) | |
info["name"] = file_path.name | |
print(file_path.name) | |
file_extension = file_path.suffix | |
info["type"] = "audio" | |
audio = AudioFileClip(file.name) | |
info["duration"] = audio.duration | |
info["audio_channels"] = audio.nchannels | |
filename = file_path.name | |
###### | |
#info为音频信息 | |
# videopath="myCoolSong.mp4" | |
#返回视频 | |
if info["size"] > 100000000: | |
raise gr.Error( | |
"Please make sure all files are less than 100MB in size." | |
) | |
path = mkdtemp() | |
path2 = mkdtemp() | |
print(path) | |
print(path2) | |
audio_bytes = file.read() | |
with open(path+"/example", 'wb+') as fd: | |
fd.write(audio_bytes) | |
print(fd.name) | |
audio.close() | |
execute_command("bash script_generate.sh transflower_expmap_old example --generate_bvh --generate_video --data_dir="+path+" --seed=seed1 --output_folder="+path2) | |
videopath=path2+"/example.mp4" | |
return videopath | |
# coding=utf-8 | |
with gr.Blocks() as demo: | |
gr.Markdown( | |
""" | |
# <span style="margin-right: 0.3rem;">🏞</span>音频自动生成舞蹈 | |
上传音频,系统会自动生成对应的舞蹈 | |
""", | |
elem_id="header", | |
) | |
with gr.Row(): | |
with gr.Column(): | |
user_file = gr.File( | |
file_count="single", label="音频文件", keep_filename=True, | |
file_types=allowed_medias | |
) | |
dclass=gr.Dropdown(["流行", "民族", "混合"], label="舞蹈类型", info="") | |
dappear=gr.Dropdown(["舞蹈服", "休闲服", "混合"], label="人物外观", info="") | |
number = gr.Slider(minimum=-0, maximum=20, value=1, step=1, | |
interactive=True, label="人物数量") | |
btn = gr.Button("Run", label="Run") | |
with gr.Column(): | |
generated_video = gr.Video( | |
interactive=False, label="舞蹈视频", include_audio=True | |
) | |
# generated_command = gr.Markdown() | |
btn.click( | |
fn=update, | |
inputs=[user_file, dclass], | |
outputs=[generated_video] | |
) | |
if __name__ == "__main__": | |
demo.launch() | |