aka7774 commited on
Commit
e96ebdd
1 Parent(s): f8d4e09

Upload 6 files

Browse files
Files changed (6) hide show
  1. app.py +61 -0
  2. fn.py +52 -0
  3. install.bat +56 -0
  4. main.py +55 -0
  5. requirements.txt +4 -0
  6. venv.sh +7 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import fn
3
+ import gradio as gr
4
+
5
+ fn.load_model()
6
+
7
+ last_url = None
8
+ last_video = None
9
+ last_audio = None
10
+ last_chat = None
11
+ last_info = None
12
+
13
+ def run(url, mode):
14
+ global last_url, last_video, last_audio, last_chat, last_info
15
+
16
+ if url != last_url:
17
+ last_url = url
18
+ if os.path.exists(last_video):
19
+ os.remove(last_video)
20
+ last_video = None
21
+ if os.path.exists(last_audio):
22
+ os.remove(last_audio)
23
+ last_audio = None
24
+ last_chat = None
25
+ last_info = None
26
+
27
+ result = fn.run(url, mode)
28
+
29
+ if mode == 'video':
30
+ ext = os.path.splitext(result)[1]
31
+ os.rename(result, 'last_video' + ext)
32
+ last_video = 'last_video' + ext
33
+ elif mode == 'audio':
34
+ ext = os.path.splitext(result)[1]
35
+ os.rename(result, 'last_audio' + ext)
36
+ last_audio = 'last_audio' + ext
37
+ elif mode == 'chat':
38
+ last_chat = result
39
+ elif mode == 'info':
40
+ last_info = result
41
+
42
+ return last_video, last_audio, last_chat, last_info
43
+
44
+ with gr.Blocks() as demo:
45
+ title = gr.Markdown('# ytdlp')
46
+ url = gr.Textbox(label="URL", interactive=True)
47
+ mode = gr.Dropdown(value='video', choices=['video', 'audio', 'chat', 'info'], label="mode", allow_custom_value=False, interactive=True)
48
+ run_button = gr.Button(value='Run')
49
+ video = gr.Video(label="Video", interactive=False)
50
+ audio = gr.Audio(label="Audio", interactive=False, type="filepath")
51
+ chat = gr.Textbox(label="Chat", interactive=False, show_copy_button=True)
52
+ info = gr.Textbox(label="Info", interactive=False, show_copy_button=True)
53
+
54
+ run_button.click(
55
+ fn=run,
56
+ inputs=[url, mode],
57
+ outputs=[video, audio, chat, info],
58
+ )
59
+
60
+ if __name__ == '__main__':
61
+ demo.launch()
fn.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import glob
3
+ import json
4
+ from yt_dlp import YoutubeDL
5
+
6
+ def load_model():
7
+ pass
8
+
9
+ def run(url, mode = None):
10
+ if not url.startswith('https://'):
11
+ url = f'https://www.youtube.com/watch?v={url}'
12
+ video_id = url[-11:]
13
+
14
+ # cleanup
15
+ for file in glob.glob(f'{video_id}.*'):
16
+ os.remove(file)
17
+
18
+ if mode == 'info':
19
+ with YoutubeDL() as ydl:
20
+ info = ydl.extract_info(url, download=False)
21
+ return json.dumps(info)
22
+ elif mode == 'chat':
23
+ args = {
24
+ 'outtmpl': '%(id)s'+'.%(ext)s',
25
+ 'writesubtitles': True,
26
+ 'skip_download': True,
27
+ }
28
+ with YoutubeDL(args) as ydl:
29
+ result = ydl.download([url])
30
+ print(result)
31
+ for file in glob.glob(f'{video_id}.*'):
32
+ return file
33
+ elif mode == 'audio':
34
+ args = {
35
+ 'outtmpl': '%(id)s'+'.%(ext)s',
36
+ 'format': 'bestaudio',
37
+ }
38
+ with YoutubeDL(args) as ydl:
39
+ result = ydl.download([url])
40
+ print(result)
41
+ for file in glob.glob(f'{video_id}.*'):
42
+ return file
43
+ else:
44
+ args = {
45
+ 'outtmpl': '%(id)s'+'.%(ext)s',
46
+ 'format': 'best',
47
+ }
48
+ with YoutubeDL(args) as ydl:
49
+ result = ydl.download([url])
50
+ print(result)
51
+ for file in glob.glob(f'{video_id}.*'):
52
+ return file
install.bat ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+
3
+ rem -------------------------------------------
4
+ rem NOT guaranteed to work on Windows
5
+
6
+ set REPOS=https://huggingface.co/spaces/aka7774/ytdlp
7
+ set APPDIR=ytdlp
8
+ set VENV=venv
9
+
10
+ rem -------------------------------------------
11
+
12
+ set INSTALL_DIR=%~dp0
13
+ cd /d %INSTALL_DIR%
14
+
15
+ :git_clone
16
+ set DL_URL=%REPOS%
17
+ set DL_DST=%APPDIR%
18
+ git clone %DL_URL% %APPDIR%
19
+ if exist %DL_DST% goto install_python
20
+
21
+ set DL_URL=https://github.com/git-for-windows/git/releases/download/v2.41.0.windows.3/PortableGit-2.41.0.3-64-bit.7z.exe
22
+ set DL_DST=PortableGit-2.41.0.3-64-bit.7z.exe
23
+ curl -L -o %DL_DST% %DL_URL%
24
+ if not exist %DL_DST% bitsadmin /transfer dl %DL_URL% %DL_DST%
25
+ %DL_DST% -y
26
+ del %DL_DST%
27
+
28
+ set GIT=%INSTALL_DIR%PortableGit\bin\git
29
+ %GIT% clone %REPOS%
30
+
31
+ :install_python
32
+ set DL_URL=https://github.com/indygreg/python-build-standalone/releases/download/20240107/cpython-3.10.13+20240107-i686-pc-windows-msvc-shared-install_only.tar.gz
33
+ set DL_DST="%INSTALL_DIR%python.tar.gz"
34
+ curl -L -o %DL_DST% %DL_URL%
35
+ if not exist %DL_DST% bitsadmin /transfer dl %DL_URL% %DL_DST%
36
+ tar -xzf %DL_DST%
37
+
38
+ set PYTHON=%INSTALL_DIR%python\python.exe
39
+ set PATH=%PATH%;%INSTALL_DIR%python310\Scripts
40
+
41
+ :install_venv
42
+ cd %APPDIR%
43
+ %PYTHON% -m venv %VENV%
44
+ set PYTHON=%VENV%\Scripts\python.exe
45
+
46
+ :install_pip
47
+ set DL_URL=https://bootstrap.pypa.io/get-pip.py
48
+ set DL_DST=%INSTALL_DIR%get-pip.py
49
+ curl -o %DL_DST% %DL_URL%
50
+ if not exist %DL_DST% bitsadmin /transfer dl %DL_URL% %DL_DST%
51
+ %PYTHON% %DL_DST%
52
+
53
+ %PYTHON% -m pip install gradio
54
+ %PYTHON% -m pip install -r requirements.txt
55
+
56
+ pause
main.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import time
4
+ import signal
5
+ import psutil
6
+ import io
7
+
8
+ from fastapi import FastAPI, Request, status, Form, UploadFile
9
+ from fastapi.staticfiles import StaticFiles
10
+ from fastapi.middleware.cors import CORSMiddleware
11
+ from pydantic import BaseModel, Field
12
+ from fastapi.exceptions import RequestValidationError
13
+ from fastapi.responses import JSONResponse
14
+
15
+ import fn
16
+ import gradio as gr
17
+ from app import demo
18
+
19
+ app = FastAPI()
20
+
21
+ app.add_middleware(
22
+ CORSMiddleware,
23
+ allow_origins=['*'],
24
+ allow_credentials=True,
25
+ allow_methods=["*"],
26
+ allow_headers=["*"],
27
+ )
28
+
29
+ gr.mount_gradio_app(app, demo, path="/gradio")
30
+
31
+ fn.load_model()
32
+
33
+ @app.post("/raw1")
34
+ async def api_raw1(file: UploadFile = Form(...)):
35
+ try:
36
+ file_content = await file.read()
37
+ file_stream = io.BytesIO(file_content)
38
+
39
+ raw1, raw2 = fn.raw(file_stream)
40
+
41
+ return Response(content=raw1, media_type="audio/wav")
42
+ except Exception as e:
43
+ return {"error": str(e)}
44
+
45
+ @app.post("/raw2")
46
+ async def api_raw2(file: UploadFile = Form(...)):
47
+ try:
48
+ file_content = await file.read()
49
+ file_stream = io.BytesIO(file_content)
50
+
51
+ raw1, raw2 = fn.raw(file_stream)
52
+
53
+ return Response(content=raw2, media_type="audio/wav")
54
+ except Exception as e:
55
+ return {"error": str(e)}
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ yt-dlp
4
+ python-multipart
venv.sh ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/bash
2
+
3
+ python3 -m venv venv
4
+ curl -kL https://bootstrap.pypa.io/get-pip.py | venv/bin/python
5
+
6
+ venv/bin/python -m pip install gradio
7
+ venv/bin/python -m pip install -r requirements.txt