mp4 -4 mp3
This commit is contained in:
@@ -11,6 +11,7 @@ router = APIRouter(prefix="/yt-dlp", tags=["yt-dlp"])
|
||||
class DownloadRequest(BaseModel):
|
||||
url: str
|
||||
format: str = "best"
|
||||
audio_only: bool = False
|
||||
|
||||
@router.post("/download")
|
||||
async def download_video(data: DownloadRequest):
|
||||
@@ -19,9 +20,22 @@ async def download_video(data: DownloadRequest):
|
||||
"""
|
||||
save_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../data'))
|
||||
os.makedirs(save_dir, exist_ok=True)
|
||||
# choose output dir and format
|
||||
if data.audio_only:
|
||||
out_dir = os.path.join(save_dir, 'audio')
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
outtmpl = os.path.join(out_dir, '%(title)s.%(ext)s')
|
||||
ydl_format = data.format if data.format != "best" else "bestaudio[ext=m4a]/bestaudio"
|
||||
else:
|
||||
outtmpl = os.path.join(save_dir, '%(title)s.%(ext)s')
|
||||
ydl_format = data.format
|
||||
|
||||
ydl_opts = {
|
||||
'outtmpl': os.path.join(save_dir, '%(title)s.%(ext)s'),
|
||||
'format': data.format,
|
||||
'outtmpl': outtmpl,
|
||||
'format': ydl_format,
|
||||
# Use aria2c for parallel segmented downloading to speed up large files
|
||||
'external_downloader': 'aria2c',
|
||||
'external_downloader_args': ['-x', '16', '-s', '16', '-k', '1M'],
|
||||
}
|
||||
try:
|
||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||
|
||||
Reference in New Issue
Block a user