File size: 3,521 Bytes
c1bb399
be724b2
c1bb399
bc8101c
8dd7f4b
b412759
 
 
 
bc8101c
b412759
 
 
 
 
 
bc8101c
 
3a59ba0
b412759
 
7ce73d8
b412759
 
 
 
 
6bb70a7
 
b412759
c8f3c37
 
 
6bb70a7
3a59ba0
c8f3c37
b412759
 
bc8101c
 
b412759
8dd7f4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a59ba0
8dd7f4b
 
93e6b38
bc8101c
c1bb399
ac9693c
c1bb399
 
 
 
 
 
 
 
 
ac9693c
a0f7c4d
c1bb399
 
fc7d316
b412759
 
56ccd9c
7ce73d8
56ccd9c
3ddc412
c1bb399
 
b412759
 
c1bb399
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import gradio as gr
import os,secrets,requests
from basic_pitch.inference import predict_and_save
from urllib.parse import quote
import traceback
from deta import Deta  # Import Deta

# Initialize
deta = Deta()

# This how to connect to or create a database.
last= deta.Base("last")
records=deta.Base("records")
files=deta.Drive('music')

os.system('chmod +x ./pget')


def addrecord(video_id,orig,pitmp3,mid,gname,ghint):
    if not gname or not ghint:
        return 'E/缺少name或hint.'
    names=list(set(gname.split('\n')))
    lastkey=last.get('last')
    if not lastkey:
        thiskey=0
    else:
        thiskey=lastkey['next']
    if records.fetch({'vid':video_id}).count>0:
        return 'Already In'
    #upload file src
    print(files.put(name=orig,path=orig))
    print(files.put(name=pitmp3,path=pitmp3))
    print(files.put(name=mid,path=mid))

    print(records.put({"vid":video_id,"name":names,"hint":ghint,"orig":orig,"pitmp3":pitmp3,"mid":mid},str(thiskey)))
    print(last.put({'next':thiskey+1},'last'))
    return 'OK'
    


def downloadaud(video_id,gname,ghint):
    try:
        filename=secrets.token_hex()
        # Step 1: Search for videos with the given query
    
        if not video_id:
            return None
    
        # Step 3: Get the formats for the video and find the URL for the best audio-only format
        formats_url = f"https://draw-8fj.begin.app/api/info/{video_id}"
        formats = requests.get(formats_url)
        if formats.ok!=True:
            formats_url = f"https://draw-8fj-staging.begin.app/api/info/{video_id}"
            formats = requests.get(formats_url) 
        formats_response=formats.json()
        #print(formats_response["formats"])
        best_audio_format = None
        for fmt in formats_response.get("formats", []):
            if fmt.get("hasVideo") is False and fmt.get("hasAudio") is True and fmt.get("container") == "mp4":
                if not best_audio_format or fmt.get("audioBitrate") > best_audio_format.get("audioBitrate"):
                    best_audio_format = fmt
        print(best_audio_format)
        # If no suitable audio format was found, return None
        if not best_audio_format:
            return None
    
        aurl=best_audio_format["url"]
        print(aurl)
        command = f'./pget -o {filename}.mp4 -p 4 "{aurl}" '
        os.system(command)
        os.system(f'ffmpeg -i {filename}.mp4 -q:a 0 -map a {filename}.mp3')
        os.system(f'mv {filename}.mp3 /tmp/{filename}.mp3')
        o,m,i= predict(f'/tmp/{filename}.mp3')
        res=addrecord(video_id,o,m,i,gname,ghint)
        return o,m,i,res
    except Exception as e:
        print(traceback.print_exc(e))

def predict(ina):
    fn=ina
    predict_and_save(
    [ina],
    '/'.join(ina.split('/')[:-1]),
    True,
    True,
    False,
    False,
)
    ina='.'.join(ina.split('.')[:-1])
    
    ina=ina+'_basic_pitch'
    os.system(f'ffmpeg -i "{ina}.wav" -f mp3 -acodec libmp3lame -y "{ina}.mp3"')
    
    return f'{fn}',f'{ina}.mp3',f'{ina}.mid'


aud=gr.Textbox(label="视频ID,最长三分半(YouTube视频地址栏的?v=xxxx&...的xxxx)")
g_name=gr.TextArea(label="游戏名称(谜底),回车分名称,一行一个")
g_hint=gr.TextArea(label="开发厂商、发售日期(提示)")
oar=gr.Audio(label="下载的音频")
oa=gr.Audio(label="预览midi")
pf=gr.File(label="下载midi")
res=gr.Textbox(label="结果")
demo = gr.Interface(fn=downloadaud, inputs=[aud,g_name,g_hint], outputs=[oar,oa,pf,res])
demo.launch()