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