Spaces:
Build error
Build error
Download utils.py from Taf2023/smart-bold-space: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/Taf2023/smart-bold-space/resolve/main/utils.py
- Command line
-
hf download hf://spaces/Taf2023/smart-bold-space/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Taf2023/smart-bold-space/resolve/main/utils.py
1.21 kB
| import numpy as np | |
| from PIL import Image | |
| import tempfile | |
| from moviepy.editor import ImageClip, AudioArrayClip | |
| import scipy | |
| def create_video(image: "PIL.Image", audio: tuple, duration: float) -> str: | |
| # Convert image to numpy array | |
| img_array = np.array(image) | |
| # Unpack audio data | |
| sample_rate, audio_data = audio | |
| # Ensure audio is the correct duration | |
| target_samples = int(duration * sample_rate) | |
| if audio_data.shape[1] > target_samples: | |
| audio_data = audio_data[:, :target_samples] | |
| else: | |
| # Pad audio if too short | |
| padding = target_samples - audio_data.shape[1] | |
| audio_data = np.pad(audio_data, ((0, 0), (0, padding)), mode='constant') | |
| # Create video clip | |
| img_clip = ImageClip(img_array, duration=duration) | |
| # Create audio clip | |
| audio_clip = AudioArrayClip(audio_data.T, fps=sample_rate) | |
| img_clip = img_clip.set_audio(audio_clip) | |
| # Save to temporary file | |
| with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile: | |
| img_clip.write_videofile( | |
| tmpfile.name, | |
| fps=24, | |
| audio_codec="aac", | |
| logger=None | |
| ) | |
| return tmpfile.name |