Spaces:
Running
on
L4
Running
on
L4
add shared ui cropping methods
Browse files
app.py
CHANGED
@@ -15,6 +15,58 @@ snapshot_download(
|
|
15 |
local_dir = "./checkpoints"
|
16 |
)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
import argparse
|
19 |
from omegaconf import OmegaConf
|
20 |
import torch
|
@@ -35,6 +87,18 @@ def main(video_path, audio_path, progress=gr.Progress(track_tqdm=True)):
|
|
35 |
print(f"Input audio path: {audio_path}")
|
36 |
print(f"Loaded checkpoint path: {inference_ckpt_path}")
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
scheduler = DDIMScheduler.from_pretrained("configs")
|
39 |
|
40 |
if config.model.cross_attention_dim == 768:
|
@@ -93,6 +157,12 @@ def main(video_path, audio_path, progress=gr.Progress(track_tqdm=True)):
|
|
93 |
height=config.data.resolution,
|
94 |
)
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
return video_out_path
|
97 |
|
98 |
|
|
|
15 |
local_dir = "./checkpoints"
|
16 |
)
|
17 |
|
18 |
+
from moviepy.editor import VideoFileClip
|
19 |
+
from pydub import AudioSegment
|
20 |
+
|
21 |
+
def process_video(input_video_path, temp_dir="temp_dir"):
|
22 |
+
"""
|
23 |
+
Crop a given MP4 video to a maximum duration of 10 seconds if it is longer than 10 seconds.
|
24 |
+
Save the new video in the specified folder (default is temp_dir).
|
25 |
+
|
26 |
+
Args:
|
27 |
+
input_video_path (str): Path to the input video file.
|
28 |
+
temp_dir (str): Directory where the processed video will be saved.
|
29 |
+
|
30 |
+
Returns:
|
31 |
+
str: Path to the cropped video file.
|
32 |
+
"""
|
33 |
+
# Ensure the temp_dir exists
|
34 |
+
os.makedirs(temp_dir, exist_ok=True)
|
35 |
+
|
36 |
+
# Load the video
|
37 |
+
video = VideoFileClip(input_video_path)
|
38 |
+
|
39 |
+
# Determine the output path
|
40 |
+
input_file_name = os.path.basename(input_video_path)
|
41 |
+
output_video_path = os.path.join(temp_dir, f"cropped_{input_file_name}")
|
42 |
+
|
43 |
+
# Crop the video to 10 seconds if necessary
|
44 |
+
if video.duration > 10:
|
45 |
+
video = video.subclip(0, 10)
|
46 |
+
|
47 |
+
# Write the cropped video to the output path
|
48 |
+
video.write_videofile(output_video_path, codec="libx264", audio_codec="aac")
|
49 |
+
|
50 |
+
# Return the path to the cropped video
|
51 |
+
return output_video_path
|
52 |
+
|
53 |
+
def process_audio(file_path, temp_dir):
|
54 |
+
# Load the audio file
|
55 |
+
audio = AudioSegment.from_file(file_path)
|
56 |
+
|
57 |
+
# Check and cut the audio if longer than 4 seconds
|
58 |
+
max_duration = 8 * 1000 # 4 seconds in milliseconds
|
59 |
+
if len(audio) > max_duration:
|
60 |
+
audio = audio[:max_duration]
|
61 |
+
|
62 |
+
# Save the processed audio in the temporary directory
|
63 |
+
output_path = os.path.join(temp_dir, "trimmed_audio.wav")
|
64 |
+
audio.export(output_path, format="wav")
|
65 |
+
|
66 |
+
# Return the path to the trimmed file
|
67 |
+
print(f"Processed audio saved at: {output_path}")
|
68 |
+
return output_path
|
69 |
+
|
70 |
import argparse
|
71 |
from omegaconf import OmegaConf
|
72 |
import torch
|
|
|
87 |
print(f"Input audio path: {audio_path}")
|
88 |
print(f"Loaded checkpoint path: {inference_ckpt_path}")
|
89 |
|
90 |
+
is_shared_ui = True if "fffiloni/LatentSync" in os.environ['SPACE_ID'] else False
|
91 |
+
temp_dir = None
|
92 |
+
if is_shared_ui:
|
93 |
+
temp_dir = tempfile.mkdtemp()
|
94 |
+
cropped_video_path = process_video(video_path)
|
95 |
+
print(f"Cropped video saved to: {cropped_video_path}")
|
96 |
+
video_path=cropped_video_path
|
97 |
+
|
98 |
+
trimmed_audio_path = process_audio(audio_path, temp_dir)
|
99 |
+
print(f"Processed file was stored temporarily at: {input_audio}")
|
100 |
+
audio_path=trimmed_audio_path
|
101 |
+
|
102 |
scheduler = DDIMScheduler.from_pretrained("configs")
|
103 |
|
104 |
if config.model.cross_attention_dim == 768:
|
|
|
157 |
height=config.data.resolution,
|
158 |
)
|
159 |
|
160 |
+
if is_shared_ui:
|
161 |
+
# Clean up the temporary directory
|
162 |
+
if os.path.exists(temp_dir):
|
163 |
+
shutil.rmtree(temp_dir)
|
164 |
+
print(f"Temporary directory {temp_dir} deleted.")
|
165 |
+
|
166 |
return video_out_path
|
167 |
|
168 |
|