支持fastapi服务

This commit is contained in:
puke
2025-10-28 01:33:36 +08:00
committed by puke
parent c387137446
commit c200761b97
28 changed files with 1854 additions and 4 deletions

View File

@@ -56,6 +56,7 @@ class VideoGeneratorService:
n_scenes: int = 5, # Only used in generate mode; ignored in fixed mode
voice_id: str = "zh-CN-YunjianNeural",
output_path: Optional[str] = None,
use_uuid_filename: bool = False, # Use UUID instead of timestamp for filename
# === LLM Parameters ===
min_narration_words: int = 5,
@@ -194,10 +195,17 @@ class VideoGeneratorService:
# Auto-generate output path if not provided
if output_path is None:
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
# Use first 10 chars of final_title for filename
safe_name = final_title[:10].replace('/', '_').replace(' ', '_')
output_path = f"output/{timestamp}_{safe_name}.mp4"
if use_uuid_filename:
# API mode: use UUID for filename
import uuid
filename = str(uuid.uuid4()).replace('-', '')
output_path = f"output/{filename}.mp4"
else:
# Default mode: use timestamp + title
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
# Use first 10 chars of final_title for filename
safe_name = final_title[:10].replace('/', '_').replace(' ', '_')
output_path = f"output/{timestamp}_{safe_name}.mp4"
# Ensure output directory exists
Path(output_path).parent.mkdir(parents=True, exist_ok=True)

View File

@@ -158,6 +158,32 @@ async def edge_tts(
raise RuntimeError("Edge TTS failed without error (unexpected)")
def get_audio_duration(audio_path: str) -> float:
"""
Get audio file duration in seconds
Args:
audio_path: Path to audio file
Returns:
Duration in seconds
"""
try:
# Try using ffmpeg-python
import ffmpeg
probe = ffmpeg.probe(audio_path)
duration = float(probe['format']['duration'])
return duration
except Exception as e:
logger.warning(f"Failed to get audio duration: {e}, using estimate")
# Fallback: estimate based on file size (very rough)
import os
file_size = os.path.getsize(audio_path)
# Assume ~16kbps for MP3, so 2KB per second
estimated_duration = file_size / 2000
return max(1.0, estimated_duration) # At least 1 second
async def list_voices(locale: str = None, retry_count: int = _RETRY_COUNT, retry_delay: float = _RETRY_DELAY) -> list[str]:
"""
List all available voices for Edge TTS