73 lines
2.5 KiB
Python
73 lines
2.5 KiB
Python
"""Endpoint to retrieve stream information for an uploaded file."""
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
from backend.config import settings
|
|
from backend.services.file_manager import FileManager
|
|
from backend.services.task_manager import task_manager
|
|
|
|
# Import core functions
|
|
from audio_splitter.ffmpeg import get_stream_info, get_container_format, get_audio_codec
|
|
from audio_splitter.formats import determine_default_format
|
|
from audio_splitter.constants import FORMAT_INFO
|
|
from audio_splitter.defaults import DEFAULT_FORMAT
|
|
|
|
router = APIRouter(prefix="/api", tags=["info"])
|
|
|
|
|
|
@router.get("/info/{task_id}")
|
|
async def get_task_info(task_id: str):
|
|
"""
|
|
Return stream information (has_audio, has_video, has_subtitle, audio_codec)
|
|
for the uploaded file.
|
|
"""
|
|
if not task_manager.has_task(task_id):
|
|
raise HTTPException(status_code=404, detail=f"Task {task_id} not found")
|
|
|
|
input_path = FileManager.get_input_path(task_id)
|
|
if not input_path or not input_path.exists():
|
|
raise HTTPException(status_code=404, detail="Input file not found")
|
|
|
|
try:
|
|
info = get_stream_info(str(input_path))
|
|
return {
|
|
"task_id": task_id,
|
|
"has_audio": info["has_audio"],
|
|
"has_video": info["has_video"],
|
|
"has_subtitle": info["has_subtitle"],
|
|
"audio_codec": info["audio_codec"],
|
|
}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Failed to retrieve stream info: {str(e)}")
|
|
|
|
|
|
@router.get("/info/recommended-format/{task_id}")
|
|
async def get_recommended_format(task_id: str):
|
|
"""
|
|
Return the recommended output format (container name) for the uploaded file,
|
|
based on its container and audio codec.
|
|
"""
|
|
if not task_manager.has_task(task_id):
|
|
raise HTTPException(status_code=404, detail=f"Task {task_id} not found")
|
|
|
|
input_path = FileManager.get_input_path(task_id)
|
|
if not input_path or not input_path.exists():
|
|
raise HTTPException(status_code=404, detail="Input file not found")
|
|
|
|
try:
|
|
container = get_container_format(str(input_path))
|
|
codec = get_audio_codec(str(input_path))
|
|
|
|
fmt = determine_default_format(container, codec)
|
|
|
|
# Fallback if detection fails or format is unsupported
|
|
if fmt is None:
|
|
fmt = DEFAULT_FORMAT
|
|
if fmt not in FORMAT_INFO:
|
|
fmt = "mp3" # ultimate fallback
|
|
|
|
return {"format": fmt}
|
|
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=f"Failed to determine format: {str(e)}")
|