mirror of
https://github.com/R0m1k3/Socialflow.git
synced 2026-10-24 15:38:10 +02:00
main.py fix
This commit is contained in:
1 parent
430187cbad
commit
1050f351c2
1 file changed
+21
-45
+21
-45
@@ -84,66 +84,42 @@ async def process_reel(request: ReelRequest, x_api_key: str = Header(None)):
|
|||||||
# We continue without music if it fails
|
# We continue without music if it fails
|
||||||
|
|
||||||
# 3. Build FFmpeg Command
|
# 3. Build FFmpeg Command
|
||||||
# This is a simplified command. Real implementation for tiktok-style text overlay is complex.
|
|
||||||
# For now, we will draw simple text centered.
|
|
||||||
|
|
||||||
cmd = ["ffmpeg", "-y", "-i", str(input_video_path)]
|
cmd = ["ffmpeg", "-y", "-i", str(input_video_path)]
|
||||||
|
|
||||||
filters = []
|
video_filters = []
|
||||||
|
audio_filters = []
|
||||||
|
|
||||||
# Text Overlay
|
# Text Overlay (Video Filter)
|
||||||
if request.text:
|
if request.text:
|
||||||
# Escape text for drawtext
|
# Escape text for drawtext
|
||||||
sanitized_text = request.text.replace("'", "").replace(":", "\\:")
|
sanitized_text = request.text.replace("'", "").replace(":", "\\:")
|
||||||
# Use a default font or one inside the container
|
# Move text to bottom (with 150px padding to avoid UI elements)
|
||||||
font_path = "/usr/share/fonts/ttf-dejavu/DejaVuSans-Bold.ttf"
|
drawtext = f"drawtext=fontfile={font_path}:text='{sanitized_text}':fontcolor=white:fontsize={request.font_size}:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=h-text_h-150"
|
||||||
drawtext = f"drawtext=fontfile={font_path}:text='{sanitized_text}':fontcolor=white:fontsize={request.font_size}:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h)/2"
|
video_filters.append(drawtext)
|
||||||
filters.append(drawtext)
|
|
||||||
|
|
||||||
# Audio Mixing
|
# Audio Mixing/Volume (Audio Filter)
|
||||||
if has_music:
|
if has_music:
|
||||||
cmd.extend(["-i", str(input_audio_path)])
|
cmd.extend(["-i", str(input_audio_path)])
|
||||||
# Mix original audio (if any) with new music
|
# Apply volume adjustment to the music
|
||||||
# [0:a] is video audio, [1:a] is music
|
audio_filters.append(f"volume={request.music_volume}")
|
||||||
# We need to loop music if video is longer, or cut it.
|
|
||||||
# Simple mix: amix
|
|
||||||
# Better: volume adjustment
|
|
||||||
|
|
||||||
# Complex filter for audio:
|
# Apply Video Filters if any
|
||||||
# [1:a]volume={request.music_volume}[music];[0:a][music]amix=inputs=2:duration=first[aout]
|
if video_filters:
|
||||||
# But what if video has no audio? We need to verify.
|
cmd.extend(["-vf", ",".join(video_filters)])
|
||||||
# For simplicity, we assume video has audio stream. If not, this might fail or produce silent output.
|
|
||||||
# Safer: -map 0:v -map 1:a (replace audio completely) or check streams.
|
|
||||||
# Let's simple check: replacing audio is often safer for "Reels" unless voiceover is needed.
|
|
||||||
# But usually we want background music.
|
|
||||||
# Use filter_complex to mix
|
|
||||||
|
|
||||||
filters.append(f"[1:a]volume={request.music_volume}[music]")
|
# Apply Audio Filters if any
|
||||||
# We assume input video has audio. If not, we map just music.
|
# Note: -af applies to the output audio stream.
|
||||||
# To be robust, we map [music] to output audio.
|
if audio_filters:
|
||||||
# Let's TRY to mix, but fallback to just music if 0:a doesn't exist?
|
cmd.extend(["-af", ",".join(audio_filters)])
|
||||||
# FFmpeg is tricky here.
|
|
||||||
# Strategy: Generate a silent audio track matching video length, mix everything alongside.
|
|
||||||
# Too complex for this snippet.
|
|
||||||
# Decision: Replace audio if music is present, or mix if simple.
|
|
||||||
# Let's just output the music track as the audio for now (Reel style), usually original audio is noise.
|
|
||||||
# Or add it.
|
|
||||||
# [0:a]volume=1.0[orig];[1:a]volume={vol}[mus];[orig][mus]amix=inputs=2[a]
|
|
||||||
|
|
||||||
# Lets try Map 0:v and Map 1:a (Music replaces audio) - Safer MVP
|
|
||||||
# filters.append(...)
|
|
||||||
pass
|
|
||||||
|
|
||||||
filter_str = ",".join(filters)
|
|
||||||
|
|
||||||
if filters:
|
|
||||||
cmd.extend(["-vf", filter_str])
|
|
||||||
|
|
||||||
if has_music:
|
if has_music:
|
||||||
# Map video from 0, audio from 1 (music), shortest (cut music to video len)
|
# Map video from input 0
|
||||||
|
# Map audio from input 1 (music)
|
||||||
|
# -shortest: finish when the shortest input (usually video or music) ends
|
||||||
cmd.extend(["-map", "0:v", "-map", "1:a", "-shortest"])
|
cmd.extend(["-map", "0:v", "-map", "1:a", "-shortest"])
|
||||||
else:
|
else:
|
||||||
cmd.extend(["-map", "0:v", "-map", "0:a?"]) # Keep original audio if exists
|
# Keep original video and audio (if exists)
|
||||||
|
cmd.extend(["-map", "0:v", "-map", "0:a?"])
|
||||||
|
|
||||||
cmd.append(str(output_video_path))
|
cmd.append(str(output_video_path))
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user