Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,12 +4,59 @@ import tempfile
|
|
| 4 |
import shutil
|
| 5 |
import os
|
| 6 |
import logging
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
-
logger = logging.getLogger("uvicorn.error")
|
| 13 |
|
| 14 |
@app.post("/merge")
|
| 15 |
async def merge_endpoint(
|
|
@@ -21,33 +68,37 @@ async def merge_endpoint(
|
|
| 21 |
try:
|
| 22 |
saved_files = []
|
| 23 |
for uploaded_file in files:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
with open(file_location, "wb") as out_file:
|
| 27 |
content = await uploaded_file.read()
|
| 28 |
out_file.write(content)
|
| 29 |
-
saved_files.append(
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if len(saved_files) < 2:
|
| 32 |
-
return {"error": "Please upload at least 2
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
if isinstance(output_path, str) and output_path.startswith("Error"):
|
| 44 |
-
return {"error": output_path}
|
| 45 |
-
|
| 46 |
-
# Return the merged file with appropriate media type
|
| 47 |
-
media_type = "video/mp4" if output_path.lower().endswith(".mp4") else "audio/mpeg"
|
| 48 |
-
file_name = os.path.basename(output_path)
|
| 49 |
-
return FileResponse(output_path, media_type=media_type, filename=file_name)
|
| 50 |
|
| 51 |
finally:
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import shutil
|
| 5 |
import os
|
| 6 |
import logging
|
| 7 |
+
import traceback
|
| 8 |
|
| 9 |
+
from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip, CompositeAudioClip, concatenate_audioclips
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
# Configure logging
|
| 13 |
+
logging.basicConfig(level=logging.INFO)
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
def merge_videos_and_audios(video_files, audio_files, orig_vol=1.0, music_vol=0.5, temp_dir=None):
|
| 17 |
+
try:
|
| 18 |
+
output_path = os.path.join(temp_dir, "merged_output.mp4" if video_files else "merged_output.mp3")
|
| 19 |
+
|
| 20 |
+
# If no videos, just concatenate audios
|
| 21 |
+
if not video_files and audio_files:
|
| 22 |
+
audio_clips = [AudioFileClip(a) for a in audio_files]
|
| 23 |
+
final_audio = concatenate_audioclips(audio_clips)
|
| 24 |
+
final_audio.write_audiofile(output_path)
|
| 25 |
+
for clip in audio_clips:
|
| 26 |
+
clip.close()
|
| 27 |
+
final_audio.close()
|
| 28 |
+
return output_path
|
| 29 |
+
|
| 30 |
+
# If videos are present:
|
| 31 |
+
video_clips = [VideoFileClip(v) for v in video_files]
|
| 32 |
+
final_video = concatenate_videoclips(video_clips, method="compose")
|
| 33 |
+
|
| 34 |
+
if audio_files:
|
| 35 |
+
audio_clips = [AudioFileClip(a) for a in audio_files]
|
| 36 |
+
final_audio = concatenate_audioclips(audio_clips).volumex(music_vol)
|
| 37 |
+
original_audio = final_video.audio.volumex(orig_vol) if final_video.audio else None
|
| 38 |
+
if original_audio:
|
| 39 |
+
composite_audio = CompositeAudioClip([original_audio, final_audio])
|
| 40 |
+
else:
|
| 41 |
+
composite_audio = final_audio
|
| 42 |
+
final_video = final_video.set_audio(composite_audio)
|
| 43 |
+
for clip in audio_clips:
|
| 44 |
+
clip.close()
|
| 45 |
+
else:
|
| 46 |
+
final_video = final_video.volumex(orig_vol)
|
| 47 |
+
|
| 48 |
+
final_video.write_videofile(output_path, codec="libx264", audio_codec="aac")
|
| 49 |
+
for clip in video_clips:
|
| 50 |
+
clip.close()
|
| 51 |
+
final_video.close()
|
| 52 |
+
|
| 53 |
+
return output_path
|
| 54 |
+
except Exception as e:
|
| 55 |
+
error_msg = f"Error during merge: {str(e)}\n{traceback.format_exc()}"
|
| 56 |
+
logger.error(error_msg)
|
| 57 |
+
return error_msg
|
| 58 |
|
| 59 |
app = FastAPI()
|
|
|
|
| 60 |
|
| 61 |
@app.post("/merge")
|
| 62 |
async def merge_endpoint(
|
|
|
|
| 68 |
try:
|
| 69 |
saved_files = []
|
| 70 |
for uploaded_file in files:
|
| 71 |
+
file_path = os.path.join(temp_dir, uploaded_file.filename)
|
| 72 |
+
with open(file_path, "wb") as out_file:
|
|
|
|
| 73 |
content = await uploaded_file.read()
|
| 74 |
out_file.write(content)
|
| 75 |
+
saved_files.append(file_path)
|
| 76 |
+
|
| 77 |
+
video_files = [f for f in saved_files if f.lower().endswith(".mp4")]
|
| 78 |
+
audio_files = [f for f in saved_files if f.lower().endswith((".mp3", ".wav"))]
|
| 79 |
|
| 80 |
if len(saved_files) < 2:
|
| 81 |
+
return {"error": "Please upload at least 2 files (videos or audios)."}
|
| 82 |
+
|
| 83 |
+
result_path = merge_videos_and_audios(video_files, audio_files, orig_vol, music_vol, temp_dir)
|
| 84 |
+
|
| 85 |
+
if isinstance(result_path, str) and result_path.startswith("Error"):
|
| 86 |
+
return {"error": result_path}
|
| 87 |
+
|
| 88 |
+
media_type = "video/mp4" if result_path.lower().endswith(".mp4") else "audio/mpeg"
|
| 89 |
+
filename = os.path.basename(result_path)
|
| 90 |
+
return FileResponse(result_path, media_type=media_type, filename=filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
finally:
|
| 93 |
+
shutil.rmtree(temp_dir)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# Log the public URL at startup
|
| 97 |
+
def log_api_url():
|
| 98 |
+
url = os.getenv("SPACE_PUBLIC_URL")
|
| 99 |
+
if url:
|
| 100 |
+
logger.info(f"API is available at: {url}/merge")
|
| 101 |
+
else:
|
| 102 |
+
logger.info("SPACE_PUBLIC_URL environment variable not found")
|
| 103 |
+
|
| 104 |
+
log_api_url()
|