added convert libs

This commit is contained in:
2026-01-15 23:15:25 +00:00
parent b8d4d444fb
commit 157e5180b8
3 changed files with 92 additions and 2 deletions

View File

@@ -5,7 +5,12 @@ from pydantic import BaseModel, ValidationError
from typing import Optional
import uvicorn
import aiomysql
import os
import bcrypt
from pytube import YouTube
from moviepy.editor import AudioFileClip
#import asyncio
# In this FastAPI example, we skip BaseModel verification cause NuxtJS does that for us
@@ -39,10 +44,40 @@ async def fetch_user_info(email: str):
await cur.execute("SELECT * FROM users WHERE email = %s", (email,))
return await cur.fetchone()
# Configuration (customize these!)
DOWNLOAD_FOLDER = "downloads" # Where the converted files will be saved
if not os.path.exists(DOWNLOAD_FOLDER):
os.makedirs(DOWNLOAD_FOLDER)
@app.post("/convert")
async def convert_endpoint(): pass
async def create_conversion(req: Request):
"""Converts a YouTube video to MP3."""
video_url = req.json().get("url")
if (video_url is None): raise HTTPException(400, "Please provide an URL on body")
try:
yt = YouTube(video_url)
stream = yt.streams.filter(only_audio=True).first()
if not stream:
raise HTTPException(status_code=400, detail="No audio stream found.")
# Download the audio stream
temp_file = os.path.join(DOWNLOAD_FOLDER, f"{yt.title}_temp.mp4") # temporary file
stream.download(output_path=DOWNLOAD_FOLDER, filename=f"{yt.title}_temp")
# Convert to MP3 using MoviePy
audio_clip = AudioFileClip(os.path.join(DOWNLOAD_FOLDER, f"{yt.title}_temp.mp4"))
mp3_file = os.path.join(DOWNLOAD_FOLDER, f"{yt.title}.mp3")
audio_clip.write_audiofile(mp3_file)
# Clean up the temporary file
os.remove(os.path.join(DOWNLOAD_FOLDER, f"{yt.title}_temp.mp4"))
return mp3_file # Return the filename of the converted MP3
except Exception as e:
print(f"Error converting video: {e}") # Log the error
raise HTTPException(status_code=500, detail=f"Conversion failed: {str(e)}")
class register_model(BaseModel):
first_name:str
last_name:str

View File

@@ -3,13 +3,52 @@ annotated-doc==0.0.4
annotated-types==0.7.0
anyio==4.12.1
bcrypt==5.0.0
certifi==2026.1.4
click==8.3.1
fastapi[standard]==0.128.0
decorator==5.2.1
dnspython==2.8.0
email-validator==2.3.0
fastapi==0.128.0
fastapi-cli==0.0.20
fastapi-cloud-cli==0.11.0
fastar==0.8.0
h11==0.16.0
httpcore==1.0.9
httptools==0.7.1
httpx==0.28.1
idna==3.11
ImageIO==2.37.2
imageio-ffmpeg==0.6.0
Jinja2==3.1.6
markdown-it-py==4.0.0
MarkupSafe==3.0.3
mdurl==0.1.2
moviepy==2.2.1
numpy==2.4.1
pillow==11.3.0
proglog==0.1.12
pydantic==2.12.5
pydantic-extra-types==2.11.0
pydantic-settings==2.12.0
pydantic_core==2.41.5
Pygments==2.19.2
PyMySQL==1.1.2
python-dotenv==1.2.1
python-multipart==0.0.21
pytube==15.0.0
PyYAML==6.0.3
rich==14.2.0
rich-toolkit==0.17.1
rignore==0.7.6
sentry-sdk==2.49.0
shellingham==1.5.4
starlette==0.50.0
tqdm==4.67.1
typer==0.21.1
typing-inspection==0.4.2
typing_extensions==4.15.0
urllib3==2.6.3
uvicorn==0.40.0
uvloop==0.22.1
watchfiles==1.1.1
websockets==16.0

View File

@@ -0,0 +1,16 @@
aiomysql==0.3.2
annotated-doc==0.0.4
annotated-types==0.7.0
anyio==4.12.1
bcrypt==5.0.0
click==8.3.1
fastapi==0.128.0
h11==0.16.0
idna==3.11
pydantic==2.12.5
pydantic_core==2.41.5
starlette==0.50.0
typing-inspection==0.4.2
typing_extensions==4.15.0
uvicorn==0.40.0