Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,18 +5,26 @@ import shutil
|
|
| 5 |
import os
|
| 6 |
import logging
|
| 7 |
import traceback
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
-
output_path = os.path.join(
|
| 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]
|
|
@@ -26,15 +34,16 @@ def merge_videos_and_audios(video_files, audio_files, orig_vol=1.0, music_vol=0.
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
if original_audio:
|
| 39 |
composite_audio = CompositeAudioClip([original_audio, final_audio])
|
| 40 |
else:
|
|
@@ -43,13 +52,14 @@ def merge_videos_and_audios(video_files, audio_files, orig_vol=1.0, music_vol=0.
|
|
| 43 |
for clip in audio_clips:
|
| 44 |
clip.close()
|
| 45 |
else:
|
| 46 |
-
|
| 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()}"
|
|
@@ -73,27 +83,21 @@ async def merge_endpoint(
|
|
| 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 |
-
|
| 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:
|
|
|
|
| 5 |
import os
|
| 6 |
import logging
|
| 7 |
import traceback
|
| 8 |
+
from moviepy.editor import (
|
| 9 |
+
VideoFileClip,
|
| 10 |
+
concatenate_videoclips,
|
| 11 |
+
AudioFileClip,
|
| 12 |
+
CompositeAudioClip,
|
| 13 |
+
concatenate_audioclips,
|
| 14 |
+
)
|
| 15 |
import numpy as np
|
| 16 |
|
| 17 |
# Configure logging
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
| 21 |
+
def merge_videos_and_audios(
|
| 22 |
+
video_files, audio_files, orig_vol=1.0, music_vol=0.5, temp_dir=None
|
| 23 |
+
):
|
| 24 |
try:
|
| 25 |
+
output_path = os.path.join(
|
| 26 |
+
temp_dir, "merged_output.mp4" if video_files else "merged_output.mp3"
|
| 27 |
+
)
|
| 28 |
# If no videos, just concatenate audios
|
| 29 |
if not video_files and audio_files:
|
| 30 |
audio_clips = [AudioFileClip(a) for a in audio_files]
|
|
|
|
| 34 |
clip.close()
|
| 35 |
final_audio.close()
|
| 36 |
return output_path
|
|
|
|
| 37 |
# If videos are present:
|
| 38 |
video_clips = [VideoFileClip(v) for v in video_files]
|
| 39 |
final_video = concatenate_videoclips(video_clips, method="compose")
|
|
|
|
| 40 |
if audio_files:
|
| 41 |
audio_clips = [AudioFileClip(a) for a in audio_files]
|
| 42 |
final_audio = concatenate_audioclips(audio_clips).volumex(music_vol)
|
| 43 |
+
# Fix here - check if original video audio exists before volumex
|
| 44 |
+
original_audio = (
|
| 45 |
+
final_video.audio.volumex(orig_vol) if final_video.audio is not None else None
|
| 46 |
+
)
|
| 47 |
if original_audio:
|
| 48 |
composite_audio = CompositeAudioClip([original_audio, final_audio])
|
| 49 |
else:
|
|
|
|
| 52 |
for clip in audio_clips:
|
| 53 |
clip.close()
|
| 54 |
else:
|
| 55 |
+
# Fix here - only apply volumex if audio exists
|
| 56 |
+
if final_video.audio is not None:
|
| 57 |
+
final_video = final_video.volumex(orig_vol)
|
| 58 |
+
# else no audio
|
| 59 |
final_video.write_videofile(output_path, codec="libx264", audio_codec="aac")
|
| 60 |
for clip in video_clips:
|
| 61 |
clip.close()
|
| 62 |
final_video.close()
|
|
|
|
| 63 |
return output_path
|
| 64 |
except Exception as e:
|
| 65 |
error_msg = f"Error during merge: {str(e)}\n{traceback.format_exc()}"
|
|
|
|
| 83 |
content = await uploaded_file.read()
|
| 84 |
out_file.write(content)
|
| 85 |
saved_files.append(file_path)
|
|
|
|
| 86 |
video_files = [f for f in saved_files if f.lower().endswith(".mp4")]
|
| 87 |
audio_files = [f for f in saved_files if f.lower().endswith((".mp3", ".wav"))]
|
|
|
|
| 88 |
if len(saved_files) < 2:
|
| 89 |
return {"error": "Please upload at least 2 files (videos or audios)."}
|
| 90 |
+
result_path = merge_videos_and_audios(
|
| 91 |
+
video_files, audio_files, orig_vol, music_vol, temp_dir
|
| 92 |
+
)
|
| 93 |
if isinstance(result_path, str) and result_path.startswith("Error"):
|
| 94 |
return {"error": result_path}
|
|
|
|
| 95 |
media_type = "video/mp4" if result_path.lower().endswith(".mp4") else "audio/mpeg"
|
| 96 |
filename = os.path.basename(result_path)
|
| 97 |
return FileResponse(result_path, media_type=media_type, filename=filename)
|
|
|
|
| 98 |
finally:
|
| 99 |
shutil.rmtree(temp_dir)
|
| 100 |
|
|
|
|
|
|
|
| 101 |
def log_api_url():
|
| 102 |
url = os.getenv("SPACE_PUBLIC_URL")
|
| 103 |
if url:
|